struts2 格式化之数字专题

一、资源文件的配置(applicationResource_zh_CN.properties)

format.number = {0,number,###,###.##}
format.discount = {0,number,###.#######%}



二、struts.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
<constant name="struts.custom.i18n.resources" value="applicationResource"></constant>
<package name="lee" extends="struts-default">
<action name="NumberFormatTest_*" class="com.zzk.test.NumberFormatTest" method="{1}">
<result name="test">/test_show.jsp</result>
</action>
</package>
</struts>

三、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts2.0 study -->
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

四、JavaBean: Product.java

package com.zzk.bean;

public class Product {

private String pname;
private Double price;
private Double discount; //折扣

public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
}

五、Action:

package com.zzk.test;

import com.opensymphony.xwork2.ActionSupport;
import com.zzk.bean.Product;

public class NumberFormatTest extends ActionSupport {

private Product p;

public String init() {
p = new Product();
p.setPname("电脑");
p.setPrice(new Double(10000000000.45));
p.setDiscount(new Double(0.855678));
return "test";
}

public Product getP() {
return p;
}
public void setP(Product p) {
this.p = p;
}
}

五、JSP页面:

test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<a href="NumberFormatTest_init.do">Test</a>
</body>
</html>


test_show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<s:text name="format.number">
<s:param value="p.price"/>
</s:text>
<br>
<s:property value="p.pname"/>
<br>
折扣:<s:text name="format.discount">
<s:param value="p.discount"/>
</s:text>
</body>
</html>

六、显示结果:

10,000,000,000.45
电脑
折扣:85.5678%


其中:

格式百分比的格式可以由 # 号的个数去决定。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值