类型转换
struts2的自定义类型转换机制为复杂类型的输入输出处理提供了便捷.struts2已经为我们提供了几乎所有的primitive类型以及常用类型(如Date)的类型转换器,我们也可以为我们自定义类添加自定义类型转化器.
下面来看一下自定义类型转换
先定义实体类
package entity; import java.util.Date; public class User { private String name;//姓名 private Integer age;//年龄 private Date birthday;//出生日期 public String getName() { return name; } public Integer getAge() { return age; } public Date getBirthday() { return birthday; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
接下来我们创建一个AddUserAction
package action; import com.opensymphony.xwork2.ActionSupport; import entity.User; public class AddUserAction extends ActionSupport { //调用实体类 private User user; public String execute() throws Exception{ //在此我们就用出生日期来实现一下 System.out.println(user.getBirthday()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
接下来我们创建出自己的类型转换器
package converter; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.opensymphony.xwork2.conversion.TypeConversionException; public class Dateconverter extends StrutsTypeConverter{ //将前台获取到的值转换成Date类型 private final DateFormat[] dfs={ new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy年MM月dd日"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy.MM.dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("MM/dd/yyyy"), }; /** * String类转换成特定的类 * 用于前台传值到后台 */ @Override //context里的map对象 前台创来的值 public Object convertFromString(Map context, String[] values, Class toType) { String value=(String)values[0]; for (int i = 0; i < dfs.length; i++) { try { return dfs[i].parse(value); } catch (ParseException e) { // TODO Auto-generated catch block continue; } } throw new TypeConversionException(); } /** * 前台取值 * */ @Override public String convertToString(Map context, Object obj) { return new SimpleDateFormat("yyyy-MM-dd").format(obj); } }
创建AddUserAction-conversion.properties关联关系
当我们用到user.birthday属性时会自动调用converter.Dateconverter.java类型转换器
接下来写配置adduser.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="AddUser" class="action.AddUserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
接下来配置struts.xml文件并引用adduser.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.devMode" value="false"></constant> <constant name="struts.enable.DynamicMethodInvoaction" value="true"></constant> <package name="main" namespace="/" extends="struts-default"> </package> <include file="action/adduser.xml"></include> </struts>
接下来编写输入与展示页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <body> <s:fielderror/> <form action="AddUser" method="post"> 姓名:<input type="text" name="user.name"/><br/> 年龄:<input type="text" name="user.age"/><br/> 出生日期:<input type="text" name="user.birthday"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
展示页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> </head> <body> <s:date name="user.birthday" format="yyyy-MM-dd"/> </body> </html>
效果展示
补充