转换器(Converter)——Struts 2.0中的魔术师
package tutorial; import java.util.Locale; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.LocalizedTextUtil; public class HelloWorld extends ActionSupport { private String msg; private Locale loc = Locale.US; public String getMsg() { return msg; } public Locale getLoc() { return loc; } public void setLoc(Locale loc) { this .loc = loc; } @Override public String execute() { // LocalizedTextUtil是Struts 2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的 msg = LocalizedTextUtil.findDefaultText( " HelloWorld " , loc); return SUCCESS; } } 然后,在源代码文件夹下的struts.xml加入如下代码新建Action:
< package name ="ConverterDemo" extends ="struts-default" > < action name ="HelloWorld" class ="tutorial.HelloWorld" > < result > /HelloWorld.jsp </ result > </ action > </ package > 再在Web文件夹下,新建 HelloWorld.jsp,代码如下:
< %@ page contentType ="text/html; charset=UTF-8" % > < %@taglib prefix ="s" uri ="/struts-tags" % > < html > < head > < title > Hello World </ title > </ head > < body > < s:form action ="HelloWorld" theme ="simple" > Locale: < s:textfield name ="loc" /> < s:submit /> </ s:form > < h2 >< s:property value ="msg" /></ h2 > </ body > </ html > 接下来,在源代码文件夹的tutorial包中新建LocaleConverter.java文件,代码如下:
package tutorial; import java.util.Locale; import java.util.Map; public class LocaleConverter extends ognl.DefaultTypeConverter { @Override public Object convertValue(Map context, Object value, Class toType) { if (toType == Locale. class ) { String locale = ((String[]) value)[ 0 ]; return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 )); } else if (toType == String. class ) { Locale locale = (Locale) value; return locale.toString(); } return null ; } } 再接下来,在源代码文件夹下新建xwork-conversion.properties,并在其中添加如下代码: java.util.Locale = tutorial.LocaleConverter > > context——用于获取当前的ActionContext 已有的转换器
package tutorial; import java.util.Date; publicclass Product { private String name; privatedouble price; private Date dateOfProduction; public Date getDateOfProduction() { return dateOfProduction; } publicvoid setDateOfProduction(Date dateOfProduction) { this.dateOfProduction = dateOfProduction; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } publicdouble getPrice() { return price; } publicvoid setPrice(double price) { this.price = price; } } 然后,在同上的包下添加ProductConfirm.java类,代码如下:
package tutorial; import java.util.List; import com.opensymphony.xwork2.ActionSupport; publicclass ProductConfirm extends ActionSupport { public List<Product> products; public List<Product> getProducts() { return products; } publicvoid setProducts(List<Product> products) { this.products = products; } @Override public String execute() { for(Product p : products) { System.out.println(p.getName() + " | "+ p.getPrice() +" | " + p.getDateOfProduction()); } return SUCCESS; } } 接看,在同上的包中加入ProductConfirm-conversion.properties,代码如下: Element_products=tutorial.Product <action name="ProductConfirm" class="tutorial.ProductConfirm">
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Hello World</title> </head> <body> <s:form action="ProductConfirm" theme="simple"> <table> <tr style="background-color:powderblue; font-weight:bold;"> <td>Product Name</td> <td>Price</td> <td>Date of production</td> </tr> <s:iterator value="new int[3]" status="stat"> <tr> <td><s:textfield name="%{'products['+#stat.index+'].name'}"/></td> <td><s:textfield name="%{'products['+#stat.index+'].price'}"/></td> <td><s:textfield name="%{'products['+#stat.index+'].dateOfProduction'}"/></td> </tr> </s:iterator> <tr> <td colspan="3"><s:submit /></td> </tr> </table> </s:form> </body> </html> 在同样的文件夹下创建ShowProducts.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Hello World</title> </head> <body> <table> <tr style="background-color:powderblue; font-weight:bold;"> <td>Product Name</td> <td>Price</td> <td>Date of production</td> </tr> <s:iterator value="products" status="stat"> <tr> <td><s:property value="name"/></td> <td>$<s:property value="price"/></td> <td><s:property value="dateOfProduction"/></td> </tr> </s:iterator> </table> </body> </html> 发布运行应用程序,在浏览器中键入 http://localhost:8080/Struts2_Converter/AddProducts.jsp,出现如图4所示页面: > > <result name="input">/AddProducts.jsp</result> > <div style="color:red"> > |