input.jsp中可以直接使用User对象的属性: <body> <s:form action="AddUserModelAction"> <s:textfield name="userName" label="Your name" /> <s:textfield name="address.address" label="Your address" /> <s:textfield name="address.zipCode" label="Your zipCode" /> <s:textfield name="address.phone" label="Your phone" /> <s:submit/> </s:form> </body> success.jsp中可以直接访问User对象的属性: <body> <h4>User: <s:property value="userName" /> add success(by Model).</h4> <h4>Address:<s:property value="address.address" /></h4> <h4>zipCode:<s:property value="address.zipCode" /></h4> <h4>phone :<s:property value="address.phone" /></h4> </body>
import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.jaeson.hibernatestudy.bean.User; import com.jaeson.springstudy.service.CommonService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("all") @Scope("prototype") @Controller("userModelAction") public class UserModelAction extends ActionSupport implements ModelDriven<User> { /** * 在执行execute时,ValueStack已经获得模型对象的引用,在整个请求过程中框架一直使用该引用。 * 如果在execute内部改变了模型字段,框架不会察觉,这可能会导致数据不一致的问题。 */ @Override public String execute() { this.getCommonService().saveOrUpdate(this.user); //throw new RuntimeException("throw in UserAction"); return "success"; } @Override public void validate() { if (this.user.getUserName().length() == 0) { this.addFieldError("userName", "userName is required."); } } private User user = new User(); @Resource(name = "commonService") private CommonService commonService; @Override public User getModel() { return this.user; } public CommonService getCommonService() { return this.commonService; } public void setCommonService(CommonService commonService) { this.commonService = commonService; } }
import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.jaeson.hibernatestudy.bean.User; import com.jaeson.springstudy.service.CommonService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("all") @Scope("prototype") @Controller("userModelAction") public class UserModelAction extends ActionSupport implements ModelDriven<User> { /** * 在执行execute时,ValueStack已经获得模型对象的引用,在整个请求过程中框架一直使用该引用。 * 如果在execute内部改变了模型字段,框架不会察觉,这可能会导致数据不一致的问题。 */ @Override public String execute() { this.getCommonService().saveOrUpdate(this.user); //throw new RuntimeException("throw in UserAction"); return "success"; } @Override public void validate() { if (this.user.getUserName().length() == 0) { this.addFieldError("userName", "userName is required."); } } private User user = new User(); @Resource(name = "commonService") private CommonService commonService; @Override public User getModel() { return this.user; } public CommonService getCommonService() { return this.commonService; } public void setCommonService(CommonService commonService) { this.commonService = commonService; } }