SoFunction
Updated on 2025-03-01

Using username registration as an example to analyze three ways to obtain data

1. Inject properties

Inject properties directly:

public String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
 = userName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub

User user = new User();
(userName);// Here you need to receive and use username.UserDAO dao = new UserDAO();
HttpServletResponse response= ();
PrintWriter out = ();
if((user))
{
("");
}
else
{
("");
}
return null;
}

Model This is a common method

I won't repeat the explanation here.

3.ModelDriven

The third method is not commonly used, you only need to understand;

The process is divided into 4 steps:
(1) Action implements ModelDriven<User> interface

(2) Add abstract methods

(3) Define and initialize a model

User user=new User();

(4) Generate setters and getters

public class CheckUserAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
 = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
UserDAO dao = new UserDAO();
HttpServletResponse response= ();
PrintWriter out = ();
if((user))
{
("");
}
else
{
("");
}
return null;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}

Note: When using methods 1 and 3, there is no need to change the front-end and jsp code parts, because it is the userName attribute that is called directly.

Method 2 requires changing the username in jquery to,.