Struts2中获取Servlet API中传递的参数

一、Struts2中获取Servlet API中传递的参数

        Servlet API中传递参数可以通过ServletActionContext和ActionContext。

(1)使用ServletActionContext传递参数

ServletActionContext.getRequest().setAttribute("birthday", "1984-07-20");
ServletActionContext.getRequest().getSession().setAttribute("name", "zhangsan");
ServletActionContext.getServletContext().setAttribute("count", 20000);

(2)使用ActionContext传递参数

ActionContext.getContext().getSession().put("address", "beijing");
ActionContext.getContext().getApplication().put("tel", "1881003xxxx");

(3)接收Servlet API中传递的参数

    1.使用OGNL表达式获取

<s:property value="#request.birthday" />
<s:property value="#session.name" />
<s:property value="#application.count" />
<s:property value="#session.address" />
<s:property value="#application.tel" />

    2.使用attr属性获取

<s:property value="#attr.address" />
<s:property value="#attr.address" />

二、知识扩展

(1)使用atrr属性获取注意

        使用attr属性获取Servlet API中传递的参数时,参数名不能出现重名。

(2)View获取Servlet  API参数注意

        如果是获取content中的参数,使用#参数名

        如果是获取session参数,使用#session.参数名

        如果是获取application参数,使用#application.参数名


你可能感兴趣的:(Struts2中获取Servlet API中传递的参数)