Struts2后端向前端传参

Struts2后端向前端传参 html

2016-05-05 15:32 62人阅读 评论(0) 收藏 举报前端

http://static.blog.csdn.net/images/category_icon.jpg分类:java

javaee(9) http://static.blog.csdn.net/images/arrow_triangle%20_down.jpg后端

版权声明:本文为博主原创文章,未经博主容许不得转载。session

spa

因为在Action中并不能直接诶访问Servlet API,但它提供了相关类ActionContext来访问HttpServletRequest、HttpSession和 ServletContext,因此在向前端页面传值的方法就出现了多样化。通常咱们常常使用的就是经过request、session来传值,至于 Application范围这一级别的基本上用的少。.net

1. 首先若是变量是Action的全局变量,而且定义了Setter方法,那么此时无须作任何更多工做,只要它有值在前端页面就能够取到。此时取值的方法大概有这几种:htm

  • 使用Java代码:<%= request.getParameter(“str”)%>, <%=request.getAttribute(“str”)%> 这两种方式一般不推荐,原则上html代码不用掺杂Java代码;
  • 使用EL表达式:${str};
  • 使用Struts2标签:<s:property value=”str” /> ;
  • 使用OGNL表达式:<s:property value=”#request.str”/>.

2. 对应方法体内的局部变量,咱们能够放在request里面,也能够放在session里面。可是,只有在必要的时候才放在session里面。blog

(1)放在request里面:get

a. 直接调用ActionContext提供的put方法:ActionContext.getContext().put(key, value);此时的取值方式有:

  • 使用Java代码:<%=request.getAttribute("str") %>,一样不推荐;
  • 使用EL表达式:${str };
  • 使用Struts2标签:<s:property value=”str”/>;
  • 使用OGNL表达式:<s:property value=’'#request.str”/>.

b. 使用ActionContext提供的get方法:Map request = (Map)ActionContext.getContext().get("request"); request.put(key, value);此时的取值方式有:

  • 使用Java代码:<%=request.getAttribute("str") %>,一样不推荐;
  • 使用EL表达式:${str };
  • 使用OGNL表达式:<s:property value=’'#request.str”/>.

c. 使用ServletActionContext访问HttpServletRequest获得Servlet中的 request:HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute(key, value);此时的取值方式有:

  • 使用Java代码:<%=request.getAttribute("str") %>,一样不推荐;
  • 使用EL表达式:${str };
  • 使用OGNL表达式:<s:property value=’'#request.str”/>.

(2)放在session里面:

a. 使用ServletActionContext访问HttpServletRequest获得Servlet中的request,再由request获得 session:HttpServletRequest request = ServletActionContext.getRequest();

    HttpSession session = request.getSession(); session.setAttribute(key, value);或session.putValue(key, value);(已过期,不推荐使用),此时的取值方式有:

  • 使用Java代码:<%=session.getAttribute("str") %>或<%=session.getValue("sstr") %>(与putValue对应,已过期,不推荐使用);
  • 使用EL表达式:${str };
  • 使用OGNL表达式:<s:property value=’'#session.str”/>.

b.直接使用ActionContext.getContext().getSession():这种方式取值与上面的彻底同样,再也不赘述。

相关文章
相关标签/搜索