Struts2系统学习(7)Action中取得请求参数的方法总结
来源:互联网 发布:淘宝神笔模板怎么用 编辑:程序博客网 时间:2024/06/06 07:27
7. Action中取得请求参数的方法总结
7.1 采用基本类型接收请求参数(get/post)
方法一:当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值注入到类对象的属性中,此时在action中访问请求参数只需要访问类属性即可。
方法二:可以通过ActionContext
对象的Map parameterMap=context.getParameters();
方法,得到请求参数Map,然后通过parameterMap来获取请求 参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。
方法三:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter(“参数名”)得到参数值。
实例:(ref:http://lkf520java.iteye.com/blog/565989)
public class GetRequestParameterAction extends ActionSupport { private String bookName; private String bookPrice; // 省略了set和get方法 public String execute() throws Exception{ //方式一: 将参数作action的类属性,struts2通过反射技术自动填充 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("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"); return SUCCESS; }}
7.2 采用复合类型接收请求参数
当action中的属性很多是,就会在action中出现很多的getter,setter方法,不利于核心业务逻辑代码的阅读,常将属性抽象到一个javabean中,在action中持有bean的引用。
public class GetRequestParameterAction extends ActionSupport { private Book book; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; }}
其中,book.java:
public class Book { 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; }}
Struts2首先通过反射技术调用Book的默认构造器(如果没有提供默认构造器,则报InstantiationException异常)创建product对象,然后再通过反射技术调用book中与请求参数同名的属性的setter方法为book对象注入请求参数值;获取时,则可以通过action对象的属性book引用的getter方法获取。
转载请注明出处:http://blog.csdn.net/mark_lq/article/details/49814443
- Struts2系统学习(7)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中取得请求参数值的几种方法
- Java获取本机IP
- Jmeter学习(三)——常用测试元件
- JAVA---关于public class的理解
- 深入Android开发之--理解View#onTouchEvent
- 手机管理应用研究【3】—— 垃圾清理篇
- Struts2系统学习(7)Action中取得请求参数的方法总结
- 读书笔记:virtual关键字分析
- JavaScript中的this
- Oracle 常见问题整理,来自CSDN帖子
- Java中volatile的作用以及用法
- [Shell]使用find命令查找最近修改过的文件
- ci Controller.php解析
- Github上iOS开源项目分类及
- ARM7 低级错误