web之家  

Struts2 的Action中取得请求参数值的几种方法

先看GetRequestParameterAction类代码: 
Java代码  
  1. public class GetRequestParameterAction extends ActionSupport {    
  2.     
  3.     private String bookName;    
  4.     private String bookPrice;    
  5.         
  6.     public String getBookName() {    
  7.         return bookName;    
  8.     }    
  9.     
  10.     public void setBookName(String bookName) {    
  11.         this.bookName = bookName;    
  12.     }    
  13.     
  14.     public String getBookPrice() {    
  15.         return bookPrice;    
  16.     }    
  17.     
  18.     public void setBookPrice(String bookPrice) {    
  19.         this.bookPrice = bookPrice;    
  20.     }    
  21.         
  22.         
  23.     public String  execute() throws Exception{    
  24.             
  25.             
  26.         //方式一: 将参数作为Action的类属性,让OGNL自动填充    
  27.              
  28.         System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");    
  29.         System.out.println("bookName: "+this.bookName);    
  30.         System.out.println("bookPrice: " +this.bookPrice);    
  31.             
  32.             
  33.         //方法二:在Action中使用ActionContext得到parameterMap获取参数: 
  34.                      这个map只能读取不能写入,写入也不报错,但是不能被读出   
  35.                     (请求参数的值对应字符串数组)
  36.         ActionContext context=ActionContext.getContext();    
  37.         Map  parameterMap=context.getParameters();    
  38.          // 请求参数的值对应字符串数组
  39.         String bookName2[]=(String[])parameterMap.get("bookName");    
  40.         String bookPrice2[]=(String[])parameterMap.get("bookPrice");    
  41.             
  42.         System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:");    
  43.         System.out.println("bookName: " +bookName2[0]);    
  44.         System.out.println("bookPrice: " +bookPrice2[0]);    
  45.             
  46.             
  47.         //方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数    
  48.         HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);     
  49.              
  50.         String bookName=request.getParameter("bookName");    
  51.         String bookPrice=request.getParameter("bookPrice");    
  52.             
  53.         System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");    
  54.         System.out.println("bookName: " +bookName);    
  55.         System.out.println("bookPrice: " +bookPrice);    
  56.         return SUCCESS;    
  57.             
  58.     }    
  59.     
  60. }    
总结: 

方法一:当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。 
方法二:可以通过ActionContext对象Map  parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合。 
方法三:通过ActionContext取得HttpServletRequest对象,然后使用request.getParameter("参数名")得到参数值。
posted on 2016-12-17 16:37  路修远而求索  阅读(517)  评论(0编辑  收藏  举报