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 南朝 閱讀(236) 評論(0)  編輯  收藏 所屬分類: Struts2

          統計

          主站蜘蛛池模板: 开远市| 蛟河市| 铅山县| 平谷区| 会昌县| 定兴县| 峡江县| 祁连县| 咸宁市| 灵台县| 安达市| 罗田县| 疏勒县| 肥乡县| 辽宁省| 潜江市| 商水县| 辰溪县| 大荔县| 恭城| 博湖县| 申扎县| 韶关市| 明星| 武安市| 略阳县| 西华县| 忻州市| 巧家县| 舞钢市| 苏尼特左旗| 大足县| 肥西县| 巴楚县| 长宁区| 那坡县| 六盘水市| 布尔津县| 淮北市| 大竹县| 体育|