一、资源文件的配置(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%
其中:
格式百分比的格式可以由 # 号的个数去决定。