概述:下面通过jsp提交输入注册信息信息,同时完成过程文件国家化问题演示说明。【注册日期转换用注解方式实现】
工程截图:
注册页面jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="tycoon"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- method 默认为:post --> <tycoon:form action="regist" method="post"> <tycoon:textfield label="用户名" name="username"></tycoon:textfield> <tycoon:password label="密码" name="password"></tycoon:password> <tycoon:password label="确认密码" name="repassword"></tycoon:password> <tycoon:textfield label="生日" name="birthday"></tycoon:textfield> <tycoon:textfield label="年龄" name="age"></tycoon:textfield> <tycoon:submit value="登录"></tycoon:submit> </tycoon:form> </body> </html>
success.jsp(成功后跳转页面):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="tycoon"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>success</title> </head> <body> 欢迎,<tycoon:property value="username" />登陆</br> 密码为,<tycoon:property value="password" /></br> 生日,<tycoon:property value="birthday" /></br> 年龄,<tycoon:property value="age" /></br> </body> </html>
Regist.java(注册页面action文件):
package com.tycoon.usermanager; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.conversion.annotations.TypeConversion; public class RegistAction extends ActionSupport { /** * 东北大亨 */ private static final long serialVersionUID = -1245514962211959620L; private String username; private String password; private String repassword; private Date birthday; private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRepassword() { return repassword; } public void setRepassword(String repassword) { this.repassword = repassword; }
// 注解方式实现注册页面日期类型转换 @TypeConversion(converter="com.tycoon.common.DataConver3") public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String execute() throws Exception { System.out.println("调用registAction execute() "); return SUCCESS; } }
日期类型转换类:
package com.tycoon.common; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; /** * 东北大亨 * @author tycoon * */ public class DataConver3 extends StrutsTypeConverter { private SimpleDateFormat[] sdfs = new SimpleDateFormat[] { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy年MM月dd日"), new SimpleDateFormat("yyyy/MM/dd") }; @Override public Object convertFromString(Map context, String[] values, Class arg2) { if (values == null) return null; Date dt = null; for (SimpleDateFormat sdf : sdfs) { try { dt = sdf.parse(values[0]); return dt; } catch (ParseException e) { continue; } } return null; } @Override public String convertToString(Map context, Object value) { if (null == value) return null; if (value instanceof Date) { Date df = (Date) value; System.out.println(" DataConver2 :" + "convertToString"); return sdfs[0].format(df); } return null; } }
国际化文件key及value值:
(1)message_en_US.properties 文件内容:
xwork.default.invalid.fieldvalue=Invalid field value for field "{0}"
(2)message_zh_CN.properties 文件内容:
xwork.default.invalid.fieldvalue=u4F60u8F93u5165u7684u683Cu5F0Fu4E0Du6B63u786E
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置全局国家化资源文件 jsp页面和Action都可以使用 --> <constant name="struts.custom.i18n.resources" value="message"/> <package name="default" namespace="/" extends="struts-default"> <action name="regist" class="com.tycoon.usermanager.RegistAction" method="execute"> <result>/success.jsp</result> <result name="error">/failure.jsp</result> <result name="input">/regist.jsp</result> </action> </package> </struts>
演示效果截图:
未实现国际化输入错误值提示错误信息:
实现国际化输入接收的日期类型值图一:
跳转成功后: