1 类型转换最好是使用struts2给我们提供好的转化类:StrutsTypeConverter类

StrutsTypeConverter是个抽象类(存在于struts2-core-2.3.4.1.jar的org.apache.struts2.util中的StrutsTypeConverter.class)

  1. package com.test.converter; 
  2.  
  3. import java.util.Map; 
  4.  
  5. import org.apache.struts2.util.StrutsTypeConverter; 
  6.  
  7. import com.test.bean.Point; 
  8.  
  9. public class PointConverter2 extends StrutsTypeConverter { 
  10.  
  11.     @Override 
  12.     public Object convertFromString(Map arg0, String[] values, Class toClass) { 
  13.         // TODO Auto-generated method stub 
  14.         Point point =new Point(); 
  15.         String value=values[0]; 
  16.          
  17.         String[] result=value.split(","); 
  18.          
  19.         point.setX(Integer.parseInt(result[0])); 
  20.         point.setY(Integer.parseInt(result[1])); 
  21.          
  22.         return point; 
  23.     } 
  24.  
  25.     @Override 
  26.     public String convertToString(Map map, Object o) { 
  27.         // TODO Auto-generated method stub 
  28.         Point point =(Point) o; 
  29.          
  30.         int x=point.getX(); 
  31.         int y=point.getY(); 
  32.          
  33.         String result="x:" +x+"y:"+y; 
  34.         return result; 
  35.     } 
  36.  

 2.用一个转换器实现多个类型的转换,批量转换

intput.jsp

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
  2.  
  3. <!-- struts 标签库的引入  --> 
  4. <%@ taglib uri="/struts-tags" prefix="s" %> 
  5.  
  6.  
  7. <% 
  8. String path = request.getContextPath(); 
  9. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
  10. %> 
  11.  
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  13. <html> 
  14.   <head> 
  15.     <base href="<%=basePath%>"
  16.      
  17.     <title>My JSP 'input.jsp' starting page</title> 
  18.      
  19.     <meta http-equiv="pragma" content="no-cache"
  20.     <meta http-equiv="cache-control" content="no-cache"
  21.     <meta http-equiv="expires" content="0">     
  22.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
  23.     <meta http-equiv="description" content="This is my page"
  24.     <!-- 
  25.     <link rel="stylesheet" type="text/css" href="styles.css"
  26.     --> 
  27.  
  28.   </head> 
  29.    
  30.   <body> 
  31.    <h1>输入一个点的坐标,以逗号隔开</h1> 
  32.     <!-- ---------  这是普通方式进行表单输入 
  33.     
  34.     <form action="" method="post"
  35.     point: <input type="text" name="point" size="20"><br> 
  36.     username: <input type="test" name="username" size="20"><br> 
  37.     bithday: <input type="text" name="bithday"><br> 
  38.      
  39.      
  40.     date: <input type="text" name="date" size="20">   <br> 
  41.     <input type="submit" value="submit"
  42.      
  43.     </form> 
  44.      
  45.      
  46.     --> 
  47.      
  48.     <!-- 用struts2 标签库的标签进行设置表单  --> 
  49.      
  50.     <s:form action="converterAction.action" > 
  51.      
  52.         <s:textfield name="point" label="point"></s:textfield> 
  53.         <s:textfield name="point" label="point"></s:textfield> 
  54.         <s:textfield name="point" label="point"></s:textfield> 
  55.         <s:textfield name="point" label="point"></s:textfield> 
  56.         <s:textfield name="username" label="username"></s:textfield> 
  57.         <!--   
  58.         <s:password name="password" label="password"></s:password> 
  59.         --> 
  60.         <s:textfield name="age" label="age"></s:textfield> 
  61.          
  62.         <s:textfield name="birthday" label="birthday"></s:textfield> 
  63.          
  64.         <s:submit name="submit" label="submit"></s:submit> 
  65.         <!--  
  66.         <s:reset name="resert" label="resert"></s:reset> 
  67.           --> 
  68.          
  69.     </s:form> 
  70.     
  71.      
  72.   </body> 
  73. </html> 

其中表单标签中name为point的label的值都为point;

下面编写PointAction.java 利用集合把多个point集合到里面

 

  1. package com.test.action; 
  2. import java.util.Date; 
  3. import java.util.List; 
  4.  
  5. import com.opensymphony.xwork2.ActionSupport; 
  6. import com.sun.org.apache.regexp.internal.recompile; 
  7. import com.test.bean.Point; 
  8. //要实现复杂功能,action都要继承 ActionSupport这个父类,所以以后尽量都要继承这个父类 
  9. public class PointAction extends ActionSupport{ 
  10.      
  11. //  private Point point; 
  12. //  private Point point2; 
  13.     private int age; 
  14.     private String username; 
  15.     private Date birthday; 
  16.      
  17.     private List<Point> list; 
  18.      
  19.      
  20.      
  21. //  public Point getPoint() { 
  22. //      return point; 
  23. //  } 
  24. //  public void setPoint(Point point) { 
  25. //      this.point = point; 
  26. //  } 
  27. //  public Point getPoint2() { 
  28. //      return point2; 
  29. //  } 
  30. //  public void setPoint2(Point point2) { 
  31. //      this.point2 = point2; 
  32. //  } 
  33.      
  34.     public List<Point> getList() { 
  35.         return list; 
  36.     } 
  37.     public void setList(List<Point> list) { 
  38.         this.list = list; 
  39.     } 
  40.      
  41.     public int getAge() { 
  42.         return age; 
  43.     } 
  44.      
  45.      
  46.     public void setAge(int age) { 
  47.         this.age = age; 
  48.     } 
  49.     public String getUsername() { 
  50.         return username; 
  51.     } 
  52.     public void setUsername(String username) { 
  53.         this.username = username; 
  54.     } 
  55.      
  56.      
  57.     public Date getBirthday() { 
  58.         return birthday; 
  59.     } 
  60.     public void setBirthday(Date birthday) { 
  61.         this.birthday = birthday; 
  62.     } 
  63.     public String execute() { 
  64.         return "success"
  65.     } 
  66.      
  67.  

接下俩编写PointConverter3.java转换类