Pages

Tuesday, December 2, 2008

Object backed-components – Struts 2

The automatic data transfer of Struts 2 needs only to provide
JavaBeans properties on actions, using the same names as the form
fields being submitted. Object backed – components of Struts 2 allows us to transfer data as objects. Suppose you want
to create a new user.
We have User class as fallows,
public class User{
private String name;
private String email;
// getter and setter methods
}
If you use Struts 2 default data transferring process, you must give the same field names as input JSP’s field’s name, must write JavaBeans properties with the same name in you action class and you have to create User object by yourself to create a new user. If you use, object – backed components of Struts 2 for data transferring, We don’t even have to create the User object that backs the property because the framework’s data transfer will handle this for us when it starts trying to move the data over. You can have more cleaner action class as fallows.
public String execute(){
createUser(user);
return SUCCESS;
}
private User user;
public void setUser(User user){
this.user = user;
}
public User getUser(){
return this.user;
}
You also need to do slight changes in your input JSP’s and the resulting JSP’s when addressing appropriate fields. For example, in the input JSP for creating a new user,
Similarly, when rendering the result,
Welcome <s:property value="user.name" />
The minor consequences are that we have to go a dot deeper when we reference our data from the JSP pages.



0 comments:

Post a Comment

Share

Widgets