本文學習內容來自傳智部落格struts教程。

1.屬性封裝

作用:
直接把表單提交屬性封裝到action的屬性裡面
步驟:
(1)在action中宣告成員變數,變數名稱和表單輸入項的名稱一樣
(2)生成變數的set方法。
缺點:
資料只是放在屬性中,並沒有放到物件中去。

(1)action類:

package data;

import com.opensymphony.xwork2.ActionSupport;

public class DataDemo1Action extends ActionSupport {


    private String username;
    private String password;
    private String address;

    public String execute() throws Exception {
        System.out.println("username:"+username+"password:"+password+"address:"+address);
        return NONE;
    }

    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 getAddress() {
        return address;
    }

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

}

(2)struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="demo1" extends="struts-default" namespace="/">
        <action name="book" class="action.BookAction">
            <!-- chain: 轉發 ,頁面會發生改變,網址不發生改變,仍然為 http://127.0.0.1:8080/strutslearn2All/book -->
            <result name="success" type="chain">orders</result>
        </action>

        <action name="orders" class="action.OrdersAction">
            <result name="success">/HelloOrder.jsp</result>
        </action>
    </package>


    <package name="demo2" extends="struts-default" namespace="/">
        <action name="data1" class="data.DataDemo1Action"></action>
    </package>

</struts>

(3)data1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <form action="${pageContext.request.contextPath}/data1.action" method="post">
      username:<input type="text" name="username"/>
      <br/>
      password:<input type="text" name="password"/>
      <br/>
      address:<input type="text" name="address"/>
      <br/>
      <input type="submit" value="submit"/>
    </form>
</body>
</html>

2.模型驅動封裝

作用:
使用模型驅動封裝,可以直接把表單屬性封裝到實體類物件裡面
實現步驟:
(1)action實現介面ModelDriven
(2)實現接口裡面的getModel()方法
把建立物件返回
(3)在action裡面建立實體類物件
要求:
表單輸入項的name屬性值和實體類屬性名稱一樣。

(1)action類:

  package data;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import entity.User;

public class DataDemo2Action extends ActionSupport implements ModelDriven<User> {

    //建立物件
    private User user = new User();


    public String execute() throws Exception {

        System.out.println(user);
        return NONE;
    }

    @Override
    public User getModel() {
        return user;
    }

}

(2)struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="demo1" extends="struts-default" namespace="/">
        <action name="book" class="action.BookAction">
            <!-- chain: 轉發 ,頁面會發生改變,網址不發生改變,仍然為 http://127.0.0.1:8080/strutslearn2All/book -->
            <result name="success" type="chain">orders</result>
        </action>

        <action name="orders" class="action.OrdersAction">
            <result name="success">/HelloOrder.jsp</result>
        </action>
    </package>


    <package name="demo2" extends="struts-default" namespace="/">
        <action name="data2" class="data.DataDemo2Action"></action>
    </package>
</struts>

(3)data2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
     <form action="${pageContext.request.contextPath}/data2.action" method="post">
      username:<input type="text" name="username"/>
      <br/>
      password:<input type="text" name="password"/>
      <br/>
      address:<input type="text" name="address"/>
      <br/>
      <input type="submit" value="submit"/>
    </form>
</body>
</html>

(4)實體類:

package entity;

public class User {

    private String username;

    private String password;

    private String address;

    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 getAddress() {
        return address;
    }

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

3.表示式封裝

作用
使用表示式封裝可以將表單資料封裝到實體類裡面
步驟
1.在action中宣告實體類
2.生成實體類變數的set方法
3.在表單輸入項的name屬性值裡面寫表示式形式。

(1)action類

package data;

import com.opensymphony.xwork2.ActionSupport;

import entity.Book;
import entity.User;

public class DataDemo3Action extends ActionSupport {

private User user;
    private Book book;



public Book getBook() {
return book;
}

public void setBook(Book book) {
this.book = book;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@Override
public String execute() throws Exception { 
System.out.println(user);
System.out.println(book);
return NONE;
}
}

(2)book實體類

package entity;

public class Book {

    private String bookname;

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
}

(3)dat3.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form action="${pageContext.request.contextPath}/data3.action" method="post">
        username:<input type="text" name="user.username" />
        <br/> 
        password:<input type="text" name="user.password" /> 
        <br/> 
        address:<input type="text" name="user.address" /> 
        <br/> 
        bookname:<input type="text" name="book.bookname"/>
        <br/>
        <input type="submit" value="submit" />
    </form>

</body>
</html>

(4)struts檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="demo1" extends="struts-default" namespace="/">
        <action name="book" class="action.BookAction">
            <!-- chain: 轉發 ,頁面會發生改變,網址不發生改變,仍然為 http://127.0.0.1:8080/strutslearn2All/book -->
            <result name="success" type="chain">orders</result>
        </action>

        <action name="orders" class="action.OrdersAction">
            <result name="success">/HelloOrder.jsp</result>
        </action>
    </package>


    <package name="demo2" extends="struts-default" namespace="/">
        <action name="data3" class="data.DataDemo3Action"></action>
    </package>
</struts>

4.程式碼總體架構

這裡寫圖片描述

4.使用以上封裝要注意的問題

(1)同一個action中,不能同時使用屬性封裝和模型驅動封裝獲取同一個表單資料。如果同時使用,則框架只會使用模型驅動。

(2)模型驅動與表示式封裝的不同:
1.使用模型驅動只能把資料封裝到一個實體類物件裡面。
在一個action裡面不能把模型驅動封裝到不同的實體類物件裡面。
2.表示式封裝可以把資料封裝到不同的物件中去。

.