How to use ModelDriven in Struts 2

Talking about Struts 2 again
. I wanna talk (or write) about this because is very interesting, for me it remembers me the way of work on Struts 1. For those that doesn't know what am i talking about, i'm going to explain you. In Struts 1, the way of work with Action and form properties is a little bit different of default Struts 2 way. In struts 1 we have the actions that doesn't contain any form property or getter/setter method, like this:
import....
public class LoginAction extends LookupDispatchAction {
public ActionForward logMeIn(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping,
ActionForm form) {
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUsername().equals("admin") &&
loginForm.getPassword().equals("admin")) {
return mapping.findForward("enter");
} else {
return mapping.findForward("fail");
}
}
}
And, in other class, the Action Form where we can find the properties with respective getter/setter methods and optionally, struts 1 server side validation:
But, in Struts 2 it changes a little bit because properties, getter/setter methods and validation are in the action. For the previous example, it's version on Struts 2 will look like this:
public class LoginAction {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public String logMeIn() {
if (getUsername().equals("admin") &&
getPassword().equals("admin")) {
return "enter";
} else {
return "fail";
}
}
Free from extends and strange return types in methods but more charged with properties and getter/setter methods, for some people it's better, for another people it sucks. In my case i like the fact that i don´t need to extend my actions from some classes (altought you have to do that if you want to inject additional funcionality to your action like validation, prepare scripts and so on) and a simpler way to declare action methods, but i dislike the fact that all is inside the action.
But don't worry people, Struts 1 developer that migrated to Struts 2 can still use the same model Action - Action Form like Struts 1 in struts 2
, how to do that?
First, your action must implement ModelDriven interface and your interceptor stack must include ModelDrivenInterceptor, that is already included in default stack. This will allow to Struts 2 engine to fill a pojo with the data of a form or something like that instead fill the fields of the action. Following with the same example, our Struts 2 action will look like this:
public class LoginAction {
private User user;
public Object getModel() {
return user;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String logMeIn() {
if (user.getUsername().equals("admin") &&
user.getPassword().equals("admin")) {
return "enter";
} else {
return "fail";
}
}
Now our action is free of anoying properties and those are in a separate class named User.java:
public class User {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
In the front-end side, you don't have to alter any. What i'm talking about? For example, a textfield will look like this:
<s:textfield name="username" id="username" />
And that's it!! Enjoy it.







