Java 如何使用struts 2标记格式化JSP中请求属性中存储的值(数字)
以下是我的数字格式代码: 1) struts.xmlJava 如何使用struts 2标记格式化JSP中请求属性中存储的值(数字),java,jsp,struts2,Java,Jsp,Struts2,以下是我的数字格式代码: 1) struts.xml <struts> <constant name="struts.custom.i18n.resources" value="cvformat" /> <package name="basicstruts2" extends="struts-default"> <action name="format" class="com.sample.te
<struts>
<constant name="struts.custom.i18n.resources" value="cvformat" />
<package name="basicstruts2" extends="struts-default">
<action name="format" class="com.sample.test.FormatAction">
<result name="success">/sample.jsp</result>
</action>
</package>
</struts>
3) 形成作用
package com.sample.test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
public class FormatAction implements ServletRequestAware {
private double amount;
private HttpServletRequest request;
public String execute() {
amount = 18964.8953;
request.setAttribute("temp", amount);
return "success";
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public HttpServletRequest getServletRequest() {
return request;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
4) sample.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>NUMBER FORMAT EXAMPLE</title>
</head>
<body>
Amount = <s:property value="amount"></s:property><br/><br/>
1) Formating amount(with format hard coded) = <s:property value="getText('{0,number,#,##0.00}',{amount})"/><br/>
2) Formating amount(with format fetched from properties file) = <s:property value="getText('number.format',{amount})"/><br/>
3) Formating amount set in request attribute(with format hard coded) = <s:property value="getText('{0,number,#,##0.00}',#request.temp)"/><br/>
4) Formating amount set in request attribute(with format fetched from properties file) = <s:property value="getText('number.format',#request.temp)"/><br/>
</body>
</html>
如上面的输出所示,只有实例变量值使用存储在属性文件中的模式进行格式化。请告诉我如何格式化JSP中存储在请求属性中的值。您忘记将
#request.temp
包装在{}
->getText(“{0,number,#,##0.00},{#request.temp})”中。
。对我来说效果很好。一种格式比两种格式好,转到struts.custom.i18n.resources。谢谢你的回答。我在{}中包装了#request.temp。案例2和案例4(使用从struts.custom.i18n.resources中选取的模式进行格式化)工作正常,但当模式在jsp中被硬编码时,它就不工作了。无论如何,我将遵循struts.custom.i18n.resources格式。@JavaDeveloper:Woks for me。您正在使用哪个S2版本?我正在使用struts2-core-2.3.24.jar。您忘记将#request.temp
包装在{}
->getText('{0,number,#,0.00},{#request.temp})
。对我来说效果很好。一种格式比两种格式好,转到struts.custom.i18n.resources。谢谢你的回答。我在{}中包装了#request.temp。案例2和案例4(使用从struts.custom.i18n.resources中选取的模式进行格式化)工作正常,但当模式在jsp中被硬编码时,它就不工作了。无论如何,我将遵循struts.custom.i18n.resources格式。@JavaDeveloper:Woks for me。您正在使用哪个S2版本?我正在使用struts2-core-2.3.24.jar。
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>NUMBER FORMAT EXAMPLE</title>
</head>
<body>
Amount = <s:property value="amount"></s:property><br/><br/>
1) Formating amount(with format hard coded) = <s:property value="getText('{0,number,#,##0.00}',{amount})"/><br/>
2) Formating amount(with format fetched from properties file) = <s:property value="getText('number.format',{amount})"/><br/>
3) Formating amount set in request attribute(with format hard coded) = <s:property value="getText('{0,number,#,##0.00}',#request.temp)"/><br/>
4) Formating amount set in request attribute(with format fetched from properties file) = <s:property value="getText('number.format',#request.temp)"/><br/>
</body>
</html>
Amount = 18964.8953
1) Formating amount(with format hard coded) =
2) Formating amount(with format fetched from properties file) = 18,964.90
3) Formating amount set in request attribute(with format hard coded) = 18964.8953
4) Formating amount set in request attribute(with format fetched from properties file) = {0,number,#,##0.00}