【Struts2复习知识点十二】-web元素request session application等

有四种方法可以得到request session application等web元素。
方法一

LoginAction1.java

View Code
public class LoginAction1 extends ActionSupport {

private Map request;
private Map session;
private Map application;

public LoginAction1() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}

public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}


}

 

方法二

LoginAction2.java

View Code
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {

private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;

//DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}

@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}

@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}

@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}


}

 

方法三

LoginAction3.java

View Code
public class LoginAction3 extends ActionSupport {

private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}

public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}

}

 

方法四

LoginAction4.java

View Code
public class LoginAction4 extends ActionSupport implements ServletRequestAware {

private HttpServletRequest request;
private HttpSession session;
private ServletContext application;



public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}



@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}

}

 

JSP中的访问代码

View Code
<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
<s:property value="#attr.a1"/><br />
<s:property value="#attr.s1"/><br />
<s:property value="#attr.r1"/><br />


总结 :

前两种方法取得Map类型request,session,application

后两种方法真实类型 HttpServletRequest, HttpSession, ServletContext

方法1和方法3是依赖容器的。(ActionContext,ServletActionContext)

方法2和方法4是 IOC 的。具体用的时候用 方法2。

JSP中 用attr取值 是检查request session等地方 取值的,不建议用。<s:debug></s:debug>

application占资源,具体应用的时候可以通过javabean 或者数据库代替。

开发时用<s:debug></s:debug>调试跟踪request session中的值。

你可能感兴趣的:(application)