Struts2 訪問request session application物件
Struts2提供了了多種方式來訪問上述的三種現象,歸結起來,可以劃分為兩大類:與Servlet API 解耦的訪問方式和與Servlet API耦合的訪問方式。
一. 與Servlet API 解耦的訪問方式
為了避免與Servlet API 耦合在一起,方便Action類做單元測試,Struts2對HttpServletRequest,HttpSession和ServletContext進行了封裝,構造了三個Map物件來替代這三種物件,在Action中,直接使用HttpServletRequest,HttpSession和ServletContext對應的Map物件來儲存和讀取資料。
要獲取這三個物件,可以使用com.opensymphont.xwork2.ActionContext類。
ActionContext是action執行的上下文,在ActionCOntext中儲存了action執行所需的一組物件,包括parameters,request,session,application和locale等。ActionContext類定義瞭如下方法,用於獲取HttpServletRequest,HttpSession和ServletContext對應的Map物件。
01.public Object get(Object key)
ActionContext類沒有提供類似getRequest()這樣的方法來過去封裝了HttpServletRequest的Map物件。要得到請求Map物件,你需要為get()方法傳遞引數“request”。
02.public Map getSession()
獲取封裝了HttpSession的Map物件。
03.public Map getApplication()
獲取封裝了ServletCOntext的Map物件。
案例:登入進入成功頁面,顯示使用者名稱。
package cn.action; import cn.entity.User; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack; import org.apache.struts2.ServletActionContext; import java.util.Map; /** * Created by micro on 2017/10/21. */ public class LoginAction implements Action,ModelDriven{ private User user=new User(); public String execute() throws Exception { if ("admin".equals(user.getName())&&"123".equals(user.getPassword())){ //解耦方式 ActionContext context = ActionContext.getContext();
//request
Map request= (Map)context.get("request");
//application
Map<String, Object> application = context.getApplication();
//session
Map<String, Object> map = context.getSession();
map.put("uname",user.getName());
//值棧 頁面直接用 ${uname} 獲取值
//方式一:
/*ValueStack valueStack = context.getValueStack();
valueStack.push(map);*/
//方式二:
HttpServletRequest request = ServletActionContext.getRequest();
ValueStack vs= (ValueStack)request.getAttribute("struts.valueStack");
vs.push(map);
return "success";
} return "input";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Object getModel() {
return user;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default"> <action name="loginAction" class="cn.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
login.jsp
<%@page language="java" contentType="text/html; utf-8" %>
<%@taglib uri="/struts-tags" prefix="s"%>
<title>登入</title>
<html>
<body>
<s:form name="form1" namespace="/" method="POST" action="loginAction">
使用者名稱:<s:textfield name="name"></s:textfield>
密碼: <s:textfield name="password"></s:textfield>
<s:submit value="登入"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@page language="java" contentType="text/html; utf-8" isELIgnored="false" %>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<body>
<!--方式一:-->
<s:property value="#session.uname"/>
<!--方式二: 值棧-->
${uname}
</body>
</html>
除了利用ActionContext‘來獲取request,session和application物件這種方式,Action類可以實現某些特定的介面,讓Struts2框架在執行是向Action例項注入request,session和application物件。與之對應的三個介面和它們的方法如下:
二.與Servlet API 耦合的方式
public class LoginAction implements Action,ModelDriven{
private User user=new User();
public String execute() throws Exception {
if ("admin".equals(user.getName())&&"123".equals(user.getPassword())){
//耦合方式一:
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("uname",user.getName());
ServletContext servletContext = ServletActionContext.getServletContext();
servletContext.setAttribute("unames",user.getName());
return "success";
}
return "input";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Object getModel() {
return user;
}
package cn.action;
import cn.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Created by micro on 2017/10/21.
*/
public class LoginAction implements Action,ModelDriven,ServletRequestAware,ServletContextAware {
private User user=new User();
HttpServletRequest request;
ServletContext context;
public String execute() throws Exception {
if ("admin".equals(user.getName())&&"123".equals(user.getPassword())){ //耦合方式二:實現ServletRequestAware介面 給request物件賦值 ServletContextAware介面給contest物件注入
HttpSession session = request.getSession();
session.setAttribute("uname",user.getName());
return "success";
}
return "input";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Object getModel() {
return user;
}
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request=httpServletRequest;
}
public void setServletContext(ServletContext servletContext) {
this.context=servletContext;
}
}
總結:
直接用action物件接收使用者輸入的資料,分別是Servlet API 解耦方式和Servlet API 耦合方式。