https://blog.csdn.net/hebiao100/article/details/7385055
struts2添加request、session、application属性
第一种方法:
ActionContext ctx = ActionContext.getContext(); ctx.put("r1", list);//向request中添加属性 ctx.getSession().put("s1",list);//向session中添加属性 ctx.getApplication().put("a1", list);//向application中添加属性
ctx.put("names", Arrays.asList("小航,小王,小韩"));//向request中添加属性
HttpSession session =null; session = ServletActionContext.getRequest().getSession(); if(session.getAttribute("user") != null) { session.removeAttribute("user"); } session.setAttribute("user", user); Map<String,Object> session = null; session = ActionContext.getContext().getSession(); if(session.containsKey("user")){ session.remove("user"); } session.put("user", user);
第二种方法:
使用 ServletRequestAware接口
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(); } }
struts2访问request、session、application属性
第一种方法:
使用struts tag:
<s:property value="#request.r1"/> <s:property value="#session.s1"/> <s:property value="#application.a1"/>
第二种方法:
使用jsp对象
<%=request.getAttribute("r1") %> <br /> <%=session.getAttribute("s1") %> <br /> <%=application.getAttribute("a1")%> <br />
<c:forEach items="${names}" var="name">
${name}
</c:forEach>