在Struts2中提供了一种机制访问Request, Session, Application.(在Action中访问Web元素)
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String context = request.getContextPath(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> 取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用: <ol> <li>前三者:依赖于容器</li> <li>前三者:IOC</li> (只用这种) <li>后三者:依赖于容器</li> <li>后三者:IOC</li> </ol> <br /> <form name="f" action="" method="post"> 用户名:<input type="text" name="name"/> 密码:<input type="text" name="password"/> <br /> <input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();" /> <input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();" /> <input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();" /> <input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();" /> </form> </body> </html>
第一种:依赖于容器,取得Map类型的request,session,application
在Map与HttpRequest之间做了映射,将Map中的值放到Request中,看源码。
package com.bjsxt.struts2.user.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction1 extends ActionSupport { //取得Map类型的request,session,application private Map request; private Map session; private Map application; public LoginAction1() { /*1.ActionContext.getContext()是单例吗?不是单例,其实你想想就知道了,不可能是单例,因为每个不同的请求里面,request里面放的值是不样的。 如果是单例的话,你只能放同一个request里面,那就不对了。推荐看看源码,其实是一个ThreadLocal */ 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; } }
第二种(推荐,只用这一种):IOC(也叫DI),取得Map类型的request,session,application
在Map与Request做了映射,将Request中的值放到Map中,看源码。
IOC(Inverse of Control)控制反转,本来是Action自己控制的request的值,现在将request的值交给了Struts2来控制
package com.bjsxt.struts2.user.action; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware { private Map<String, Object> request;//本来是Action控制request的值,现在将request的值交给了Struts2来控制 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; } }
第三种(不推荐):依赖于容器,真实类型 HttpServletRequest, HttpSession, ServletContext的引用
package com.bjsxt.struts2.user.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; 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; } }
第四种(不推荐):IOC,真实类型 HttpServletRequest, HttpSession, ServletContext的引用
package com.bjsxt.struts2.user.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; 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(); } }
相关推荐
在这个主题“Struts2_访问web元素_accessWebElements_s”中,我们将深入探讨如何在Struts2中有效地访问和操作web元素。 首先,了解Struts2的生命周期至关重要。当一个HTTP请求到达服务器时,Struts2框架会接管请求...
综上所述,"Struts2_1200_AccessWebElements"可能包含关于如何在Struts2环境中使用注解、配置文件、OGNL表达式、拦截器、结果类型、标签库以及Ajax和国际化功能来访问和操作Web元素的实例、代码示例和解释。...
这个过程可以通过`Struts2_1200_AccessWebElements`文件来了解如何访问和操作Web元素。 数据验证是Struts2中非常重要的一部分,`Struts2_1100_SimpleDataValiation`可能展示了如何使用内置的验证框架对用户输入进行...
`struts2_AccessWebElements`可能是关于访问网页元素的内容,如表单字段、按钮等,Struts2提供了模型驱动的方式方便地绑定和操作这些元素。 `struts2_Actionadvance2`可能是Action进阶内容,涵盖了更高级的主题,...
8. **Struts2_1200_AccessWebElements**: 这个示例可能涉及了如何访问和操作网页元素,比如表单字段、按钮等。Struts2提供了多种方式来控制视图层的渲染,包括JSP、FreeMarker、Velocity等模板技术。 通过这些...
9. **访问Web元素**:`Struts2_1200_AccessWebElements`可能展示了如何通过Struts2访问和操作HTML表单元素,以及如何处理HTTP请求参数。 学习这个入门案例,你将能理解Struts2的基本架构,如何编写Action类,配置...
Structs2_AccessWebElements Structs2_ActionAdvance Structs2_ActionIntroduce Structs2_Annotation Structs2_BasicConfiguration Structs2_ByEclipse Structs2_ByManual Structs2_Exception Structs2_FileUpload ...