Model Driven

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.

Interceptor

To use ModelDriven actions, make sure that the Model Driven Interceptor is applied to your action. This interceptor is part of the default interceptor stack defaultStack so it is applied to all actions by default.

Example

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 >

Generated by CXF SiteExporter


摘自  http://struts.apache.org/release/2.3.x/docs/model-driven.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值