Struts 2 uses the library xWork 2 to perform a type conversion and validation between the template and the action classes. Thus, all errors of conventional conversions (String -> Long / Integer / Float / Boolean ...) are automatically handled by this library. In addition, specific types of conversions can be created by implementing the abstract class "StrutsTypeConverter," facilitating the settlement of a bean from a form. To emphasize the simplicity, xWork 2 provides an interface "ModelDriven" that can be implemented by action in providing the type of object to map. The supplied object is directly accessible in the view using the language OGNL (Object Graph Navigation Language). A concrete example of a form to register a person:
Definition of the subject Person:
Code:
public class Personne {
private String nom;
private String prenom;
//getters/setters
...
}
Definition of action:
Code:
public class PersonneAction extends ActionSupport implements ModelDriven<Personne> {
public String execute(){
return SUCCESS;
}
}
Definition of the JSP:
Code:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:textfield name="model.nom" label="nom" />
<s:textfield name="model.prenom" label="prenom" />
<s:submit key="button.submit" value="Enregistrer" />
Within the JSP, the attributes of the object "person" are directly passed as parameters through the model. With the OGNL language, "model.nom" represents both the getter and setter of the attribute name. In this way, the step of populating the object is achieved seamlessly without having to go through an "ActionForm" dear to Struts 1.
Bookmarks