一、概述
在B/S应用中,将字符串请求参数转换为相应的数据类型,是MVC框架提供的功能,而Struts2是很好的MVC框架实现者,理所当然,提供了类型转换机制。
Struts2的类型转换是基于OGNL表达式的,只要我们把HTML输入项(表单元素和其他GET/POET的参数)命名为合法的OGNL表达式,就可以充分利用Struts2的转换机制。
除此之外,Struts2提供了很好的扩展性,开发者可以非常简单的开发自己的类型转换器,完成字符串和自定义复合类型之间的转换。总之,Struts2 的类型转换器提供了非常强大的表现层数据处理机制,开发者可以利用Struts2的类型转换机制来完成任意的类型转换。
WEB应用接收到表单提交的数据都是String类型的,Struts2可方便的转换为 常规类型(数值,日期) / 自定义类型(User,Point类).
package com.javacrazyer.domain; import java.util.Date; public class User { private Integer id; private String loginname; private Double score; private Character cha; private Date birthday; private Gender gender; public User(){} public User(Integer id, String loginname, Double score, Gender gender, Character cha, Date birthday) { this.id = id; this.loginname = loginname; this.score = score; this.gender = gender; this.cha = cha; this.birthday = birthday; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public Character getCha() { return cha; } public void setCha(Character cha) { this.cha = cha; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String toString() { return "User [birthday=" + birthday + ", cha=" + cha + ", gender=" + gender + ", id=" + id + ", loginname=" + loginname + ", score=" + score+ "]"; } }
package com.javacrazyer.domain; public enum Gender { M,F; }
package com.javacrazyer.domain; public class Tel { private String areaCode; private String phoneCode; public String getAreaCode() { return areaCode; } public void setAreaCode(String areaCode) { this.areaCode = areaCode; } public String getPhoneCode() { return phoneCode; } public void setPhoneCode(String phoneCode) { this.phoneCode = phoneCode; } }
package com.javacrazyer.converter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.javacrazyer.domain.Tel; public class TelConverter extends StrutsTypeConverter { //把String转换成Tel类型 //context:OGNL上下文的Map对象 //values :需要转换的字符串数组 //toClass:要转换的目标类型 @Override public Object convertFromString(Map context, String[] values, Class toClass) { if(toClass == Tel.class){ String str = values[0]; String[] strArr = str.split("-"); Tel tel = new Tel(); tel.setAreaCode(strArr[0]); tel.setPhoneCode(strArr[1]); return tel; } return null; } //把Tel类型转换成String类型 //context:OGNL上下文的Map对象 //o:需要转换的对象 @Override public String convertToString(Map context, Object o) { if(o instanceof Tel){ Tel tel = (Tel)o; return tel.getAreaCode() + "-" + tel.getPhoneCode(); } return null; } }
package com.javacrazyer.web.action; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import com.javacrazyer.domain.Tel; import com.javacrazyer.domain.User; import com.opensymphony.xwork2.ActionSupport; /** * 使用注解来配置Action * */ public class UserAction extends ActionSupport { private static final long serialVersionUID = -2554018432709689579L; private User user; private List<String> list; private List<User> userList; private Set<User> userSet = new HashSet<User>(); private Map<String, String> map = new HashMap<String, String>(); private Tel tel; public String execute() throws Exception{ System.out.println(user); System.out.println(list); for(User user : userList){ System.out.println(user); } System.out.println("========================="); for(User user : userSet){ System.out.println(user); } for (Entry entry:map.entrySet()){ System.out.println(entry.getKey()+"=="+entry.getValue()); } System.out.println("区号:" + this.tel.getAreaCode()); System.out.println("电话号:" + this.tel.getPhoneCode()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public Set<User> getUserSet() { return userSet; } public void setUserSet(Set<User> userSet) { this.userSet = userSet; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Tel getTel() { return tel; } public void setTel(Tel tel) { this.tel = tel; } }
tel=com.javacrazyer.converter.TelConverter #集合属性-Set类型 Element_userSet=com.javacrazyer.domain.User #指定索引 KeyProperty_userSet=id #指定key类型 Key_map=java.lang.String #指定value类型 Element_map=java.lang.String
com.qiujy.domain.Tel=com.javacrazyer.converter.TelConverter
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 --> <constant name="struts.action.extension" value="action,do,go,xkk"/> <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.devMode" value="false"/> <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 是否允许在OGNL表达式中调用静态方法,默认值为false --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> <!-- 指定由spring负责action对象的创建 <constant name="struts.objectFactory" value="spring" /> --> <!-- 是否开启动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <package name="my" extends="struts-default" namespace="/"> <global-results> <result name="input">/error.jsp</result> </global-results> <action name="info" class="com.javacrazyer.web.action.UserAction"> <result name="success">/info.jsp</result> <result name="input">/error.jsp</result> </action> </package> </struts>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2中的转换器</title> </head> <body> <h3>Struts2中的转换器</h3><hr/> <form action="info.action" method="post"> <table> <tr> <td>登录名</td> <td><input type="text" name="user.loginname"/></td> </tr> <tr> <td>生日</td> <td><input type="text" name="user.birthday"/></td> </tr> <tr> <td>性别</td> <!--OGNL表达式:user属性的gender属性[由于是日期类型,程序中自定义有日期转换器]写法 --> <td><input type="radio" name="user.gender" value="M" checked="checked"/>男<input type="radio" name="user.gender" value="F"/>女</td> </tr> <tr> <td>考试成绩</td> <td><input type="text" name="user.score"/></td> </tr> <tr> <td colspan="2"><input type="submit" value=" 提交 "/></td> </tr> </table> <!--OGNL表达式:元素类型为String类型的List的写法 --> <input type="hidden" name="list[0]" value="张三"/> <input type="hidden" name="list[1]" value="李四"/> <input type="hidden" name="list[2]" value="王五"/> <!--OGNL表达式:元素类型为user类型的LIST属性的写法 --> <input type="hidden" name="userList[0].loginname" value="张三"/> <input type="hidden" name="userList[1].loginname" value="李"/> <input type="hidden" name="userList[2].loginname" value="王"/> <!--OGNL表达式:元素类型为user类型的set属性的写法 --> <input type="hidden" name="userSet.makeNew[0].loginname" value="xxx"/> <input type="hidden" name="userSet.makeNew[1].loginname" value="yyy"/> <input type="hidden" name="userSet.makeNew[2].loginname" value="ggg"/> <!--OGNL表达式:map属性的写法 --> <input type="hidden" name="map['jolin']" value="caiyilin"/> <input type="hidden" name="map['jay']" value="zhoujielun"/> <!--OGNL表达式:Tel属性的写法 --> <input type="hidden" name="tel" value="010-66298878"/> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>显示提交的数据</title> </head> <body> <h3>显示提交的数据</h3><hr/> <table> <tr> <td>登录名</td> <td><s:property value="user.loginname"/> </td> </tr> <tr> <td>生日</td> <td><s:date name="user.birthday" format="yyyy-MM-dd HH:mm:ss"/></td> </tr> <tr> <td>性别</td> <td><s:property value="user.gender"/> </td> </tr> <tr> <td>考试成绩</td> <td><s:property value="user.score"/></td> </tr> <tr> <td>电话号</td> <td><s:property value="tel"/></td> </tr> <tr> <td colspan="2"><input type="button" value=" 返回 " onclick="history.go(-1);" /></td> </tr> </table> <hr/><s:debug/> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>出现内部错误</title> </head> <body> <h3>出现内部错误</h3><hr/> <s:actionerror/> </body> </html>