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

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

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

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

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

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值