模型驱动
struts2没有想struts1一样的"forms"。在struts2中请求参数直接跟action类的字段绑定。当action被执行的时候,
这个action类就会被安置在栈的最上面
如果一个action实现了接口com.opensymphony.xwork2.ModelDriven,那么他需要从getModel()方法返回一个object。
Struts将请求参数移植到这个object,然后这个object
将会被设置到栈的头部一旦action被执行。验证也会通过模型类来展现。
想要使用ModelDriven actions,请确保Model Driven Interceptor被应用在action中。这个拦截器是默认拦截器栈的一部分,
所以这个拦截器默认应用于所有action
例子:
Action class:
public class ModelDrivenAction implements ModelDriven {
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return new Gangster();
}
}
Gangster class (model):
public class Gangster implements Serializable {
private String name;
private int age;
private String description;
private boolean bustedBefore;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isBustedBefore() {
return bustedBefore;
}
public void setBustedBefore(boolean bustedBefore) {
this.bustedBefore = bustedBefore;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JSP for creating a Gangster:
<s:form action="modelDrivenResult" method="POST" namespace="/modelDriven">
<s:textfield label="Gangster Name" name="name" />
<s:textfield label="Gangster Age" name="age" />
<s:checkbox label="Gangster Busted Before" name="bustedBefore" />
<s:textarea cols="30" rows="5" label="Gangster Description" name="description" />
<s:submit />
struts2没有想struts1一样的"forms"。在struts2中请求参数直接跟action类的字段绑定。当action被执行的时候,
这个action类就会被安置在栈的最上面
如果一个action实现了接口com.opensymphony.xwork2.ModelDriven,那么他需要从getModel()方法返回一个object。
Struts将请求参数移植到这个object,然后这个object
将会被设置到栈的头部一旦action被执行。验证也会通过模型类来展现。
想要使用ModelDriven actions,请确保Model Driven Interceptor被应用在action中。这个拦截器是默认拦截器栈的一部分,
所以这个拦截器默认应用于所有action
例子:
Action class:
public class ModelDrivenAction implements ModelDriven {
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return new Gangster();
}
}
Gangster class (model):
public class Gangster implements Serializable {
private String name;
private int age;
private String description;
private boolean bustedBefore;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isBustedBefore() {
return bustedBefore;
}
public void setBustedBefore(boolean bustedBefore) {
this.bustedBefore = bustedBefore;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JSP for creating a Gangster:
<s:form action="modelDrivenResult" method="POST" namespace="/modelDriven">
<s:textfield label="Gangster Name" name="name" />
<s:textfield label="Gangster Age" name="age" />
<s:checkbox label="Gangster Busted Before" name="bustedBefore" />
<s:textarea cols="30" rows="5" label="Gangster Description" name="description" />
<s:submit />
</s:form>
翻译自http://struts.apache.org/docs/model-driven.html