Action中获取web对象
在action中获取web对象的方法有二大类:
解耦方式
- 有的时候程序需要修改,我只需要改正一部分,单是如果程序的耦合性很强的话就需要从头再写一遍很不划算,而正常的开发中都是改那部分,重写那部分,把配置文件一改就成了,java中通过接口(interface),spring技术中的ioc等实现的解耦合
耦合方式
- 耦合是指两个或两个以上的体系或两种运动形式间通过相互作用而彼此影响以至联合起来的现象。 解耦就是用将其分离开来处理问题。
解耦
- 用ActionContext获取
- -
package zking.action;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.RequestMap;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="defaul")
@Namespace(value="web_Decoupling")
public class Web_Decoupling extends ActionSupport {
private static final long serialVersionUID = 1L;
public Web_Decoupling() {
}
@Action(value="/decoupling_Scope",results={@Result(name="success",location="/success.jsp")})
public String decoupling_Scope(){
//解耦方式 取得 session application request
//取得action的 上下文
ActionContext actionContext=ActionContext.getContext();
// 在action中解耦 requets 只能,使用RequestMap 不能使用HttpServletRequest
RequestMap request=(RequestMap)actionContext.get("request");
request.put("request", "解耦 request中的值");
//取 session
Map<String,Object> session=actionContext.getSession();
session.put("session", "解耦 session中的值");
//取application
Map<String,Object> application=actionContext.getApplication();
application.put("application", "解耦 application中的值");
return "success";
}
}
- 采用,实现 RequestAware,ApplicationAware,SessionAware 重写方法获取
package zking.action;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="defaul")
@Namespace(value="web_TestDecouplingAware")
public class Web_TestDecouplingAware extends ActionSupport implements RequestAware,ApplicationAware,SessionAware {
//定义 接受 request session application 解耦的三个 map集合
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
private static final long serialVersionUID = 1L;
@Action(value="/decoupling_ScopeAware",results={@Result(name="success",location="/success.jsp")})
public String decoupling_ScopeAware(){
//设 request
request.put("request", "解耦Aware request中的值 ");
//设 session
session.put("session", "解耦Aware session中的值");
//设application
application.put("application", "解耦Aware application中的值");
//注销 解耦的session中的值
SessionMap<String,Object> seesionmap=(SessionMap<String,Object>)session;
//清除 数据
seesionmap.invalidate();
return "success";
}
@Override
public void setSession(Map<String, Object> arg0) {
//取得 解耦的session
this.session=arg0;
}
@Override
public void setApplication(Map<String, Object> arg0) {
//取得 解耦的application
this.application=arg0;
}
@Override
public void setRequest(Map<String, Object> arg0) {
//取得 解耦的request
this.request=arg0;
}
}
耦合
- 从ServletActionContext获取
package zking.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="defaul")
@Namespace(value="web_Servlet_Coupling")
public class Web_Servlet_Coupling extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value="/getServletScope",results={@Result(name="success",location="/success.jsp")})
public String getServletScope(){
//耦合方式
//取到request
HttpServletRequest request=ServletActionContext.getRequest();
//取到session
HttpSession session=request.getSession();
//取到 application
ServletContext application=ServletActionContext.getServletContext();
//设 request
request.setAttribute("request", "耦合 request中的值 ");
//设 session
session.setAttribute("session", "耦合 session中的值");
//设application
application.setAttribute("application", "耦合 application中的值");
//耦合 清除 session中的值
session.invalidate();
return "success";
}
}
- 实现xxxAware接口获取 这里的xxx泛指 作用域
- -
package zking.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value="defaul")
@Namespace(value="web_Servlet_Coupling_Aware")
public class Web_Servlet_Coupling_Aware extends ActionSupport implements ServletRequestAware,ServletContextAware{
//建立 request session application 三个作用域 属性
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value="/getServletScope",results={@Result(name="success",location="/success.jsp")})
public String getServletScope(){
//耦合方式
//设 request
request.setAttribute("request", "耦合Aware request中的值 ");
//设 session
session.setAttribute("session", "耦合Aware session中的值");
//设application
application.setAttribute("application", "耦合Aware application中的值");
return "success";
}
@Override
public void setServletContext(ServletContext arg0) {
//取到 applicaton
this.application=arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
//取到 requset
this.request=arg0;
//从请求中,取到session
session=this.request.getSession();
}
}
清除session中的值
清除session值的方式也分二种:
- 解耦
//注销 解耦的session中的值
SessionMap<String,Object> seesionmap(SessionMap<String,Object>)session;
//清除 数据
seesionmap.invalidate();
- 耦合
//耦合 清除 session中的值
session.invalidate();