Struts 2 does not have "forms" like Struts 1 did. In Struts 2 request parameters are bound directly to fields in the actions class, and this class is placed on top of the stack when the action is executed.If an action class implements the interface com.opensymphony.xwork2.ModelDriven then it needs to return an object from the getModel() method. Struts will then populate the fields of this object with the request parameters, and this object will be placed on top of the stack once the action is executed. Validation will also be performed on this model object, instead of the action. public class ModelDrivenAction implements ModelDriven { public String execute() throws Exception { return SUCCESS; } public Object getModel() { return new Gangster(); } } public class Gangster implements Serializable { private String name; private int age; private String description; private boolean bustedBefore;.......<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>红色的实体是和你数据库对应的。页面是和红色实体对应的。这样不就对应起来了吗?参看struts-xx-all/struts-xx/docs/WW/model-driven.html说明。
Struts 2 does not have "forms" like Struts 1 did. In Struts 2 request parameters are bound directly to fields in the actions class, and this class is placed on top of the stack when the action is executed哪个是你下的struts2的zip包内的目录。
public class ModelDrivenAction implements ModelDriven {
public String execute() throws Exception {
return SUCCESS;
} public Object getModel() {
return new Gangster();
}
}
public class Gangster implements Serializable {
private String name;
private int age;
private String description;
private boolean bustedBefore;.......<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>红色的实体是和你数据库对应的。页面是和红色实体对应的。这样不就对应起来了吗?参看struts-xx-all/struts-xx/docs/WW/model-driven.html说明。
是不是jsp中只能提交一个表单(如果不用ajxa什么的话),那ModelDriven默认就和唯一的一个表单匹配上了?然后实体中的属性是根据form表单中的属性名字匹配的吗?