Struts2.0学习笔记 --- 访问或添加几个属性的三种方式

第一种方式:最常用到

	public String execute() {
		ActionContext ac=ActionContext.getContext();
		ac.getApplication().put("app", "application");
		ac.getSession().put("ses", "session");
		ac.put("req","context");
		
		return SUCCESS;
	}
获取时直接

    ${applicationScope.app }
    ${sessionScope.ses }
    ${requestScope.con }
第二种方式:

		HttpServletRequest request=ServletActionContext.getRequest();
		ServletContext servletContext=ServletActionContext.getServletContext();
		request.setAttribute("req", "req");	
		HttpServletResponse  response = ServletActionContext.getResponse();

第三种方式:

   实现制定接口,由struts框架运行时注入:

public class ShowWords implements ServletRequestAware, ServletResponseAware, ServletContextAware {

	private HttpServletRequest  request;
	private ServletContext servletContext;
	private HttpServletResponse response;
	
	public String execute() {
		request.setAttribute("req", "arg0");
		return "success";
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		servletContext=arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
	}

}
直接执行execute,便可把数据储存在request域中,访问方式同第一种情况。
相关文章
相关标签/搜索