Xử lý Dữ liệu Form trong Struts2: Ba Phương Pháp Cơ Bản

3. Ba phương pháp nhận dữ liệu form trong Struts2

Trong Struts2, có ba cách chính để xử lý dữ liệu từ form gửi về:

1. Thuộc tính cơ bản

Khai báo trực tiếp các trường trong action với tên tương ứng với thuộc tính form

public class Publication {
    private String title;    // Tên sách
    private String author;   // Tác giả
    // getter, setter
}

public class Account {
    private String accountName; // Tên tài khoản
    private String password;    // Mật khẩu
    private List<Publication> favorites; // Danh sách sách yêu thích
    // getter, setter
}

Form mẫu:

<form action="handleSimple.action" method="post">
    <div>
        Tên tài khoản: <input type="text" name="accountName">
    </div>
    <div>
        <input type="submit" value="Đăng nhập" />
    </div>
</form>

Action xử lý:

public class SimpleHandlerAction {
    private String accountName;
    
    public String login() {
        System.out.println("Tên tài khoản: " + accountName);
        return "success";
    }
}

2. Đối tượng miền (Domain Object)

Khởi tạo đối tượng chứa các thuộc tính và sử dụng định danh với dấu chấm

<form action="handleDomain.action" method="post">
    <div>
        Tên tài khoản: <input type="text" name="account.accountName">
    </div>
    <div>
        <input type="submit" value="Đăng nhập" />
    </div>
</form>

Action xử lý:

public class DomainHandlerAction {
    private Account account;
    
    public String login() {
        System.out.println("Tên tài khoản: " + account.getAccountName());
        return "success";
    }
}

3. Model-Driven

Thực thi interface ModelDriven để tự động gán dữ liệu

<form action="handleModel.action" method="post">
    <div>
        Tên tài khoản: <input type="text" name="accountName">
    </div>
    <div>
        Sách yêu thích 1: <input type="text" name="favorites[0].title">
    </div>
    <div>
        Sách yêu thích 2: <input type="text" name="favorites[1].title">
    </div>
    <div>
        <input type="submit" value="Đăng nhập" />
    </div>
</form>

Action xử lý:

public class ModelDrivenHandlerAction implements ModelDriven<Account> {
    private Account account = new Account();
    
    @Override
    public Account getModel() {
        return account;
    }
    
    public String login() {
        System.out.println("Tài khoản: " + account.getAccountName());
        System.out.println("Sách yêu thích: " + account.getFavorites().get(0).getTitle());
        return "success";
    }
}

4. Xác thực form với Struts2

Định nghĩa phương thức validate() để kiểm tra dữ liệu:

@Override
public void validate() {
    if (account.getAccountName() == null || account.getAccountName().isEmpty()) {
        addFieldError("accountName", "Tên tài khoản không được để trống");
    }
    if (account.getPassword() == null || account.getPassword().length() < 6) {
        addFieldError("password", "Mật khẩu phải có ít nhất 6 ký tự");
    }
}

Trong struts.xml:

<result name="input">/login.jsp</result>

Trong JSP sử dụng thẻ Struts2:

<s:fielderror name="accountName" />
<s:fielderror name="password" />

Thẻ: struts2 form-handling model-driven validation action-mapping

Đăng vào ngày 26 tháng 6 lúc 13:23