1. 程式人生 > >十一 三種Struts2的數據封裝方式

十一 三種Struts2的數據封裝方式

spa action get set方法 方法 vat 封裝 htm pat

Struts2的數據封裝:Struts2是一個web層框架,框架是軟件的半成品。提供了數據封裝的基本功能。

屬性驅動:提供屬性set方法的方式

這種方式很少用,除非數據少,需要在Action類裏自己往對象裏封裝

前端jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>Struts2的數據封裝</h1> 11 <h3>方式一:屬性驅動:提供Set方法的方式</h3> 12 <form action="${pageContext.request.contextPath }/userAction1.action"
method="post"> 13 用戶名:<input type="text" name="username"><br/> 14 密碼:<input type="password" name="password"><br/> 15 年齡:<input type="text" name="age"><br/> 16 生日:<input type="text" name="birthday"><br/> 17 工資:<input type="text" name="salary"><
br/> 18 <input type="submit" value="提交"> 19 </form> 20 </body> 21 </html>

實體類User:

package com.itheima.struts2.domain;
/**
 * 實體對象
 */
import java.util.Date;

public class User {
   private String username;
   private String password;
   private int age;
   private Date birthday;
   private Double salary;

public void setUsername(String username) {
    this.username = username;
}
public void setPassword(String password) {
    this.password = password;
}
public void setAge(int age) {
    this.age = age;
}
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
public void setSalary(Double salary) {
    this.salary = salary;
}
   
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday
+ ", salary=" + salary + "]";
}
}

Action類:

 1 package com.itheima.struts2.demo2;
 2 
 3 import java.util.Date;
 4 
 5 import com.itheima.struts2.domain.User;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 /**
 8  * 數據封裝的方式一:屬性封裝
 9  *
10  */
11 public class UserAction1 extends ActionSupport {
12    private String username;
13    private String password;
14    private Integer age;
15    private Date birthday;
16    private Double salary;
17    
18     
19 public void setUsername(String username) {
20     this.username = username;
21 }
22 
23 
24 public void setPassword(String password) {
25     this.password = password;
26 }
27 
28 
29 public void setAge(Integer age) {
30     this.age = age;
31 }
32 
33 
34 public void setBirthday(Date birthday) {
35     this.birthday = birthday;
36 }
37 
38 
39 public void setSalary(Double salary) {
40     this.salary = salary;
41 }
42 
43 
44     public String execute() throws Exception{
45        //接收數據
46         System.out.println(username);
47         System.out.println(password);
48         System.out.println(age);
49         System.out.println(birthday);
50         System.out.println(salary);
51        //封裝數據
52         User user = new User();
53         user.setAge(age);
54         user.setUsername(username);
55         user.setBirthday(birthday);
56         user.setSalary(salary);
57         
58        return NONE;
59    }
60 }

屬性驅動:提供頁面表達式的方式

前端JSP:name屬性前加上實體類的對象前綴,如:user.password

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <h1>Struts2的數據封裝</h1>
11 <h3>方式二:屬性驅動:提供頁面表達式方式</h3>
12 <form action="${pageContext.request.contextPath }/userAction2.action" method="post">
13 用戶名:<input type="text" name="user.username"><br/>
14 密碼:<input type="password" name="user.password"><br/>
15 年齡:<input type="text" name="user.age"><br/>
16 生日:<input type="text" name="user.birthday"><br/>
17 工資:<input type="text" name="user.salary"><br/>
18 <input type="submit" value="提交">
19 </form>
20 </body>
21 </html>

Action類:相較而言,這種直接封裝對象,而不是操作屬性的方式,比較簡約

package com.itheima.struts2.demo2;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 數據封裝方式二:屬性驅動
 * @author 李騰
 *
 */
public class UserAction2 extends ActionSupport {
    //提供一個User對象,在前端頁面的name屬性上加上user前綴
    private User user;
    //提供user的get和set方法,一定要提供get方法
    //因為攔截器提供對象的封裝,需要創建User對象,前提是前端屬性名字的前綴和類一樣
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    
    public String execute() throws Exception{
        System.out.println(user);
        return NONE;
    }

}

模型驅動:采用模型驅動的方式(最常用)

Action類實現ModelDriven接口,重寫getModel方法,返回實體對象

缺點:只能想一個對象(比如user)中封裝對象。而頁面表達式可以向多個對象封裝(只需改變name屬性的前綴如student.username user.username )

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <h1>Struts2的數據封裝</h1>
11 <h3>方式三:模型驅動方式</h3>
12 <form action="${pageContext.request.contextPath }/userAction3.action" method="post">
13 用戶名:<input type="text" name="username"><br/>
14 密碼:<input type="password" name="password"><br/>
15 年齡:<input type="text" name="age"><br/>
16 生日:<input type="text" name="birthday"><br/>
17 工資:<input type="text" name="salary"><br/>
18 <input type="submit" value="提交">
19 </form>
20 </body>
21 </html>

Action類:

package com.itheima.struts2.demo2;

import com.itheima.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 數據封裝方式三:模型驅動  最常用的方式,缺點:只能同時向一個對象中封裝數據
 *          二:向多個數據封裝中同時封裝數據
 * @author 李騰
 *
 */
public class UserAction3 extends ActionSupport  implements ModelDriven<User>{
    //前提:手動實例化User
    private User user = new User();
    
    //模型驅動需要使用的方法
    @Override
    public User getModel() {
        return user;
    }
    
    public String execute() throws Exception{
        System.out.println(user);
        return NONE;
    }



}

十一 三種Struts2的數據封裝方式