首页 > 代码库 > struts2封装客户端请求数据(3种封装方式)

struts2封装客户端请求数据(3种封装方式)

长话短说,直接进入主题.

1.属性驱动

  • action的属性名称必须和jsp输入项的name属性保持一致;
  • 必须要在action类中提供该属性的set方法,但有时候会出错,为了保险起见,我们把get/set方法都写上;
package com.tbamaw.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class User2Action extends ActionSupport{

    private static final long serialVersionUID = 7248487833146321912L;
    
    private String username;  //属性名称必须和jsp里面的input中的name值相同
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String login() {
        
        System.out.println(username+"==="+password);
        return NONE;
        
    }
}
 <form method="post" action="${pageContext.request.contextPath }/user_login.action" >
    <table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="password"></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="提交"></td>
    </tr>
    </table>
  </form>

 技术分享

 

2.模型驱动

  • 使用模型驱动,必须在action类中实现ModelDriven<T> ;
  • 使用模型驱动时,必须要提供一个实体类来对页面的属性进行封装;
  • 如果使用了模型驱动封装,那么属性封装和表达式封装将不能使用;
  • 声明的实体必须new成一个对象使用,否则会产生空指针异常;
  • 实现模型驱动后的类要重写getModel();方法的返回值实体类对象;
  • jsp页面的输入项的值name属性必须和实体类中的属性名称一致;
 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>登录</title>
 8 </head>
 9 <body>
10  <form method="post" action="${pageContext.request.contextPath }/user_login.action" >
11     <table>
12     <tr>
13         <td>用户名:</td>
14         <td><input type="text" name="username"></td>
15     </tr>
16     <tr>
17         <td>密码</td>
18         <td><input type="password" name="password"></td>
19     </tr>
20     <tr>
21         <td colspan="2"><input type="submit" value="提交"></td>
22     </tr>
23     </table>
24   </form>
25 </body>
26 </html>
 1 package com.tbamaw.web.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.opensymphony.xwork2.ModelDriven;
 5 import com.tbamaw.domain.User;
 6 /**
 7  * 模型驱动方式
 8  * @author Administrator
 9  */
10 public class UserAction extends ActionSupport implements ModelDriven<User>{
11 
12     private static final long serialVersionUID = -5185250590002027078L;
13     private User user = new User(); //必须new对象
14     public User getModel() {
15         return user;
16     }
17     public String login() {
18         System.out.println(user.getUsername()+"--"+user.getPassword());
19         return NONE;
20     }
21 }

 实体类

 1 public class User {
 2     private int uid;
 3     private String username;
 4     private String password;
 5     public int getUid() {
 6         return uid;
 7     }
 8     public void setUid(int uid) {
 9         this.uid = uid;
10     }
11     public String getUsername() {
12         return username;
13     }
14     public void setUsername(String username) {
15         this.username = username;
16     }
17     public String getPassword() {
18         return password;
19     }
20     public void setPassword(String password) {
21         this.password = password;
22     }
23 }

技术分享

3.表达式封装

  • 和属性封装大部分的特点相同(在jsp页面的name属性中不同);
  • 页面当中要加上action当中的属性名称,实体类的属性名称;
  • 和模型驱动不同的是,他可以封装多个对象
public class User3Action extends ActionSupport{

    private static final long serialVersionUID = -2278224171908902139L;
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    public String login() {
        System.out.println(user.getUsername()+"=="+user.getPassword());
        return NONE;
        
    }
}
<form method="post" action="${pageContext.request.contextPath }/user_login.action" >
    <table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="user.username"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="user.password"></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="提交"></td>
    </tr>
    </table>
  </form>

 技术分享

 

struts2封装客户端请求数据(3种封装方式)