struts2中我们可以使用Model-Driven和Property-Driven进行表单数据的封装,便于我们在Action中直接使用
属性驱动时,只要保持表单域名字和Action属性名一致,即可自动进行值得填充。
第一种
http://localhost:8080/Struts/paramter?username=aa&password=bb
访问的路径和参数username=aa&password=bb
代码
package org.heinrich.action; import com.opensymphony.xwork2.ActionSupport; public class ParamterAction extends ActionSupport { private static final long serialVersionUID = -8556185601880040463L; private String username; private String password; public String login(){ System.out.println(username); System.out.println(password); return SUCCESS; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
hr-user.xml文件
<action name="paramter" class="org.heinrich.action.ParamterAction" method="login"> <result>/success.jsp</result> </action>
在Action中直接可以拿到
username :heinrich password :123456
在页面上我要拿到username和passowrd的话
username : ${username } password : ${password }
框架会为你把username和password传到下一个页面
username : heinrich password : 123456
还有一个属性驱动的方式
http://localhost:8080/Struts/paramter?user.username=heinrich&user.password=123456
参数传递方式user.username=heinrich&user.password=123456
package org.heinrich.action; import org.heinrich.User; import com.opensymphony.xwork2.ActionSupport; public class ParamterAction extends ActionSupport { private static final long serialVersionUID = -8556185601880040463L; private User user; public String login(){ System.out.println(user.getUsername()); System.out.println(user.getPassword()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
获取前台传过来的参数,还会把他放到下个页面使用
页面取值 username : ${user.username } password : ${user.password }
还有一种基于模型驱动的方式,官方不推荐使用,我比较喜欢用官方的
package org.heinrich.action; import org.heinrich.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class ParamterAction extends ActionSupport implements ModelDriven<User>{ private static final long serialVersionUID = -8556185601880040463L; private User user = new User(); public String login(){ System.out.println(user.getUsername()); System.out.println(user.getPassword()); return SUCCESS; } @Override public User getModel() { return user; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
在我看来这种方式不错,用来做Abstract类的Action的时候很好用
其实还有
方法3:可以通过ActionContext对象Map parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。
package org.heinrich.action; import java.util.Map; import org.heinrich.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class ParamterAction extends ActionSupport{ private static final long serialVersionUID = -8556185601880040463L; public String login(){ Map<String, Object> contextMap = ActionContext.getContext().getParameters(); for(String key:contextMap.keySet()){ Object object = contextMap.get(key); System.out.println(key+" :"+object.toString()); } return SUCCESS; } }
方法4:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter("参数名")得到参数值。
package org.heinrich.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.heinrich.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class ParamterAction extends ActionSupport{ private static final long serialVersionUID = -8556185601880040463L; public String login(){ HttpServletRequest request =(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); String username = request.getParameter("user.username"); String password = request.getParameter("user.password"); System.out.println(username); System.out.println(password); request.setAttribute("username", username); request.setAttribute("password", password); return SUCCESS; } }