Struts2之Struts2-2.5.5 Interceptor,struts2interceptor

來源:互聯網
上載者:User

Struts2之Struts2-2.5.5 Interceptor,struts2interceptor

Struts2-2.5.5版本是目前為止最新的版本了,相對於之前的2.3版本以及再之前的版本而言,新版本改動了很多。

好了,廢話不多說,GO CODE!

基本jar包:

web.xml核心配置,這裡要注意咯!

        <!-- struts2核心控制器 -->    <filter>        <filter-name>struts2</filter-name>                <!-- 這裡要格外注意咯 -->        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>        <!-- 設定struts2預設編碼集為UTF-8 -->        <init-param>            <param-name>struts.il8.encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <!-- 設定struts.xml檔案位置 -->        <init-param>            <param-name>filterConfig</param-name>            <param-value>classpath:struts.xml</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>
web.xml

接下來是struts.xml

<struts>    <!-- 設定預設編碼集為UTF-8 -->    <constant name="struts.il8n.encoding" value="UTF-8" />    <!--設定開發人員模式 -->    <constant name="struts.devMode" value="true" />    <!-- 當 struts的設定檔修改後,系統是否自動重新載入該檔案,預設值為false(生 產環境下使用),開發階段最好開啟 -->    <constant name="struts.configuration.xml.reload" value="true" />    <!--設定主題 -->    <!-- <constant name="struts.ui.theme" value="simple" /> -->    <!-- 該 屬性指定需要Struts 2處理的請求尾碼,該屬性的預設值是action,即 所有匹配*.action的請求都由Struts 2處理。如         果使用者需要指定多個請求尾碼,則多個尾碼之間以英文逗號(,)隔開 -->    <constant name="struts.action.extension" value="action," />    <!-- 設定是否支援動態方法引動過程,true為支援,false不支援. -->    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <!-- 設定瀏覽器是否緩衝靜態內容,預設值為true(生產環境下使用),開發階段最好關閉 -->    <constant name="struts.serve.static.browserCache" value="false" />    <constant name="struts.convention.default.parent.package" value="common"/>    <!-- 公用package -->    <package name="common" namespace="/" extends="struts-default">        <!-- 配置攔截器 -->        <interceptors>            <!-- 註冊攔截器 -->            <interceptor name="loginInterceptor" class="com.Struts2Dashboard.action.LoginInterceptor" />            <!-- 引用已經註冊了攔截器,形成自訂攔截器棧,自訂攔截器棧,將覆蓋到預設棧 ,必須手動再自訂攔截器棧中 引用預設棧 -->            <interceptor-stack name="loginStack">                <!-- 預設 -->                <interceptor-ref name="defaultStack"></interceptor-ref>                <!-- 自訂 -->                <interceptor-ref name="loginInterceptor"></interceptor-ref>            </interceptor-stack>        </interceptors>        <!-- 修改當前<package>預設棧,修改後package下的所有的action都將使用此棧 -->        <default-interceptor-ref name="loginStack"></default-interceptor-ref>        <!-- 全域結果集 -->        <global-results>            <!-- 沒有登陸 -->            <result name="noLogin">/WEB-INF/pages/sweet/404.jsp</result>        </global-results>    </package></struts>
struts.xml

可有可無,不要怕麻煩,前期是為了後期維護做準備的。

bean就不在粘貼了哈,需要的話,可以評論留言哈。

/** * 登入攔截器action */@SuppressWarnings("serial")public class LoginInterceptor implements Interceptor {    //初始化        public void init() {    }    //攔截    public String intercept(ActionInvocation invocation) throws Exception {                /*if(LoginAction.class == invocation.getAction().getClass()){              return invocation.invoke();          }*/        Object action = invocation.getAction();        if (action instanceof LoginAction) {              //如果是LoginAction,則進行執行,即不做攔截            return invocation.invoke();          }        //判讀session是否存在使用者        User user =  (User) ActionContext.getContext().getSession().get("user");        System.out.println(user);                if(user == null){            System.out.println("action");            return "noLogin";        }        //允許存取        return invocation.invoke();    }    //銷毀    public void destroy() {    }}
LoginInterceptor
public class LoginAction extends ActionSupport implements ModelDriven<User> {    private User user = new User();    @Override    public User getModel() {        // TODO Auto-generated method stub        return user;    }    @Action(value="login")    public String execute() throws Exception {        ActionContext actionContext = ActionContext.getContext();        if ("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())) {            actionContext.getSession().put("user", user);            System.out.println("success");            return "success";        } else {            System.out.println("input");            return "input";        }    }}
LoginAction

只是部分關鍵性代碼,需要項目的話,評論處留下連絡方式。

沒有考慮session到期的情況哈,只是最基礎的登入攔截器而已,請多多指教謝謝。

轉載請註明出處,謝謝!未經授權請勿私自對本文相關圖片進行添加浮水印!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.