zoukankan      html  css  js  c++  java
  • Struts2---自定义拦截器

    一:UserIfo

    1.  
      public class UserInfo {
    2.  
      private String username;
    3.  
      private String password;
    4.  
       
    5.  
      public String getUsername() {
    6.  
      return username;
    7.  
      }
    8.  
       
    9.  
      public void setUsername(String username) {
    10.  
      this.username = username;
    11.  
      }
    12.  
       
    13.  
      public String getPassword() {
    14.  
      return password;
    15.  
      }
    16.  
       
    17.  
      public void setPassword(String password) {
    18.  
      this.password = password;
    19.  
      }
    20.  
      }

    二:LoginAction

    1.  
      public class LoginAction implements Action {
    2.  
      private UserInfo user;
    3.  
       
    4.  
      public String execute() throws Exception {
    5.  
      Map<String, Object> session = ActionContext.getContext().getSession();
    6.  
      if (user != null) {
    7.  
      if (user.getUsername().equals("1") && user.getUsername().equals("1")) {
    8.  
      session.put("uname", user.getUsername());
    9.  
      return SUCCESS;
    10.  
      } else {
    11.  
      return LOGIN;
    12.  
      }
    13.  
      } else {
    14.  
      if (session != null && session.get("uname") != null) {
    15.  
      return "success";
    16.  
      } else {
    17.  
      System.out.println("自定义代码");
    18.  
      return "login";
    19.  
      }
    20.  
      }
    21.  
      }
    22.  
       
    23.  
       
    24.  
      public UserInfo getUser() {
    25.  
      return user;
    26.  
      }
    27.  
       
    28.  
      public void setUser(UserInfo user) {
    29.  
      this.user = user;
    30.  
      }
    31.  
      }
    32.  
       

    三:BookAction

    1.  
      public class BookAction implements Action {
    2.  
      public String execute() throws Exception {
    3.  
      System.out.println("bookaction");
    4.  
      return SUCCESS;
    5.  
      }
    6.  
      }
    7.  
       


    四:MyInterceptor

    1.  
      public class MyInterceptor implements Interceptor {
    2.  
      public void destroy() {
    3.  
       
    4.  
      }
    5.  
       
    6.  
      public void init() {
    7.  
      System.out.println("拦截器已经成功初始化.....");
    8.  
      }
    9.  
       
    10.  
      public String intercept(ActionInvocation invocation) throws Exception {
    11.  
      System.out.println("对象" + invocation);
    12.  
      Object action = invocation.getAction();
    13.  
      System.out.println("action===========" + action);
    14.  
      String value;
    15.  
      Map<String, Object> session = ActionContext.getContext().getSession();
    16.  
      Object name = session.get("uname");
    17.  
      String actionName = invocation.getProxy().getActionName();
    18.  
      invocation.getProxy().getNamespace();
    19.  
      System.out.println("actionNmae=============" + actionName);
    20.  
      if (actionName.equals("login")) {
    21.  
      System.out.println("================");
    22.  
      value = invocation.invoke();
    23.  
      } else if (name != null) {
    24.  
      value = invocation.invoke();
    25.  
      String method = invocation.getProxy().getMethod();
    26.  
      System.out.println("方法名=========" + method);
    27.  
      } else {
    28.  
      value = "login";
    29.  
      }
    30.  
      System.out.println("逻辑视图名====" + value);
    31.  
      return value;
    32.  
      }
    33.  
      }
    34.  
       


    五:struts-day07-02.xml

    1.  
      <struts>
    2.  
      <constant name="struts.devMode" value="true"></constant>
    3.  
      <package name="day07" namespace="/" extends="default">
    4.  
      <action name="login" class="cn.day07interceptor02.action.LoginAction">
    5.  
      <result name="success">/WEB-INF/myinter/success.jsp</result>
    6.  
      <result name="login">/WEB-INF/myinter/login.jsp</result>
    7.  
      </action>
    8.  
      <action name="book" class="cn.day07interceptor02.action.BookAction">
    9.  
      <result name="success">/WEB-INF/myinter/booksuccess.jsp</result>
    10.  
      </action>
    11.  
      </package>
    12.  
      </struts>

    
    
    

    六:struts.ml

    1.  
      <struts>
    2.  
      <constant name="struts.devMode" value="true"></constant>
    3.  
      <package name="day07" namespace="/" extends="default">
    4.  
      <action name="login" class="cn.day07interceptor02.action.LoginAction">
    5.  
      <result name="success">/WEB-INF/myinter/success.jsp</result>
    6.  
      <result name="login">/WEB-INF/myinter/login.jsp</result>
    7.  
      </action>
    8.  
      <action name="book" class="cn.day07interceptor02.action.BookAction">
    9.  
      <result name="success">/WEB-INF/myinter/booksuccess.jsp</result>
    10.  
      </action>
    11.  
      </package>
    12.  
      </struts>
    13.  
      <struts>
    14.  
      <!--支持动态方法调用-->
    15.  
      <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    16.  
      <!--修改struts.xml 不用重启容器-->
    17.  
      <constant name="struts.devMode" value="true"/>
    18.  
      <package name="default" namespace="/" extends="struts-default">
    19.  
      <interceptors>
    20.  
      <interceptor name="myInter" class="cn.happy.day07interceptor.interceptor.MyInterceptor"></interceptor>
    21.  
      <interceptor-stack name="myStack">
    22.  
      <interceptor-ref name="defaultStack"></interceptor-ref>
    23.  
      <interceptor-ref name="myInter"></interceptor-ref>
    24.  
      </interceptor-stack>
    25.  
      </interceptors>
    26.  
      <default-interceptor-ref name="myStack"></default-interceptor-ref>
    27.  
      <default-action-ref name="first"></default-action-ref>
    28.  
      <action name="first"></action>
    29.  
      <!--<global-results>
    30.  
      <result name="error">/WEB-INF/jsp/error.jsp</result>
    31.  
      </global-results>-->
    32.  
      <!-- <global-exception-mappings>
    33.  
      <exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>
    34.  
      </global-exception-mappings>-->
    35.  
      <action name="helloWord" class="cn.day01.action.TestAction01">
    36.  
      <result name="success">day01jsp/first.jsp</result>
    37.  
      </action>
    38.  
      </package>
    39.  
      <!--<include file="struts-day03.xml"></include>
    40.  
      <include file="struts-day05.xml"></include>
    41.  
      <include file="struts-day06.xml"></include>
    42.  
      <include file="struts-day04.xml"></include>
    43.  
      <include file="struts-day07.xml"></include>
    44.  
      <include file="struts-day08.xml"></include>-->
    45.  
      <include file="struts-day07-02.xml"></include>
    46.  
      </struts>
    七:login.jsp
    1.  
      <body>
    2.  
      <h2>登录</h2>
    3.  
      <s:form method="POST" action="login">
    4.  
      请输入用户名: <s:textfield name="user.username"></s:textfield> <br/>
    5.  
      <s:textfield name="user.password"></s:textfield><br/>
    6.  
      <s:submit value="登陆"></s:submit>
    7.  
      </s:form>
    8.  
  • 相关阅读:
    ORA28056:Writing audit records to Windows EventLog failed
    oracle odp.net
    C#共享内存
    JQuery EasyUI Tree和tab右键菜单实现
    Query EasyUI Tree树使用介绍
    UML类图
    Oracle 创建表空间和用户
    Oracle 11g ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务 .
    Client使用c#和odp.net连接server oracle
    WPF 4文字模糊不清晰解决方法
  • 原文地址:https://www.cnblogs.com/LWK5100/p/14231572.html
Copyright © 2011-2022 走看看