slowalker

导航

struts2 类型转换

 

为什么需要进行类型转换?

  服务器/浏览器结构的应用参数是通过浏览器传递给服务器的, 这些参数不可能拥有丰富的数据结构, 因此需要在服务器端完成数据类型转换.

  浏览器发送的参数为String类型的, 服务器需要将String类型转换为多种数据类型.

  所以类型转换是对于服务器端而言的, 因为服务器需要用到多种数据类型以进行相应的业务逻辑.

 

如何进行类型转换?

  1.传统的数据类型转换

      对于一个Servlet, 其中通过getparameter()方法获得的参数均为字符串.如示例中的19 - 22 行所示.

  对于age属性, 传递的是一个字符串, 需要将其转换为int类型

  对于birth属性, 传递的是一个字符串, 需要将其转换为Date类型(转换方法如下)

 1 package slowalker.struts.c4;
 2 
 3 import javax.servlet.http.HttpServlet;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import java.io.IOException;
 7 import java.text.SimpleDateFormat;
 8 import java.util.Date;
 9 import java.io.PrintWriter;
10 
11 public class RegisteServlet extends HttpServlet{
12     private static final long serialVersionUID = 1L;
13 
14     public void service(HttpServletRequest request, HttpServletResponse response) throws IOException{
15         //设置解码格式
16         request.setCharacterEncoding("utf-8");
17         
18         //获取参数
19         String name = request.getParameter("username");
20         String word = request.getParameter("password");
21         String uage = request.getParameter("age");
22         String ubirth = request.getParameter("birth");
23         
24         //类型转换
25         int age = Integer.parseInt(uage);
26         SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
27         Date birth = null;
28         try {
29             birth = sdf.parse(ubirth);
30         }catch(Exception e) {e.printStackTrace();}
31         
32         UserBean user = new UserBean(name, word, age, birth);
33         
34         //使用servlet直接输出
35         response.setContentType("text/html;charset=utf-8");
36         PrintWriter pw = response.getWriter();
37         pw.println("<html><head><title>data type</title></head><body>");
38         pw.println("用户姓名:" + user.getUsername());
39         pw.println("用户密码:" + user.getPassword());
40         pw.println("用户年龄:" + user.getAge());
41         pw.println("用户生日:" + user.getBirth());
42         pw.println("</body></html>");
43         pw.close();
44     }
45 }

  UserBean:

  四个属性String username, String password, int age, Date birth

  两个构造方法 : 一个无参, 一个UserBean(String username, String password, int age, Date birth)

  四个属性getter和setter

 

struts2内建的类型转换器

  Struts2内建了多种从字符串到其他数据类型的转换方法,

             可以完成int, long, char, float, double, boolean,Date,数组,集合和字符串之间的转换.(数组,集合是对于元素而言).

 1 package slowalker.struts.c4;
 2 
 3 
 4 import com.opensymphony.xwork2.ActionSupport;
 5 import java.util.Date;
 6 
 7 public class RegistAction extends ActionSupport {
 8 
 9     private static final long serialVersionUID = 1L;
10     
11     private String username;
12     private String password;
13     private int age;
14     private Date birth;
15     
16     //无参构造器
17     public RegistAction() {}
18     
19     public RegistAction(String username, String password, int age, Date birth) {
20         this.username = username;
21         this.password = password;
22         this.age = age;
23         this.birth = birth;
24     }
25 
26     //getter setter
27     public String getUsername() {
28         return username;
29     }
30 
31     public void setUsername(String username) {
32         this.username = username;
33     }
34 
35     public String getPassword() {
36         return password;
37     }
38 
39     public void setPassword(String password) {
40         this.password = password;
41     }
42 
43     public int getAge() {
44         return age;
45     }
46 
47     public void setAge(int age) {
48         this.age = age;
49     }
50 
51     public Date getBirth() {
52         return birth;
53     }
54 
55     public void setBirth(Date birth) {
56         this.birth = birth;
57     }
58 
59     
60     //未重写execute方法, 执行超类中的该方法
61 }

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 username:<s:property value="username" /><br/>
12 password:<s:property value="password" /><br/>
13 user&nbsp;age:<s:property value="age" /><br/>
14 birthday:<s:property value=".birth" /><br/>
15 </body>
16 </html>

 

 

基于OGNL的类型转换:  (其中插入很多输出, 可以从控制台输出信息得到运行流程)

 1 package slowalker.struts.c4;
 2 
 3 import java.util.Date;
 4 import com.opensymphony.xwork2.Action;
 5 
 6 public class RegistAction3 implements Action{
 7     
 8     private String tip;
 9     private UserBean user;
10     
11     public void setUser(UserBean user) {
12         System.out.println("setUser");
13         this.user = user;
14     }
15     
16     public UserBean getUser() {
17         System.out.println("getuser");
18         return user;
19     }
20 
21 
22     public String getTip() {
23         System.out.println("action tip属性获得");
24         return tip;
25     }
26 
27 
28     public void setTip(String tip) {
29         System.out.println("action tip属性设置");
30         this.tip = tip;
31     }
32 
33 
34     @Override
35     public String execute() throws Exception {
36         setTip("转换成功");
37         return SUCCESS;
38     }
39 
40 }

 

package slowalker.struts.c4;

import java.util.Date;

public class UserBean {
    private String username;
    private String password;
    private int age;
    private Date birth;
    
    
    public UserBean() {
        super();
        System.out.println("UserBean构造器无参");
    }

    public UserBean(String username, String password, int age, Date birth) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.birth = birth;
        System.out.println("UserBean构造器含参");
    }

    
    //getter / setter
    public String getUsername() {
        System.out.println("UserBean username属性获得");
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
        System.out.println("UserBean username属性设置");
    }
    public String getPassword() {
        System.out.println("UserBean password属性获得");
        return password;
    }
    public void setPassword(String password) {
        System.out.println("UserBean password属性设置");
        this.password = password;
    }
    public int getAge() {
        System.out.println("UserBean age属性获得");
        return age;
    }
    public void setAge(int age) {
        System.out.println("UserBean age属性设置");
        this.age = age;
    }
    public Date getBirth() {
        System.out.println("UserBean birth属性获得");
        return birth;
    }
    public void setBirth(Date birth) {
        System.out.println("UserBean birth属性设置");
        this.birth = birth;
    }
}

 

重点:!!!!! 表单中的属性都通过OGNL表达式表示,而输出视图也是通过该方式的

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8   <form action="Register2" method="post">
 9     <table>
10       <tr><td>username:</td><td><input type="text" name="user.username"></td></tr>
11       <tr><td>password:</td><td><input type="password" name="user.password"></td></tr>
12       <tr><td>age:</td><td><input type="text" name="user.age"></td></tr>
13       <tr><td>birthday:</td><td><input type="text" name="user.birth"></td></tr>
14       <tr><td></td><td><input type="submit" value="submit"></td></tr>
15     </table>
16   </form>
17 </body>
18 </html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
username:<s:property value="user.username" /><br/>
password:<s:property value="user.password" /><br/>
user&nbsp;age:<s:property value="user.age" /><br/>
birthday:<s:property value="user.birth" /><br/>
tips:<s:property value="tip"/>
</body>
</html>

 

关于处理流程

getuser
UserBean构造器无参
setUser
UserBean age属性设置
getuser
UserBean birth属性设置
getuser
UserBean password属性设置
getuser
UserBean username属性设置
action tip属性设置
getuser
UserBean username属性获得
getuser
UserBean password属性获得
getuser
UserBean age属性获得
getuser
UserBean birth属性获得
action tip属性获得

 

表单中包含四个请求参数, username,password,age,birth.

处理OGNL表达式如user.username时(在设置属性时):服务器根据user属性调用getUser()方法获得user,然后调用setUsername()方法设置username.

 

posted on 2017-12-19 14:30  slowalker-lee  阅读(126)  评论(0编辑  收藏  举报