取得Map類型的request、session、application、真實類型的HttpServletRequest、HttpSession、HttpServletApplication
第一種方式:在Action中取得Map類型的request、session、application 依賴於容器的方式
public class LoginAction1 extends ActionSupport{
private Map request;
private Map session;
private Map application;
//構造函數
public LoginAction1(){
request = (Map)ActionContext.getContext().get("request");
//為什麼直接在get裡面填寫request就可以取得Map類型的request了呢?這是為什麼呢
//我們可以在jsp的頁面上使用<s:debug>,就可以發現在debug中有兩部分內容
//第一部分:Value stack(值棧) 其中包含的是一些Action中的屬性、errors、fieldErrors、actionErrors等內容
//這些值可以在頁面上用<s:property value="errors.name[0]"/>取出錯誤信息 或 <s:property value="action中的屬性"/>
//第二部分:Stack Context 也就是ActionContext (action的上下文環境),其中就包括了許多的key和對應的value
//在key的取值中就有request,所以上面用get("request")的方式就會取到Map類型的request值
//還有一點需要注意,在頁面上訪問ActionContext中的值時需要使用 #key 的方式。
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute(){
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
request SUCCESS;
}
}
前台jsp頁面訪問時,可以這樣來訪問:
<s:property value="#request.r1"/> | <%=request.getAttribute("r1")%>
<s:property value="#session.s1"/> | <%=session.getAttribute("s1")%>
<s:property value="#application.a1"/> | <%=application.getAttribute("a1")%>
<s:property value="#attr.a1"/> //attr會自動搜索request,session,application范圍搜索
第二種方式: IOC方式
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;
}
}
其中的aware(得知,知道的意思),需要重寫對應的方法第三種方法:獲得HttpServletRequest、HttpSession、HttpServletApplication 依賴於容器的方式
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;
}
}
第四種方式:獲得HttpServletRequest、HttpSession、HttpServletApplication IOC方式
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();
}
}