共四种方式,示例在下面给出,其中第二种常用。 【注意1】:我们需要知道前两种方法得到的是Map<String,Object>,而后两种方式得到的才是真正的request等对象。而Map就是把request对象中的属性取出做成了键值对而已。 【注意2】:另外如果就是为了在action和jsp传递参数的话,只需要在action中定义成员,然后Jsp中利用struts标签<s:property value="name"/>就能够访问到数据,而这些内容都是被保存在了value stack中。关于value stack 和 stack context 会在后面得内容涉及。 ? 方法一: public class LoginAction1 extends ActionSupport { private Map request; private Map session; private Map application; public LoginAction1() { request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); } public String execute() { request.put("r1", "r1"); session.put("s1", "s1"); application.put("a1", "a1"); return SUCCESS; } } 方法二: public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware { private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; //DI dependency injection //IoC inverse of control public String execute() { request.put("r1", "r1"); session.put("s1", "s1"); application.put("a1", "a1"); return SUCCESS; } ? @Override public void setRequest(Map<String, Object> request) { this.request = request; } @Override public void setSession(Map<String, Object> session) { this.session = session; } @Override public void setApplication(Map<String, Object> application) { this.application = application; } } 方法三: public class LoginAction3 extends ActionSupport { private HttpServletRequest request; private HttpSession session; private ServletContext application; public LoginAction3() { request = ServletActionContext.getRequest(); session = request.getSession(); application = session.getServletContext(); } public String execute() { request.setAttribute("r1", "r1"); session.setAttribute("s1", "s1"); application.setAttribute("a1", "a1"); return SUCCESS; } } 方法四: public class LoginAction4 extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; private HttpSession session; private ServletContext application; public String execute() { request.setAttribute("r1", "r1"); session.setAttribute("s1", "s1"); application.setAttribute("a1", "a1"); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; this.session = request.getSession(); this.application = session.getServletContext(); } }
详细解决方案
怎么在Action中获取:request,session,application
热度:89 发布时间:2013-08-13 16:43:28.0
相关解决方案
- request.setAttribute("list" "''");该怎么解决
- ssh action 调用service的时分service 为null
- Struts-config.xml配置文件《action-地图pings》元素的详解
- hibernate 连接 oracle session 有关问题
- request.setAttribute("message" message)不能传int参数,该怎么处理
- Servlet Session 购物车,该怎么解决
- 关于<form action >的一个有关问题
- 新手求解,使用struts报错Cannot retrieve definition for form bean studsForm on action /stu,该怎么解决
- form 表单交付 <form action="<c:url value='desktop'/>"
- RequestDispatcher rd = request.getRequestDispatcher跳转页面出现有关问题
- Servlet Session 购物车解决办法
- request.setAttribute("message" message)不能传int参数,该怎么解决
- ssh多数据库配置 如何get session
- 生手求解,使用struts报错Cannot retrieve definition for form bean studsForm on action /stu
- 上面是JAVA WEB中,学生在线考试(action)的代码,有几个不明白,帮忙解释一上
- SSH有关问题,There is no Action 地图ped for action name null
- 为何jsp request.getParameter取不到值,但request.getQueryString()却有值呢
- hibernate中Query query=session.createQuery的有关问题
- CAPTCHA 模块中 ajax form 导致CAPTCHA session reuse attack detected异常,解决方法
- 有个有关问题,请各位帮忙看看:java.lang.ClassNotFoundException: org.apache.struts.action.Act
- 采取spring mvc和hibernate开发,application.xml的配置求详细分析
- Parse error in application web.xml,该如何解决
- 求老妪能解解释Response、Request
- 警告: Could not find action or result 什么原因?该如何解决
- session 覆盖的有关问题 求高手
- Could not deserialize session data解决方法
- response.write(session.getAttribute("BranchName")提示异常
- strust2 No result defined for action and result input有关问题
- request.setAttribute()有关问题
- JSP上if(trim(session.getAttribute("grade"))== "0") 多谢!