The struts 2 framework is a web framework, and provides the function of data subpackage
Data encapsulation property driven encapsulation
set method provided by property driven encapsulation (not commonly used)
Let's take an example to explain:
Let's start with a simple user form submission page
Key jsp code:
<form action="${pageContext.request.contextPath}/userAction.action" method="post"> name: <input type="text" name="firstname"><br> password: <input type="password" name="lastname"><br> age: <input type="text" name="age"><br> birthday:<input type="text" name="birthday"><br> salary:<input type="text" name="salary"><br> <input type="submit" value="Submission"> </form>
Write Action main code
//We use the second way to inherit public Class UserAction extends ActionSupport { private String username; private String password; private Integer age; private Date birthday; private Double salary; //Their set method .... public String execute() throws Exception{ //Encapsulated data User user = new User();// Database User table and User class need to be established -- hbt is not the focus of this time user.setUsername(username); user.setPassword(password); user.setAge(age); user.setBirthday(birthday); user.Salary(salary); return NONE;
How to submit expression in property driven page
Key jsp code
<form action="${pageContext.request.contextPath}/userAction.action" method="post"> name: <input type="text" name="user.firstname"><br> password: <input type="password" name="user.lastname"><br> age: <input type="text" name="user.age"><br> birthday:<input type="text" name="user.birthday"><br> salary:<input type="text" name="user.salary"><br> <input type="submit" value="Submission"> </form>
Action key code
public class UserAction extends ActionSupport{ private User user;//1. Provide a user object //2. Providing the set and get method interceptors will create this object and encapsulate it in your, so you must give the set method ...
Model driven (most commonly used)
Key jsp
<form action="${pageContext.request.contextPath}/userAction.action" method="post"> name: <input type="text" name="firstname"><br> password: <input type="password" name="lastname"><br> age: <input type="text" name="age"><br> birthday:<input type="text" name="birthday"><br> salary:<input type="text" name="salary"><br> <input type="submit" value="Submission"> </form>
Writing Action
public class UserAction extends ActionSuppert implements ModelDriven<User>{ //Create objects manually private User user = new User(); //2. Provide getModel method public User getModel(){ return user; } //It's self-contained }
Disadvantage: only one object can be encapsulated at the same time
But the second one can encapsulate multiple objects (supplementary)