先看GetRequestParameterAction类代码:

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("参数名")得到参数值。

在Struts2的Action中取得请求参数值的几种方法的更多相关文章

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

    先看GetRequestParameterAction类代码:  Java代码 public class GetRequestParameterAction extends ActionSupport ...

  2. 在Struts2的Action中获得request response session几种方法

    转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...

  3. struts2的action从request获取参数值的几种方式

    使用jquery框架的ajax能够方便的向后台传递参数,以$.post为例,参数有2种方式字符串和键值对:$.post(url, "name=aty&age=25")和$. ...

  4. Struts2的Action中获取request对象的几种方式?

    通过ActionContext.getSession获取 通过ServletActionContext.getRequest()获取 通过SessionAware接口注入 通过ServletReque ...

  5. Struts2 在Action中操作数据

    Servlet存储数据的方式 在Servlet中,使用ServletContext对象来存储整个WebApp的数据,ServletContext中直接存储整个WebApp的公共数据,可使用set|ge ...

  6. Struts2中Action取得表单数据的几种方法

    Struts2中Action取得表单数据的几种方法   Struts2中Action获得表单数据的几种方法struts2 Action获取表单传值 1.通过属性驱动式JSP: <form act ...

  7. 在struts2的action中操作域对象(request、session)

    在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...

  8. JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  9. SpringMVC的请求转发的三种方法

    SpringMVC请求转发的三种方法 首先明白请求转发是一次请求,地址栏不会发生变化,区别于重定向.springmvc环境自行配置. 以下举例中存在如下文件/WEB-INF/pages/success ...

随机推荐

  1. bzoj 2784 时间流逝 —— 树上高斯消元

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2784 其实转移是一棵树,从根到一个点表示一种能量圈状态,当能量值大于 T 是停止,也就是成为 ...

  2. 2017中国大学生程序设计竞赛 - 女生专场(dp)

    Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...

  3. php中的continue用法

    continue 2 表示跳出两层 continue 默认跳出一层 if (count($content_arr) > 0 ) { // 获取相应的goods数据 $goodsdata = ar ...

  4. python开发IO模型:阻塞&非阻塞&异步IO&多路复用&selectors

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  5. python开发面向对象基础:接口类&抽象类&多态&钻石继承

    一,接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数名)且并未实 ...

  6. Dynamics CRM 2011 Web Service

    Data Services:   SOAP Endpoint REST Endpoint Capabilities Assign Records Retrieve Metadata Execute M ...

  7. Android使用图表库简单教程

    经常要用到统计数据这个功能,要直观的显示出来,最好还是用图表.自己弄也麻烦,所以用了Github上的一个非常著名的开源图标库:MpChart. 使用前去网上找它俩的jar包,然后导入就行.资源比较好找 ...

  8. keil的使用:新建Project

    新建项目--->新建文件夹----->把新建的项目放在自己的文件夹中------>选择开发板------>添加开发板的驱动文件---->main函数 项目分组基本如图,S ...

  9. 手机发烫是为何—— App 电量测试定位方法

    为什么要做电量测试 随着移动互联网的快速发展,手机的实用性.娱乐性越来越强.日常使用中发现,安装了应用后,即使不怎么使用,电量也会消耗很快.但如果恢复出场设置充满电后,手机可以待机很长时间.真相只有一 ...

  10. How to Change Master Page @ Run-time

    This tip will give complete knowledge of how to change master page, render controls and accessing it ...