在struts2里面对八种基本数据类型和Date类型有自动的转换功能,也可以自定义类型转换.
主要步骤如下:
- 编写自定义类型的类(JavaBean).
- 编写自定义类型转换器(继承StrutsTypeConverter类或者DefaultTypeConverter类)
- 编写Action(继承ActionSupport类)
- 在Action的同一目录下建立ActionName-conversion.properties(ActionName是Action的类名,conversion.properties是固定后缀)
结构:
用登陆功能来分析:
JavaBean:User.java
1 package com.sunflower.bean; 2 3 public class User { 4 private String username; 5 private String password; 6 7 public String getUsername() { 8 return username; 9 } 10 11 public void setUsername(String username) { 12 this.username = username; 13 } 14 15 public String getPassword() { 16 return password; 17 } 18 19 public void setPassword(String password) { 20 this.password = password; 21 } 22 23 }
类型转换器:UserConvert.java
1 package com.sunflower.util; 2 3 import java.util.Map; 4 5 import org.apache.struts2.util.StrutsTypeConverter; 6 7 import com.sunflower.bean.User; 8 9 /** 10 * 自定义类型转换类 11 * 12 * @author hanyuan 13 * @time 2012-6-2 下午06:12:13 14 */ 15 public class UserConvert extends StrutsTypeConverter { 16 17 /** 18 * 将前台字符类型数据转换为自定义类型 19 */ 20 public Object convertFromString(Map arg0, String[] arg1, Class arg2) { 21 22 String userinfo = arg1[0]; 23 String username = userinfo.split(":")[0]; 24 String password = userinfo.split(":")[1]; 25 26 User user = new User(); 27 user.setUsername(username); 28 user.setPassword(password); 29 30 return user; 31 } 32 33 /** 34 * 将后台自定义类型数据转换为前台字符类型 35 */ 36 public String convertToString(Map arg0, Object arg1) { 37 User user = (User) arg1; 38 39 String userinfo = ""; 40 if (user.getUsername().equals("yuan") && user.getPassword().equals("123")) 41 userinfo = user.getUsername(); 42 else 43 userinfo = "username:" + user.getUsername() + " password:" + user.getPassword(); 44 return userinfo; 45 } 46 47 }
Action:LoginAction.java
1 package com.sunflower.action; 2 3 import com.opensymphony.xwork2.Action; 4 import com.opensymphony.xwork2.ActionSupport; 5 import com.sunflower.bean.User; 6 7 /** 8 * 校验用户信息的Action 9 * 10 * @author hanyuan 11 * @time 2012-6-2 下午05:08:54 12 */ 13 public class LoginAction extends ActionSupport { 14 15 private User user; 16 17 public User getUser() { 18 return user; 19 } 20 21 public void setUser(User user) { 22 this.user = user; 23 } 24 25 public String execute() throws Exception { 26 // 判断用户名和密码是否正确 27 if (user.getUsername().equals("yuan") && user.getPassword().equals("123")) 28 return Action.SUCCESS; 29 else 30 return Action.ERROR; 31 } 32 }
Properties:LoginAction-conversion.properties
user是要对其进行转换的数据对象,com.sunflower.util.UserConvert是对应的类型转换器
struts.xml中的配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="struts2" extends="struts-default"> 8 <action name="logintest" class="com.sunflower.action.LoginAction"> 9 <result name="success">/login.jsp</result> 10 <result name="error">/failed.jsp</result> 11 </action> 12 </package> 13 14 </struts>