Action 访问 Servlet API 的方式
这里只针对 Servlet 中常用的,如 HttpServletRequest、HttpServletResponse 和 HttpSession 来展开。一般来说,在 Struts2 中访问 Servlet API 的方式有2种:
(1) 直接访问 Servlet API。这将会使得你的 Action 类与 Servlet API 耦合在一起。
(2) 通过 Struts2 API 来访问 Servlet API,这样 Action 类就不会再跟 Servlet API 耦合。
通过 ServletActionContext 来访问 Servlet API ( 耦合 )
package fan.tutorial.action;
import com.opensymphony.xwork2.Action;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
public class ServletApiAction implements Action {
private HttpSession session;
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception {
request = ServletActionContext.getRequest();
session = request.getSession();
response = ServletActionContext.getResponse();
request.setAttribute("requestScopeParam", "fan");
session.setAttribute("sessionScopeParam", "tutorial");
response.setHeader("expires", "-1");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
return SUCCESS;
}
}
import com.opensymphony.xwork2.Action;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
public class ServletApiAction implements Action {
private HttpSession session;
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception {
request = ServletActionContext.getRequest();
session = request.getSession();
response = ServletActionContext.getResponse();
request.setAttribute("requestScopeParam", "fan");
session.setAttribute("sessionScopeParam", "tutorial");
response.setHeader("expires", "-1");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
return SUCCESS;
}
}
通过 ServletRequestAware、ServletResponseAware 接口来访问 Servlet API ( 耦合 )
package fan.tutorial.action;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class IServletApiAction implements Action, ServletRequestAware, ServletResponseAware {
private HttpSession session;
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception {
request.setAttribute("requestScopeParam", "fan");
session.setAttribute("sessionScopeParam", "tutorial");
response.setHeader("expires", "-1");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class IServletApiAction implements Action, ServletRequestAware, ServletResponseAware {
private HttpSession session;
private HttpServletRequest request;
private HttpServletResponse response;
public String execute() throws Exception {
request.setAttribute("requestScopeParam", "fan");
session.setAttribute("sessionScopeParam", "tutorial");
response.setHeader("expires", "-1");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
通过 ActionContext 来访问 Servlet API ( 解耦 )
package fan.tutorial.action;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class StrutsServletApiAction implements Action {
private Map<String, Object> session;
private Map<String, Object> request;
private ActionContext context = ActionContext.getContext();
public String execute() throws Exception {
session = context.getSession();
request = (Map<String, Object>)context.get("request");
request.put("requestScopeParam", "fan");
session.put("sessionScopeParam", "tutorial");
return SUCCESS;
}
}
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class StrutsServletApiAction implements Action {
private Map<String, Object> session;
private Map<String, Object> request;
private ActionContext context = ActionContext.getContext();
public String execute() throws Exception {
session = context.getSession();
request = (Map<String, Object>)context.get("request");
request.put("requestScopeParam", "fan");
session.put("sessionScopeParam", "tutorial");
return SUCCESS;
}
}
通过 RequestAware、SessionAware 接口来访问 Servlet API ( 解耦 )
package fan.tutorial.action;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
public class IStrutsServletApiAction implements Action, RequestAware, SessionAware {
private Map<String, Object> session;
private Map<String, Object> request;
public String execute() throws Exception {
request.put("requestScopeParam", "fan");
session.put("sessionScopeParam", "tutorial");
return SUCCESS;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
Struts2 对 HttpServletRequest 和 HttpSession 做了一层封装,使用 Map<String, Object> 来替换掉这2种对象,这样可避免 Action 类与 Servlet API 耦合在一起。
import java.util.Map;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
public class IStrutsServletApiAction implements Action, RequestAware, SessionAware {
private Map<String, Object> session;
private Map<String, Object> request;
public String execute() throws Exception {
request.put("requestScopeParam", "fan");
session.put("sessionScopeParam", "tutorial");
return SUCCESS;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
文章示例源码下载
struts-servlet.zip
http://www.blogjava.net/fancydeepin/archive/2014/03/17/struts-servlet.html
[ 随笔均原创,转载请注明出处:http://www.blogjava.net/fancydeepin ]