struts2中的Action经过JSP中定义的name属性来获取页面值,在页面传的值均为String类型,在Action中须要对这些值进行类型转换,在struts2中大部分的类型struts2都已经帮开发人员创建好,以下:css
1)boolean和Boolean:完成字符串和布尔值之间的转换;html
2)char和Character:完成字符串和字符之间的转换;java
3)int和Integer:完成字符串和整形值之间的转换;数组
4)long和Long:完成字符串和长整形之间的转换;ide
5)float和Float:完成字符串和浮点值之间的转换;this
6)Date:完成字符串和双精度浮点值之间的转换;spa
在JSP中定义name相同的表单属性:code
<form action=""> <table> <tr> <th width="200"><em>*</em> 用户名称1:</th> <td> <s:textfield type="text" cssClass="inputbox" id="user" name="user"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户名称2:</th> <td> <s:textfield type="text" cssClass="inputbox" id="age" name="user"/> </td> </tr> <input name="submitbutton" id="submitbutton" type="button" value="保存" /> </table> </form>
在Action中获取用户名称这个String[],定义字符串数组名字为user便可以获取值;注意要定义set和get方法;orm
//action其余代码省略 private String[] user; public String[] getUser() { return user; } public void setUser(String[] user) { this.user = user; }
一样在JSP页面中:经过user.userName和user.age的形式将参数值赋给Action中的User对象;xml
<form action=""> <table> <tr> <th width="200"><em>*</em> 用户名称:</th> <td> <s:textfield type="text" cssClass="inputbox" name="user.userName"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户年龄:</th> <td> <s:textfield type="text" cssClass="inputbox" id="age" name="user.age"/> </td> </tr> <input name="submitbutton" id="submitbutton" type="button" value="保存" /> </table> </form>
在Action中经过定义User user属性而且设置set和get方法获取;
//action其余代码省略 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }
一样在JSP页面中:经过集合名词[集合下标].集合对象中的属性的形式将参数值赋给Action中的集合;
<form action=""> <table> <tr> <th width="200"><em>*</em> 用户名称1:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userList[0].userName"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户年龄1:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userList[0].age"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户名称2:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userList[1].userName"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户年龄2:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userList[1].age"/> </td> </tr> <input name="submitbutton" id="submitbutton" type="button" value="保存" /> </table> </form>
在Action中经过定义List<User> user属性而且设置set和get方法获取;
//action其余代码省略 private List<User> userList; public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList= userList; }
运行结果以下:若JSP页面下标指定从1开始,struts2会默认将集合中的第一个设置为null
在JSP页面中name设置为Map对象[Map的Key].泛型对象中的属性值;
<form action=""> <table> <tr> <th width="200"><em>*</em> 用户名称1:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userMap['one'].userName"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户年龄1:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userMap['one'].age"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户名称2:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userMap['two'].userName"/> </td> </tr> <tr> <th width="200"><em>*</em> 用户年龄2:</th> <td> <s:textfield type="text" cssClass="inputbox" name="userMap['two'].age"/> </td> </tr> <input name="submitbutton" id="submitbutton" type="button" value="保存" /> </table> </form>
一样在Action中经过定义Map<String, User> mapUser属性而且设置set和get方法获取;
可是这些都是已经为struts2指定好具体的对象的状况下,若是上面的List没有指定具体的泛型,List User这样的状况该如何转换类型呢?
1)在放置Action相同目录下新建ActionName-conversion.properties文件ActionName为Action的类名;
2)在ActionName-conversion.properties文件中指定key-value;
如为List指定:Element_集合名词=集合类型的完整限定名
Element_userList=com.shcredit.controller.type.User
为Map指定:需同时指定Map的key属性和value属性
Key_<MapPropName>=<KeyType> Element_<MapPropName>=<ValueType>
MapPropName是Map类型的属性名,KeyType和ValueType为对应类型的quan全限类名;
当JSP页面传的为一个字符串类型user,Action中的user确为一个User 对象,这种状况又该如何处理呢?
<form action=""> <table> <tr> <th width="200"><em>*</em> 用户名称和年龄:</th> <td> <s:textfield type="text" cssClass="inputbox" name="user"/> </td> </tr> <input name="submitbutton" id="submitbutton" type="button" value="保存" /> </table> </form>
//action其余代码省略 private User user; public User getUser() { return user; } public void setUser(User user) { this.user= user; }
1)ONGL提供了类型转换器接口TypeConverter,自定义类型转换器能够实现该接口;实现该接口的convertValue方法比较复杂。
2)OGNL提供了DefaultTypeConverter实现类,这也是较为经常使用的方法;
(局部类型转换器)在跟Action相同目录下新建ActionName-conversion.properties。在该文件下指定Action中须要进行类型转换的name对应的转换类;这样JSP根据name中的name去Struts2中默认的类型转换中去找,没有找到会去该Action目录下寻找对应的conversion.properties文件(若src下存在xwork-conversion.properties文件(全局)则局部类型转换器优先),经过该文件指定对应的类型转换器,该类型转换器进行转换后,Action经过set方法将返回值set进Action中;
user=com.shcredit.controller.type.UserConver
public class UserConver extends DefaultTypeConverter { @Override public Object convertValue(Map context, Object value, Class toType) { if(toType == User.class){ //系统的请求参数是一个字符串数组 String[] params = (String[])value; User user = new User(); String[] userValues = params[0].split(","); user.setUserName(userValues[0]); user.setAge(userValues[1]); return user; }else if(toType == String.class){ User user = (User)value; return "<"+user.getUserName()+","+user.getAge()+">"; } return null; } }
这样虽然能解决问题,可是每个Action都须要去配置对应的ActionName-conversion.properties文件,有什么办法解决呢?
(全局类型转换器)它不是对特定的Action起做用,而是对指定类型起做用。
1)在src下新建xwork-conversion.properties文件,为须要进行类型转换的类进行指定:
com.shcredit.controller.type.User=com.shcredit.controller.type.UserConver
它会对全部的Action的特定类型起做用;其余步骤类似;
为了简化类型转化器的实现,Struts2提供了一个StrutsTypeConverter抽象类,该类时DefaultTypeConverter的子类,该类已经实现了convertFromString方法(将字符串转换成复合类型)和convertToString方法(将复合类型转换为字符串)。
public class StrutsUserConerter extends StrutsTypeConverter { @Override public Object convertFromString(Map arg0, String[] arg1, Class arg2) { User user = new User(); //只处理请求参数数组第一个数组元素 String[] userValues = arg1[0].split(","); user.setUserName(userValues[0]); user.setAge(userValues[1]); //返回转换后的User实列 return user; } @Override public String convertToString(Map arg0, Object arg1) { User user = (User)arg1; return "<"+user.getUserName()+","+user.getAge()+">"; } }