13、如何去增添request/session/application的属性
通过ActionContext.getContext();就可以往这三个对象中添加属性
ActionContext ctx=ActionContext.getContext();
相当于是map
ctx.getApplication().put("app","App范围");
ctx.getSession().put("ses","Session范围");
ctx.put("req","request范围");
在页面上打印出来
${applicationScope.app}
${sessionScope.ses}
${requestScope.req}
获取HttpServletRequest/HttpSession/ServletContext/HttpServletResponse对象
方法1、通过servletActionContext类直接获取
public String rsa() throws Exception{
HttpServletRequest request=ServletActionContext.getServletContext();
request.getSession;
HttpServletResponse response=ServletActionContext.getResponese();
}
方法2、实现指定的接口,由struts2框架运行时注入
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;
}
}
建议使用第一种
public String rsa() throws Exception{
HttpServletRequest request=ServletActionContext.getRequest();
ServletContext servletContext=ServletActionContext.getServletContext();
request.getSession().setAttribute("ses","会话范围属性");
request.setAttribute("req","请求范围");
servContext.setAttribute("app","应用范围属性");
}
假设要得到对象
假设要得到站点目录下的绝对路径
servContext.getRealPath(argo)
在第一种方法中
ctx.put("names",Arrays.asList("老张","老黎","老方");
在页面中加入 把jar文件导入,jstl.jar-->lib目录下
<%@taglib uri="http://......jstl/prefix="c"%>
<c:foreach items="${name}" var="name">
${name}
<c:foreach>