大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
          Java,Php,Shell,Python,服務(wù)器運(yùn)維,大數(shù)據(jù),SEO, 網(wǎng)站開發(fā)、運(yùn)維,云服務(wù)技術(shù)支持,IM服務(wù)供應(yīng)商, FreeSwitch搭建,技術(shù)支持等. 技術(shù)討論QQ群:428622099
          隨筆 - 238, 文章 - 3, 評(píng)論 - 117, 引用 - 0
          數(shù)據(jù)加載中……

          struts2中如何獲取Session,HttpServletRequest,HttpServletResponse

          1,How do we get access to the session?

          You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.

          Ask the ActionContext

           

          Map attibutes = ActionContext.getContext().getSession();

           

          Implement SessionAware

          The session attributes are available on the ActionContext instance, which is made available via ThreadLocal. _Preferred_

          • Ensure that servlet-config Interceptor is included in the Action's stack.
            • The default stack already includes servlet-config.
          • Edit the Action so that it implements the SessionAware interface.
            • The SessionAware interface expects a setSession method. You may wish to include a companion getSession method.
          • At runtime, call getSession to obtain a Map representing the session attributes.
          • Any changes made to the session Map are reflected in the actual HttpSessionRequest. You may insert and remove session attributes as needed.
          • Map parameters = this.getSession();
          To unit test a SessionAware Action, create your own Map with the pertinent session attributes and call setSession as part of the test's setUp method.


          2,How can we access the HttpServletRequest

          You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred.

          Ask the ActionContext

          The request is available on the ActionContext instance, which is made available via ThreadLocal.
          HttpServletRequest request = ServletActionContext.getRequest();

          Implement ServletRequestAware

          Preferred

          • Ensure that servlet-config Interceptor is included in the Action's stack.
            • The default stack already includes servlet-config.
          • Edit the Action so that it implements the ServletRequestAware interface.
            • The ServletRequestAware interface expects a setServletRequest method. You may wish to include a companion getServletRequest method.
          • At runtime, call getServletRequest to obtain a reference to the request object.
          It is more difficult to test Actions with runtime dependencies on HttpServletRequest. Only implement ServletRequestAware as a last resort. If the use case cannot be solved by one of the other servet-config interfaces (ApplicationAware, SessionAware, ParameterAware), consider whether an custom Interceptor could be used instead of Action code. (Review how servlet-config works for examples of what can be done.)


          3,How can we access the HttpServletResponse

          You can obtain the request by asking the ActionContext or implementing ServletResponseAware. Implementing ServletResponseAware is preferred.

          Ask the ActionContext


          The response is available on the ActionContext instance, which is made available via ThreadLocal.
          HttpServletResponse response = ServletActionContext.getResponse();

          Implement ServletResponseAware


          Preferred

          • Ensure that servlet-config Interceptor is included in the Action's stack.
            • The default stack already includes servlet-config.
          • Edit the Action so that it implements the ServletResponseAware interface.
            • The ServletResponseAware interface expects a setServletResponse method. You may wish to include a companion getServletResponse method.
          • At runtime, call getServletResponse to obtain a reference to the response object.
          t is more difficult to test Actions with runtime dependencies on HttpServletReponse. Only implement ServletResponseAware as a last resort. A better approach to solving a use case involving the response may be with a custom Result Type.

          posted on 2009-03-29 16:40 草原上的駱駝 閱讀(3774) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA框架

          主站蜘蛛池模板: 凯里市| 额济纳旗| 武胜县| 唐山市| 石景山区| 谢通门县| 东乌珠穆沁旗| 安达市| 邹平县| 体育| 高邮市| 茶陵县| 威远县| 株洲市| 木兰县| 鸡东县| 石台县| 大石桥市| 黄平县| 西平县| 金寨县| 天津市| 湄潭县| 乾安县| 且末县| 监利县| 兴隆县| 红安县| 桦甸市| 高唐县| 宕昌县| 濮阳县| 区。| 泗阳县| 浏阳市| 罗江县| 开阳县| 亚东县| 安西县| 阳城县| 石家庄市|