Action訪問ServletAPI
          struts2的一個重大改良之處就是與ServletAPI的解耦。不過,對于Web應用而言,不訪問ServletAPI幾乎是不可能的。例如跟蹤HTTPSession的狀態。Struts2框架提供了一種輕松的方式來訪問ServletAPI。通常需要訪問的對象是HttpServletRequest,HttpServletSession,ServletContext,這三類也代表了JSP的內置對象中的request,session,application.

          方法有:
          1 Object get(Object key):類似于條用HttpServletRequest的getAttribute(String name)
          2 Map getApplication:返回對象為map,模擬了ServletContext。
          3 Static ActionContext().getContext(),獲取ActionContext實例。
          4 Map getParameters()
          5 void setApplication(Map application)
          6 void setSession(Map session)
          用一個2方法,獲取ActionContext實例,通過該對象的getApplication()和getSession()的put(key,value)方法,實現訪問ServletAPI。

          一個用ActionContext().getContext()獲取ServletContext的例子
          index.jsp

          <%@page language="java" contentType=" text/html; charset=GBK"%>
          <%@ taglib prefix="s" uri="/struts-tags"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
              
          <head>
                  
          <title>struts2.demo1</title>
              
          </head>

              
          <body>
                  
          <s:form action="login">
                      
          <s:textfield name="username"/>
                      
          <s:password name="password"/>
                      
          <s:submit />
                  
          </s:form>
              
          </body>
          </html>

          welcome.jsp

          <%@page language="java" contentType=" text/html; charset=GBK"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head><title>struts2.demo1</title></head>
            
            
          <body>
            本站訪問次數:${applicationScope.counter }
          <br>
            ${sessionScope.user },你已經登陸
          <br>
            ${requestScope.tip }
            
          </body>
          </html>

          LoginAction類的execute()
          public String execute() throws Exception {
                  ActionContext ctx
          =ActionContext.getContext();
                  Integer counter
          =(Integer) ctx.getApplication().get("counter");
                  
          if(counter==null){
                      counter
          =1;
                  }
          else{
                      counter
          =counter+1;
                  }

                  ctx.getApplication().put(
          "counter", counter);
                  ctx.getSession().put(
          "user", getUsername());
                  
          if(getUsername().equals("ming")&&getPassword().equals("123456")){
                      setTip(
          "歡迎您,"+getUsername());
                      
          return SUCCESS;
                  }
          else{
                      
          return ERROR;
                  }


          另外,

          雖然Struts2提供了ActionContext來訪問ServletAPI,但是并不能直接獲得ServletAPI的實例。但是Struts2提供了一下接口,
          1 ServletContextAware:實現該接口的Action可以直接訪問ServletContext實例。
          2 ServletRequestAware:實現該接口的Action可以直接訪問HttpServletRequest實例。
          3 ServletResponseAware
          例如
          Action類
          public class LoginAciton implements Action,ServletResponseAwre{
           private HttpServletResponse response;
           private String username;
           private String password;
           ....//setter getter
           public void serServletResponse(HttpServletResponse response){
            this.response=response;
           }
           public String execute() throws Exception{
            Cookie c = new Cookie("user",getUsername);
            c.setMaxAge(60*60);
            response.addCookie(c);
            return SUCCESS;
           }
          }
          //通過HttpServletResponse為系統添加Cookies對象。
          jsp頁面
          <body>
          從系統中讀出Cooki值:${cookies.user.value}<br>
          </body>

          =======雖然可以在Action中獲得HttpServleResponse對象,但是希望通過它來生成服務器相應是不可能的。即使在Struts2中獲得了HttpServletResponse對象,也不要嘗試直接在Action中對客戶端生成相應。沒有任何實際意義。

          posted on 2009-11-05 01:03 南朝 閱讀(238) 評論(0)  編輯  收藏 所屬分類: Struts2

          統計

          主站蜘蛛池模板: 西贡区| 辽中县| 平塘县| 桓台县| 宁河县| 万安县| 广河县| 孟州市| 德庆县| 汉沽区| 沿河| 牙克石市| 崇仁县| 田阳县| 焦作市| 安阳县| 安阳市| 泰顺县| 苏尼特右旗| 西盟| 丰台区| 彩票| 西充县| 宝丰县| 昌平区| 乐至县| 濉溪县| 广平县| 威信县| 青浦区| 图片| 苏尼特右旗| 宣城市| 马尔康县| 云林县| 文登市| 兴仁县| 东乡族自治县| 鄂托克旗| 济阳县| 咸宁市|