1. 程式人生 > >Struts2獲取表單資料並封裝到實體類的方法

Struts2獲取表單資料並封裝到實體類的方法

原連結

1.採用ActionContext物件的方式

複製程式碼
public String execute() throws Exception {
        // 獲取ActionContext物件
        ActionContext context = ActionContext.getContext();
        // 獲取引數Map物件
        Map<String, Object> map = context.getParameters();
        // 遍歷Map集合
        Set<String> set = map.keySet();
        
for (String key : set) { // 表單value值是Object陣列,因為存在表單中多值的情況 Object[] values = (Object[]) map.get(key); System.out.println(Arrays.toString(values)); } return NONE; }
複製程式碼

2.採用ServletActionContext物件的方式

複製程式碼
public String execute() throws Exception {
        // 通過ServletActionContext物件得到HttpServletRequest物件
HttpServletRequest request = ServletActionContext.getRequest(); // 取值 String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); // 建立User物件並複製進去 return
NONE;
複製程式碼

3.採用介面注入的方式

複製程式碼
public class TestAction3 extends ActionSupport implements ServletRequestAware {
    private HttpServletRequest request = null;
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request = request;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        System.out.println(username + ":" + password + ":" + address);
        return NONE;
    }
複製程式碼

4.採用屬性封裝

   表單資料

 <form action="${pageContext.request.contextPath }/form.action" method="post">
        使用者名稱:<input type="text" name="username"/><br/>
        密 碼:<input type="password" name="password"/><br/>
        地 址:<input type="text" name="address"/><br/>
        <input type="submit" value="提交"/>
    </form>

  宣告表單屬性,並生成set方法

複製程式碼
   private String username;
    private String password;
    private String address;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String execute() throws Exception {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAddress(address);
        return NONE;
    }
複製程式碼

5.模型驅動封裝(重點)

複製程式碼
public class DemoAction1 extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    public User getUser() {
        return user;
    }

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }

    public String execute() throws Exception {
        System.out.println(user);
        return NONE;
    }
}
複製程式碼

6.表示式封裝

  表單資料,注意name屬性值的寫法,實體物件。屬性名

 <form action="${pageContext.request.contextPath }/form.action" method="post">
        使用者名稱:<input type="text" name="user.username"/><br/>
        密 碼:<input type="password" name="user.password"/><br/>
        地 址:<input type="text" name="user.address"/><br/>
        <input type="submit" value="提交"/>
    </form>

  程式碼,注意生成實體類的set和get方法

複製程式碼
    private User 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;
    }
複製程式碼

 7.List集合封裝

      表單資料    [0]為list下標值

複製程式碼
<form action="${pageContext.request.contextPath }/form5" method="post">
          使用者名稱:<input type="text" name="list[0].username"/><br/>
          密 碼:<input type="password" name="list[0].password"/><br/>
          年 齡:<input type="text" name="list[0].age"/><br/>
          地 址:<input type="text" name="list[0].address"/><br/>
          <input type="submit" value="提交"/>
      </form>
複製程式碼

  程式碼

複製程式碼
    private List<User> list;

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println(list);
        return NONE;
    }
複製程式碼

8.Map集合封裝

  頁面   'one'為鍵

複製程式碼
    <form action="${pageContext.request.contextPath }/form6" method="post">
          使用者名稱:<input type="text" name="map['one'].username"/><br/>
          密 碼:<input type="password" name="map['one'].password"/><br/>
          年 齡:<input type="text" name="map['one'].age"/><br/>
          地 址:<input type="text" name="map['one'].address"/><br/>
          <input type="submit" value="提交"/>
      </form>
複製程式碼

  程式碼

複製程式碼
    private Map<String, User> map;
    
    public Map<String, User> getMap() {
        return map;
    }

    public void setMap(Map<String, User> map) {
        this.map = map;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println(map);
        return NONE;
    }
複製程式碼