**Servlet 的 API 的访问 (开发中偶尔会使用到)**
1. 在 Action 类中也可以获取到 Servlet 一些常用的 API, 有如下三种方式获取
* 完全解耦合的方式
* 使用接口注入的方式
* 使用 ServletActionContext 中静态方法直接访问 Servlet 的 API
* 需求: 提供 JSP 的表单页面的数据, 在 Action 中使用 Servlet 的 API 接收到, 然后保存到三个域对象中, 最后再显示到 JSP 的页面上
* 提供 JSP 注册的页面, 演示下面这三种方式:
- <h3 > 注册页面 </h3>
- <form action="${ pageContext.request.contextPath }/xxx.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
- <input type="submit" value="注册" />
- </form>
2. 完全解耦合的方式
* 如果使用该种方式, Struts2 框架中提供了一个类, ActionContext 类, 该类中提供一些方法, 通过方法获取 Servlet 的 API
* 一些常用的方法如下:
* static ActionContext getContext() -- 获取 ActionContext 对象实例
* java.util.Map<java.lang.String,java.lang.Object> getParameters() -- 获取请求参数, 相当于 request.getParameterMap();
* java.util.Map<java.lang.String,java.lang.Object> getSession() -- 获取的代表 session 域的 Map 集合, 就相当于操作 session 域
* java.util.Map<java.lang.String,java.lang.Object> getApplication() -- 获取代表 application 域的 Map 集合
* void put(java.lang.String key, java.lang.Object value) -- 注意: 向 request 域中存入值
* 完成代码的测试
3. 使用接口注入的方式
* Struts2 框架中提供了一些接口, 编写的 Action 类可以是去实现这些接口, 然后实现这些接口中的方法, 这些方法都是把一些 Servlet 的常用对象通过参数的方式传递进来
* 常用的接口如下:
* ServletRequestAware -- 注入 request
* ServletContextAware -- 注入 ServletContext
* ServletResponseAware -- 注入 response.
4. 使用 ServletActionContext 中静态方法直接访问 Servlet 的 API
* Struts2 框架提供了一个类, ServletActionContext, 该类中提供了一些静态的方法
* 具体的方法如下
- * getPageContext();
- * getRequest()
- * getResponse();
- * getServletContext();
- demo4.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>
- <h3 > 注册页面 (完全解耦合的方式)</h3>
- <form action="${ pageContext.request.contextPath }/regist.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
- <input type="submit" value="注册" />
- </form>
- <h3 > 注册页面 (接口注入的方式)</h3>
- <form action="${ pageContext.request.contextPath }/regist2.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
- <input type="submit" value="注册" />
- </form>
- <h3 > 通过 ServletActionContext 对象的方式 </h3>
- <form action="${ pageContext.request.contextPath }/regist3.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
- <input type="submit" value="注册" />
- </form>
- </body>
- </html>
- struts_demo4.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="demo4" extends="struts-default" namespace="/">
- <!-- 完全解耦合的方式 -->
- <action name="regist" class="demo4.UserAction" method="regist">
- <result>/demo4/success.jsp</result>
- </action>
- <!-- 接口注入的方式 -->
- <action name="regist2" class="demo4.UserAction2" method="regist">
- <result>/demo4/success.jsp</result>
- </action>
- <!-- 使用 ServletActionContext 对象的方式 -->
- <action name="regist3" class="demo4.UserAction3" method="regist">
- <result>/demo4/success.jsp</result>
- </action>
- </package>
- </struts>
- Action:
- package demo4;
- import java.util.Map;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 完全解耦合的方式
- * @author mjl
- *
- */
- public class UserAction extends ActionSupport {
- public String regist() {
- // 接收表单的数据
- // 先获取到 ActionContext 对象
- ActionContext context = ActionContext.getContext();
- // 获取请求的参数
- Map < String,
- Object > map = context.getParameters();
- // 通过 Key 获取值
- String[] username = (String[]) map.get("username");
- String[] password = (String[]) map.get("password");
- System.out.println("用户名:" + username[0] + ", 密码:" + password[0]);
- // 获取 session
- Map < String,
- Object > sessionMap = context.getSession();
- // 像该 map 存入具体的值
- sessionMap.put("sessName", "美美");
- return SUCCESS;
- }
- }
- package demo4;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction2 extends ActionSupport implements ServletRequestAware {
- private HttpServletRequest request;
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public String regist() {
- String username = request.getParameter("username");
- request.getSession().setAttribute("sessName", username);
- return SUCCESS;
- }
- }
- package demo4;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 采用 servletActionContext 对象的方法
- * @author mjl
- *
- */
- public class UserAction3 extends ActionSupport {
- public String regist() {
- HttpServletRequest request = ServletActionContext.getRequest();
- String username = request.getParameter("username");
- request.getSession().setAttribute("sessName", username);
- return SUCCESS;
- }
- }
来源: http://www.bubuko.com/infodetail-2516312.html