Reputation: 2242
I am new to Struts2 and I just realized that whenever I call an action class via a form of a JSP page, I need to have getters and setters for all the parameters in the called action class to access the parameters as shown in below action class:
public class LoginAction extends ActionSupport {
private String userName;
private String password;
public String execute {
System.out.println(this.userName+" "+this.password);
return "success";
}
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;
}
}
However, I also have a POJO class for User which has the same 2 attributes and its getters and setters. Is there a way I can use those getters/setters within my action class? Right now I have getters and setters in both my POJO and my action class. Can anyone help how to eliminate this redundancy?
Upvotes: 0
Views: 1500
Reputation: 1
The action bean are put on the top of the value stack, the parameters are accessed directly by name, i.e. userName
, password
. Struts2 uses OGNL to access objects in the value stack, so if you place your POJO to the value stack it will be accessible via OGNL. For example
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
it should use parameter names user.userName
and user.password
.
Upvotes: 2