在Struts2的Action中取得请求参数值的几种方法
来源:互联网 发布:平价好用口红知乎 编辑:程序博客网 时间:2024/05/15 14:10
Action类代码
public class TestAction extends ActionSupport { private String bookName; private String bookPrice; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookPrice() { return bookPrice; } public void setBookPrice(String bookPrice) { this.bookPrice = bookPrice; } public String execute() throws Exception{ //方式一: 将参数作为Action的类属性,让OGNL自动填充 System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:"); System.out.println("bookName: "+this.bookName); System.out.println("bookPrice: " +this.bookPrice); //方法二:在Action中使用ActionContext得到parameterMap获取参数: ActionContext context=ActionContext.getContext(); Map parameterMap=context.getParameters(); String bookName2[]=(String[])parameterMap.get("bookName"); String bookPrice2[]=(String[])parameterMap.get("bookPrice"); System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:"); System.out.println("bookName: " +bookName2[0]); System.out.println("bookPrice: " +bookPrice2[0]); //方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数 HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST); String bookName=request.getParameter("bookName"); String bookPrice=request.getParameter("bookPrice"); System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:"); System.out.println("bookName: " +bookName); System.out.println("bookPrice: " +bookPrice); return SUCCESS; } }
总结: 方法一:当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。方法二:可以通过ActionContext对象Map parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。方法三:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter("参数名")得到参数值。 Struts2中Action接收参数的方法主要有以下三种:1.使用Action的属性接收参数: a.定义:在Action类中定义属性,创建get和set方法; b.接收:通过属性接收参数,如:userName; c.发送:使用属性名传递参数,如:user1!add?userName=Magci;2.使用DomainModel接收参数: a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法; b.接收:通过对象的属性接收参数,如:user.getUserName(); c.发送:使用对象的属性传递参数,如:user2!add?user.userName=MGC;3.使用ModelDriven接收参数: a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象; b.接收:通过对象的属性接收参数,如:user.getUserName(); c.发送:直接使用属性名传递参数,如:user2!add?userName=MGC;
0 0
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- 在Struts2的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- Struts2 的Action中取得请求参数值的几种方法
- 实际项目中的常见算法
- linux基础学习笔记-4-shell编程
- cocos2dx UI性能优化
- NSDate的常用用法
- C语言第十五篇:C语言中.h和.c文件解析(很精彩)
- 在Struts2的Action中取得请求参数值的几种方法
- Wonderware配置-手动修改数据 4
- 51NOD 1009 数字1的数量
- 函数指针总结
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 网络协议的认识
- 河北沧州财政局收到5亿"环保"拨款 不知怎么分
- 解密SparkStreaming运行机制和架构进阶之Job
- python函数一日一发--关于divmod函数