一、Struts2中的Servlet API
1.1、struts2的Action实现了MVC中C层的作用
针对请求用户显示不同的信息
登录后段保存用户信息 ----session
保存当前在线人数等功能---application
1.2、传统的Servlet API类型
HttpServletRequest
HttpSession
ServletContext
1.3、Struts2中将传统的Servlet API类型被处理成Map类型
访问更方便
不依赖传统Servlet API 类型--解耦合
二、ActionContext
所在包com.opensymphony.xwork2
ActionContext被称为Action上下文或者Action环境
ActionContext,提供每个Action运行时与之相关的所有信息
访问Servlet API
ActionContext类提供getContext()方法获得实例
通过ActionContext类的实例可获得Map类型的请求,会话等
通过Put()和Get()方法在Map类型的对象中存取数据
二、简单例子
web.xml中添加
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页面</title> </head> <body> <form action="user01.action" method="post"> <table> <tr> <td>用户名:<input name="username"/></td> </tr> <tr> <td>用户名:<input type="password" name="password"/></td> </tr> <tr> <td><input type="submit" value="提交"/><input type="reset" value="重置"/></td> </tr> </table> </form> </body> </html>
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>登录成功页面</title> </head> <body> <h1>jsp方式取值:</h1> <%=request.getAttribute("linkaddress") %><br/> <%=session.getAttribute("uname") %><br/> <%=application.getAttribute("online") %><br/> <h1>EL表达式取值:</h1> ${request.linkaddress }<br/> ${session.uname }<br/> ${application.online }<br/> <h1>struts取值:</h1> <!-- 推荐使用这种 --> <s:property value="#request.linkaddress"/><br/> <!-- 使用attr取值不推荐--> <s:property value="#attr.linkaddress"/><br/> <s:property value="#session.uname"/><br/> <s:property value="#attr.uname"/><br/> <s:property value="#application.online"/><br/> <s:property value="#attr.online"/><br/> <s:debug/> </body> </html>
UserAction3.java
package com.pb.web.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction3 extends ActionSupport { private String username; private String password; private Map<String , Object> request; private Map<String , Object> session; private Map<String , Object> application; public String login(){ ActionContext ac=ActionContext.getContext(); request=(Map<String, Object>) ac.get("request"); session=ac.getSession(); application=ac.getApplication(); if(this.getUsername().equals("accp")&&this.getPassword().equals("accp")){ request.put("linkaddress", "这次的登录地点:SZ"); session.put("uname", username); application.put("online", "当前在线人数:xxxx"); System.out.println(request.get("linkaddress")); System.out.println(session.get("uname")); System.out.println(application.get("online")); return SUCCESS; } return INPUT; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Map<String, Object> getRequest() { return request; } public void setRequest(Map<String, Object> request) { this.request = request; } public Map<String, Object> getSession() { return session; } public void setSession(Map<String, Object> session) { this.session = session; } public Map<String, Object> getApplication() { return application; } public void setApplication(Map<String, Object> application) { this.application = application; } }
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="user01" class="com.pb.web.action.UserAction3" method="login"> <result name="success">/loginSuccess.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
三、IOC方式获取Servlet API对象
更改以上的代码
login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页面</title> </head> <body> <!-- <form action="user01.action" method="post"> --> <form action="user02.action" method="post"> <table> <tr> <td>用户名:<input name="username"/></td> </tr> <tr> <td>用户名:<input type="password" name="password"/></td> </tr> <tr> <td><input type="submit" value="提交"/><input type="reset" value="重置"/></td> </tr> </table> </form> <s:property value="#session.msg"/> </body> </html>
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>登录成功页面</title> </head> <body> <h1>jsp方式取值:</h1> <%=request.getAttribute("linkaddress") %><br/> <%=session.getAttribute("uname") %><br/> <%=application.getAttribute("online") %><br/> <h1>EL表达式取值:</h1> ${request.linkaddress }<br/> ${session.uname }<br/> ${application.online }<br/> <h1>struts取值:</h1> <!-- 推荐使用这种 --> <s:property value="#request.linkaddress"/><br/> <!-- 使用attr取值不推荐--> <s:property value="#attr.linkaddress"/><br/> <s:property value="#session.uname"/><br/> <s:property value="#attr.uname"/><br/> <s:property value="#application.online"/><br/> <s:property value="#attr.online"/><br/> <s:debug/> </body> </html>
UserAction2.java
package com.pb.web.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.ActionSupport; public class UserAction2 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware { private static final long serialVersionUID = 1L; //属性要有getter、setter方法 private String username; private String password; //request,session,application不需要getter、setter方法 private Map<String , Object> request; private Map<String , Object> session; private Map<String , Object> application; public String login(){ if(this.getUsername().equals("accp")&&this.getPassword().equals("accp")){ request.put("linkaddress", "这次的登录地点:SZ"); session.put("uname", username); application.put("online", "当前在线人数:xxxx"); System.out.println(request.get("linkaddress")); System.out.println(session.get("uname")); System.out.println(application.get("online")); return SUCCESS; } session.put("msg", "用户名或者密码不正确!"); return INPUT; } //使用struts2提供接口来生成application,session,request对象, @Override public void setApplication(Map<String, Object> application) { this.application=application; } @Override public void setSession(Map<String, Object> session) { this.session=session; } @Override public void setRequest(Map<String, Object> request) { this.request=request; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
struts.xml
<action name="user02" class="com.pb.web.action.UserAction2" method="login"> <result name="success" type="dispatcher">/loginSuccess.jsp</result> <result name="input" type="redirect">/login.jsp</result> </action>
使用传统的方式获取Servlet API
实现ServletRequestAware接口
package com.pb.web.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class UserAction04 extends ActionSupport implements ServletRequestAware, ServletContextAware { private String username; private String password; private HttpServletRequest request; private HttpSession session; private ServletContext application; public String login() { // 获取对象 session = request.getSession(); // 保存数据 request.setAttribute("loginAddress", "您本次登录地点是:北京"); session.setAttribute("username", username); application.setAttribute("online", "当前在线人数:XXX"); // 获取数据 System.out.println(request.getAttribute("loginAddress")); System.out.println(session.getAttribute("username")); System.out.println(application.getAttribute("online")); return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } @Override public void setServletContext(ServletContext application) { this.application = application; } }
或者使用时直接获取对象
package com.pb.web.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 UserAction03 extends ActionSupport { private String username; private String password; private HttpServletRequest request; private HttpSession session; private ServletContext application; public String login() { // 获取对象 request = ServletActionContext.getRequest(); session = request.getSession(); application = ServletActionContext.getServletContext(); // 保存数据 request.setAttribute("loginAddress", "您本次登录地点是:北京"); session.setAttribute("username",username); application.setAttribute("online","当前在线人数:XXX"); // 获取数据 System.out.println(request.getAttribute("loginAddress")); System.out.println(session.getAttribute("username")); System.out.println(application.getAttribute("online")); return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }