演示页面index.jsp
演示页面submit.jsp
演示页面show.jsp
LocaleConverter.java源代码
package com.action;
import java.lang.reflect.Member;
import java.util.Locale;
import java.util.Map;
// 继承ognl默认的格式转换类
public class LocaleConverter extends ognl.DefaultTypeConverter { @Override // Map context:上下上下文对象; Object value:待转换对象; Class toType:目标类型 public Object convertValue(Map context, Object value, Class toType) { // 如果要转换的是本地化类型Locale.class(如整形:Integer.class) if (toType == Locale.class) { // 把待转换类型强制转换成字符串类型,并取第一个元素 String locale = ((String[]) value)[0]; // 截取字符串去掉"_";如果zh_CN截取得(zh,CN) return new Locale(locale.substring(0, 2), locale.substring(3)); } // 转换成字符串类型 else if (toType == String.class) { Locale locale = (Locale) value; return locale.toString(); } return null; } }
LoginAction.java源代码
package com.action; import java.util.Locale; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.LocalizedTextUtil; @SuppressWarnings("serial") public class LoginAction extends ActionSupport { private String msg; private Locale loc = Locale.CHINA; //默认语言版本的中文 public LoginAction() { super(); // TODO Auto-generated constructor stub } public LoginAction(String msg, Locale loc) { super(); this.msg = msg; this.loc = loc; } @Override public String execute() throws Exception { //LocalizedTextUtil是struts2.0中的国际化初始化工具, //获取当前资源文件中的名为HelloWorld的key,然后使用loc语言版本 msg = LocalizedTextUtil.findDefaultText("HelloWorld", loc); return SUCCESS; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Locale getLoc() { return loc; } public void setLoc(Locale loc) { this.loc = loc; } }
ProductConfirm.java源代码
package com.action; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.pojo.Product; @SuppressWarnings("serial") public class ProductConfirm extends ActionSupport { // 标准list集合,这里为了避免html转码,将<>写为< <>>,使用时候替换即可 private List< <product>> products; public List< <product>> getProducts() { return products; } public void setProducts(List< <product>> products) { this.products = products; } @Override public String execute() throws Exception { //遍历products列表 for (Product p : products) { System.out.println(p.getPname() + "|" + p.getPrice() + "|" + p.getPdate()); } return SUCCESS; } }
Product.java源代码
package com.pojo; import java.io.Serializable; import java.util.Date; @SuppressWarnings("serial") public class Product implements Serializable { //String,double,Date数据类型系统都内置有他们的转换器,无需另写转换器 private String pname; private double price; private Date pdate; public Product() { } 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 Date getPdate() { return pdate; } public void setPdate(Date pdate) { this.pdate = pdate; } public Product(String pname, double price, Date pdate) { super(); this.pname = pname; this.price = price; this.pdate = pdate; } }
src/com.action.ProductConfirm-conversion.properties
ProductConfirm-conversion.properties //固定格式Action名-conversion.properties
Element_products=com.pojo.Product //表明products的转换类型是Product,固定格式添加:Element_集合属性名=集合对象的实现类
Element_products=com.pojo.Product
src/xwork-conversion.properties
xwork-conversion.properties //全局转换配置文件,必须放在SRC目录下,文件名固定!
java.util.Locale = com.action.LocaleConverter //自动调用com.action.LocaleConverter来转换
java.util.Locale =com.action.LocaleConverter
src/struts.properties
struts.custom.i18n.resources=globalMessages
src/globalMessages_en_US.properties
HelloWorld=HelloWorld failtip={0}Login failed\! language=Select language smg=Now is{0} succtip={0}Welcome,Login success. usen=Americal English zhcn=Simplified Chinese
src/globalMessages_zh_CN.properties
HelloWorld=\u4F60\u597D failtip={0}\u767B\u5F55\u5931\u8D25 language=\u9009\u62E9\u8BED\u8A00 smg={0}\u73B0\u5728\u7684\u4E8B\u4EF6\u662F{1} succtip={0}\u6B22\u8FCE,\u767B\u5F55\u6210\u529F usen=\u82F1\u8BED zhcn=\u4E2D\u6587
struts.xml配置文件配置如下