struts2的三种方式接受请求参数
ModelDriven
为什么需要ModelDriven
所谓ModelDriven ,意思是直接把实体类当成页面数据的收集对象。比如,有实体类User 如下:
package cn.com.leadfar.struts2.actions; public class User { private int id ; private String username ; private String password ; private int age ; private String address ; public String getUsername() { return username ; } public void setUsername(String username) { this . username = username; } public String getPassword() { return password ; } public void setPassword(String password) { this . password = password; } public int getAge() { return age ; } public void setAge( int age) { this . age = age; } public String getAddress() { return address ; } public void setAddress(String address) { this . address = address; } public int getId() { return id ; } public void setId( int id) { this . id = id; } }
UserAction: public class UserAction { private int id ; private String username ; private String password ; private int age ; private String address ; public String add(){ User user = new User(); user.setId( id ); user.setUsername( username ); user.setPassword( password ); user.setAge( age ); user.setAddress( address ); new UserManager().addUser(user); return "success" ; } public int getId() { return id ; } public void setId( int id) { this . id = id; } public String getUsername() { return username ; } public void setUsername(String username) { this . username = username; } public String getPassword() { return password ; } public void setPassword(String password) { this . password = password; } public int getAge() { return age ; } public void setAge( int age) { this . age = age; } public String getAddress() { return address ; } public void setAddress(String address) { this . address = address; } }
add_input.jsp: < form action = "test/user.action" method = "post" > < input type = "hidden" name = "method:add" > username: < input type = "text" name = "username" > < br /> password: < input type = "text" name = "password" > < br /> age: < input type = "text" name = "age" > < br /> address: < input type = "text" name = "address" > < br /> < input type = "submit" name = "submit" value = " 添加用户 " > </ form > < br />
UserAction: public class UserAction { private User user ; public String add(){ new UserManager().addUser( user ); return "success" ; } public User getUser() { return user ; } public void setUser(User user) { this . user = user; } }
add_input.jsp: < form action = "test/user.action" method = "post" > < input type = "hidden" name = "method:add" > username: < input type = "text" name = "user.username" > < br /> password: < input type = "text" name = "user.password" > < br /> age: < input type = "text" name = "user.age" > < br /> address: < input type = "text" name = "user.address" > < br /> < input type = "submit" name = "submit" value = " 添加用户 " > </ form > < br />
public class UserAction implements ModelDriven{ private User user ; @Override public Object getModel() { if ( user == null ){ user = new User(); } return user ; } public String add(){ new UserManager().addUser( user ); return "success" ; } public User getUser() { return user ; } public void setUser(User user) { this . user = user; } }
< form action = "test/user.action" method = "post" > < input type = "hidden" name = "method:add" > username: < input type = "text" name = "username" > < br /> password: < input type = "text" name = "password" > < br /> age: < input type = "text" name = "age" > < br /> < input type = "submit" name = "submit" value = " 添加用户 " > </ form > < br />
public class ModelDrivenInterceptor extends AbstractInterceptor { protected boolean refreshModelBeforeResult = false ; public void setRefreshModelBeforeResult( boolean val) { this . refreshModelBeforeResult = val; } @Override public String intercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action instanceof ModelDriven) { ModelDriven modelDriven = (ModelDriven) action; ValueStack stack = invocation.getStack(); Object model = modelDriven.getModel(); if (model != null ) { stack.push(model); } if ( refreshModelBeforeResult ) { invocation.addPreResultListener( new RefreshModelBeforeResult(modelDriven, model)); } } return invocation.invoke(); }
public class UserAction implements ModelDriven{ private User user ; @Override public Object getModel () { if ( user == null ){ user = new User(); //user.setUsername(" 这是原来的 User 对象 "); } return user ; } public String updateInput(){ // 根据 ID ,查询数据库,得到 User 对象 user = new UserManager().findUserById( user .getId()); return "update_input" ; }
< form action = "test/user.action" method = "post" > < input type = "hidden" name = "method:update" > id: < input type = "text" name = "id" value = "< s:property value = "id" /> "> < br /> username: < input type = "text" name = "username" value = "< s:property value = "username" /> "> < br /> password: < input type = "text" name = "password" value = "< s:property value = "password" /> "> < br /> age: < input type = "text" name = "age" value = "< s:property value = "age" /> "> < br /> address: < input type = "text" name = "address" value = "< s:property value = "address" /> "> < br /> < input type = "submit" name = "submit" value = " 更新用户 " > </ form > < br />