现在的位置: 首页 > 综合 > 正文

ssh2 action中通过ActionContext取得HttpServletRequest对象 然后使request.getParameter(“参数名”)得到参数值

2018年07月01日 ⁄ 综合 ⁄ 共 1607字 ⁄ 字号 评论关闭

http://zhougaoyun.iteye.com/blog/1979047  从这个地址拷贝来备自己以后查用

 

struts2在action获取表单请求的方式与jquery get参数方式

 
1、jquery中要在get中传递参数
$(function(){
$.get(url,{username:user,password:pass},function(data){
window.location.href="main.jsp";
});
});

2、表单
  1)当把参数作为Action的类属性,且提供属性的getter/setter方法时,xwork的OGNL会自动把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。

        System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:"); 
        System.out.println("bookName: "+this.bookName); 
        System.out.println("bookPrice: " +this.bookPrice);

  2)[color=darkred]可以通过ActionContext对象Map  parameterMap=context.getParameters();方法,得到请求参数Map,然后通过parameterMap来获取请求参数。需要注意的是:当通过parameterMap的键取得参数值时,取得是一个数组对象,即同名参数的值的集合 。[/color]

        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]);

   3)通过ActionContext取得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);

抱歉!评论已关闭.