Struts之訪問Servlet API

1.通過ActionContext訪問Servlet API

案例:獲取表單資料,並在Servlet三大域物件中儲存key-value,轉發到show.jsp。

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>
<form action="${pageContext.request.contextPath}/domainAction.action" method="post" enctype="application/x-www-form-urlencoded">
使用者名稱:<input type="text" name="username"><br>
密碼:<input type="text" name="password"><br>
地址:<input type="text" name="address"><br>
<input type="submit" value="提交"> 
<input type="reset" value="重置">
</form>
</body>
</html>

DomainAction.java

package zh.action.demo;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DomainAction extends ActionSupport{
// 預設執行execute()
public String execute(){
// 獲取當前執行緒的ActionContext
ActionContext actionContext = ActionContext.getContext();
// 獲取表單資料
Map<String, Object> paramMap = actionContext.getParameters();
Set<String> keySet = paramMap.keySet();
for(String key : keySet){
Object[] valueArray = (Object[]) paramMap.get(key);
System.out.println(key "=" valueArray[0]);
}
// 向request域中儲存資料
actionContext.put("request_name", "request_value");
// 向session域中儲存資料
actionContext.getSession().put("session_name", "session_value");
// 向application域中儲存資料
actionContext.getApplication().put("application_name", "application_value");
return SUCCESS;
}
}

struts.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>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="demo1" namespace="/" extends="struts-default">
<!-- 不寫method屬性,預設執行execute() -->
<action name="domainAction" class="zh.action.demo.DomainAction">
<result name="success" type="dispatcher">/show.jsp</result>
</action>
</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>	
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--  配置Struts的攔截器 -->
<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>
</web-app>

show.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>獲取域中的資料</h1>
${requestScope.request_name}<br>
${sessionScope.session_name}<br>
${applicationScope.application_name}<br>
</body>
</html>

控制檯輸出:

2.通過ServletActionContext訪問Servlet API

DomainAction.java

package zh.action.demo;
import java.util.Map;
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.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通過ServletActionContext訪問Servlet API
* @author Zh
*/
public class DomainAction extends ActionSupport {
// 預設執行execute()
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext servletContext = ServletActionContext.getServletContext();
// 獲取表單資料
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
System.out.println(username "," password "," address);
// 小龍女,XLN,絕情谷底
// 分別向Servlet三大域中儲存資料
request.setAttribute("request_name", "request_value");
session.setAttribute("session_name", "session_value");
servletContext.setAttribute("application_name", "application_value");
return SUCCESS;
}
}

3.通過XXXAware介面訪問Servlet API

實現上述介面後,還需重寫抽象方法setXXX(…)。

這裡,xxx 表示 三大域物件,即servletrequest、session、servletContext。

DomainAction.java

package zh.action.demo;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 實現三個介面,重寫set()方法
* @author Zh
*/
public class DomainAction extends ActionSupport implements ServletRequestAware,
SessionAware, ServletContextAware {
// 內部定義三個變數
private HttpServletRequest request;
private Map<String,Object> session;
private ServletContext servletContext;
// 重寫三個介面中的抽象方法
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
// 預設執行execute()
public String execute() {
// 獲取表單資料
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
System.out.println(username "," password "," address);
// 小龍女,XLN,古墓
// 分別向Servlet三大域中儲存資料
request.setAttribute("request_name", "request_value");
session.put("session_name", "session_value");
servletContext.setAttribute("application_name", "application_value");
return SUCCESS;
}
}