|
一:訪問(wèn)或添加request/session/application屬性
復(fù)制代碼 代碼如下:
public String scope() throws Exception{
ActionContext ctx = ActionContext.getContext();
ctx.getApplication().put("app", "應(yīng)用范圍");//往ServletContext里放入app
ctx.getSession().put("ses", "session范圍");//往session里放入ses
ctx.put("req", "request范圍");//往request里放入req
return "scope";
}
JSP:
復(fù)制代碼 代碼如下:
<body>
${applicationScope.app} <br>
${sessionScope.ses}<br>
${requestScope.req}<br>
</body>
二:獲取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse對(duì)象
方法一,通過(guò)ServletActionContext.類直接獲取:
復(fù)制代碼 代碼如下:
public String rsa() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext servletContext = ServletActionContext.getServletContext();
request.getSession()
HttpServletResponse response = ServletActionContext.getResponse();
return "scope";
}
方法二,實(shí)現(xiàn)指定接口,由struts框架運(yùn)行時(shí)注入:
復(fù)制代碼 代碼如下:
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{
private HttpServletRequest request;
private ServletContext servletContext;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public void setServletResponse(HttpServletResponse res) {
this.response=res;
}
public void setServletContext(ServletContext ser) {
this.servletContext=ser;
}
}
jsp技術(shù):Struts2訪問(wèn)servlet分享,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。