1.在action中以解耦合的方式来访问Servlet API——–使用ActionContext对象
在Struts2中Action API 已经与Servlet API解耦合了
Servlet API 常见操作:表单提交 请求参数,获取参数,向request、session、application三个范围内存取数据。
ActionContext actionContext = ActionContext.getContext();
1)actionContext.getParameters();
获取所有请求参数
2)actionContext.put("company", "公司"); / actionContext.get("company")
对request范围内存取数据
3)actionContext.getSession();
获取session数据Map,对session范围内存取数据
4)actionContext.getApplication();
获取ServletContext数据Map,对应访问存取数据
例子:
新建LoginAction.java
package com.cn.struts2.actionimport java.util.Mapimport com.opensymphony.xwork2.ActionContextimport com.opensymphony.xwork2.ActionSupportpublic class LoginAction extends ActionSupport{ @Override public String execute() throws Exception { ActionContext actionContext = ActionContext.getContext() //1.获取请求参数 Map<String,Object> par = actionContext.getParameters() Object value = par.get("username") System.out.println(value) //2.读取request的Attribute actionContext.put("company", "公司") System.out.println(actionContext.get("company")) //3.存取session的Attribute Map<String, Object> sessionMap = actionContext.getSession() sessionMap.put("age",20) System.out.println(sessionMap.get("age")) //4.存取Application的Attribute Map<String,Object> applicationMap = actionContext.getApplication() applicationMap.put("info", "yaoxiayu") System.out.println(applicationMap.get("info")) return SUCCESS }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
新建form.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <h1>Login</h1> <form action="${pageContext.request.contextPath}/login1.action" method="post"> 请输入您的姓名:<input type="text" name="username"/> <input type="submit" value="提交"/> </form></body></html>
新建success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <h1>success</h1> <h6>${requestScope.company}</h6> <h6>${sessionScope.age}</h6> <h6>${applicationScope.info}</h6></body></html>
在struts.xml中添加
<action name="login1" class="com.cn.struts2.action.LoginAction"> <result name="success">/jsp/success.jsp</result> </action>
访问http://localhost:8080/web/jsp/from.jsp 输入名字然后点击提交,跳转到success页面,页面显示success、公司、20、yaoxiayu,即表示成功。
2.使用接口注入的方式,操作Servlet API
ServletContextAware(接口):注入ServletContext对象
ServletRequestAware(接口): 注入request对象
ServletResponseAware(接口):注入response对象
使用哪个接口就实现哪个接口即可。
例子:
新建LoginAction2.java
package com.cn.struts2.action;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class LoginAction2 extends ActionSupport implements ServletRequestAware,ServletContextAware{ private HttpServletRequest request; private ServletContext context; @Override public String execute() throws Exception { System.out.println(request.getParameter("username")); request.setAttribute("company", "公司"); request.getSession().setAttribute("age", 20); context.setAttribute("info", "下雨了"); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } @Override public void setServletContext(ServletContext context) { this.context = context; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
新建form2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <h1>Login2</h1> <form action="${pageContext.request.contextPath}/login2.action" method="post"> 请输入您的姓名:<input type="text" name="username"/> <input type="submit" value="提交"/> </form></body></html>
在struts.xml中添加配置
<action name="login2" class="com.cn.struts2.action.LoginAction2"> <result>jsp/success.jsp</result> </action>
访问http://localhost:8081/web/jsp/form2.jsp 然后输入姓名点击提交,跳转到新页面显示如下图即表示成功了。
3.在Action中直接通过ServletActionContext获得Servlet API
ServletActionContext.getRequest()
: 获得request对象(session)
ServletActionContext.getResponse()
:获得response对象
ServletActionContext.getServletContext()
:获得ServletContext对象
静态方法没有线程问题,ThreadLocal
例子:
新建LoginAction3.java类
package com.cn.struts2.action;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction3 extends ActionSupport{ @Override public String execute() throws Exception { System.out.println(ServletActionContext.getRequest().getParameter("username")); ServletActionContext.getRequest().setAttribute("company", "公司"); ServletActionContext.getRequest().getSession().setAttribute("age", 19); ServletActionContext.getServletContext().setAttribute("info", "雨很大"); return SUCCESS; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
新建form3.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <h1>Login3</h1> <form action="${pageContext.request.contextPath}/login3.action" method="post"> 请输入您的姓名:<input type="text" name="username"/> <input type="submit" value="提交"/> </form></body></html>
在struts.xml配置文件中添加
<action name="login3" class="com.cn.struts2.action.LoginAction3"> <result>jsp/success.jsp</result> </action>
访问http://localhost:8081/web/jsp/form3.jsp 然后输入姓名点击提交,跳转到新页面显示如下图即表示成功了。