打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在Struts2的Action中取得请求参数值的几种方法
在Struts2的Action中取得请求参数值的几种方法
文章分类:Java编程
先看GetRequestParameterAction类代码:
Java代码 
 
public class GetRequestParameterAction 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("参数名")得到参数值。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[转]IBatis学习总结
struts2 面试题
struts2.x的ActionContext,ValueStack和常用标签介绍
request.getAttribute和getParameter 的区别
getParameter和getAttribute的区别
Request对象详细介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服