How to get the HttpServletRequest in Struts 2
来源:互联网 发布:java斗牛算法 编辑:程序博客网 时间:2024/06/05 15:22
In Struts 2 , you can use the following two methods to get the HttpServletRequest object.
1. ServletActionContext
Get the HttpServletRequest object directly from org.apache.struts2.ServletActionContext.
import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext; public class LocaleAction{//business logicpublic String execute() {HttpServletRequest request = ServletActionContext.getRequest();return "SUCCESS";}}
2. ServletRequestAware
Make your class implements the org.apache.struts2.interceptor.ServletRequestAware.
When Struts 2 ‘servlet-config‘ interceptor is seeing that an Action class is implemented theServletRequestAware interface, it will pass a HttpServletRequest reference to the requested Action class via thesetServletRequest() method.import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware; public class LocaleAction implements ServletRequestAware{ HttpServletRequest request; //business logicpublic String execute() {String param = getServletRequest().getParameter("param");return "SUCCESS"; } public void setServletRequest(HttpServletRequest request) {this.request = request;} public HttpServletRequest getServletRequest() {return this.request;}}
Struts 2 documentation is recommended ServletRequestAware instead ofServletActionContext.
Reference
- http://struts.apache.org/2.x/docs/how-can-we-access-the-httpservletrequest.html
- http://struts.apache.org/2.0.6/struts2-core/apidocs/org/apache/struts2/interceptor/ServletRequestAware.html
- How to get the HttpServletRequest in Struts 2
- How to remove the action suffix extension in Struts 2
- How to remove the action suffix extension in Struts 2
- How to get the object type in javascript:
- In RadGrid, How to get the control inside usercontrol
- how to get the function back trace in php
- How to schedule and get the report in SCOM2012
- How to get the JSON results in ExtJS
- How to get the dmesg output in initramfs (debian)
- How to get the 2D coordinate of the max or min value in matrix in matlab
- How to Get the WDK
- How to retreive raw post data from HttpServletRequest in java
- How to get to the point quickly?
- How to get the current time in milliseconds in C Programming?
- How to get all the members in user group by using LDAP in Perl?
- How to get files to show up in the Process Monitor/Report Repository
- Ogre : How to actually get morph animations to work in the engine
- How to get the content of the 'identity' section in web.config
- (杂)小知识点
- 获知是否特定参数变化
- android 广播(broadcastReceiver)详解
- 手机GPRS定位
- PYPL编程语言人气指数:C#才是2012年度语言
- How to get the HttpServletRequest in Struts 2
- 大数与小数相乘算法
- 13_4
- android activity跳转 和 生命周期
- 大数相乘算法
- inno setup打包应用程序
- 大数的阶乘算法
- chmod 命令——chmod 755与 chmod 4755区别
- PowerDesigner概念模型详解