STRUTS2 Custom Interceptor Instance-Login authorization _struts2

Source: Internet
Author: User

Version: struts2.1.6

This instance realizes the function: the user needs to specify the user name to login, the landing successfully enters the corresponding page to carry on the operation, otherwise returns to the landing page carries on the landing, when the direct Access Operation page (after the landing can visit the page) is not allowed, must return the landing page.

The code is as follows:

One, page

login.jsp

[xhtml]  View Plain Copy <%@ page language= "java"  import= "java.util.*"  pageencoding= " UTF-8 "%>      <! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >   < html>     

welcome.jsp

[XHTML] View plain copy <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!  DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Show.jsp

[xhtml]  View Plain Copy <%@ page language= "java"  import= "java.util.*"  pageencoding= " UTF-8 "%>      <! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >   < html>     

Second, Action

Loginformaction

[Java] View plain copy package com.ywjava.action;      Import Com.opensymphony.xwork2.ActionSupport;       public class Loginformaction extends Actionsupport {public String Exexcute () {return "success"; }   }

Loginaction

[java]  View Plain copy package com.ywjava.action;      import  com.opensymphony.xwork2.actioncontext;   import com.opensymphony.xwork2.actionsupport;    import com.ywjava.utils.constants;      public class loginaction  extends  actionsupport{   private string username;   private String  password;   Public string getpassword ()  {        return password;  }   Public void setpassword (String password)  {        this.password = password;  }   public  String getusername ()  {       return username;  }    Public void setusername (string username)  {        This.username = username;  }      private boolean isinvalid (string value)  {           return  (value == null | |  value.length ()  == 0);     }      public  String execute () {           system.out.println (username) ;           system.out.println (password);                        if   (Isinvalid (GetUserName ())                        return INPUT;                            if   (IsinvalID (GetPassword ())                        return INPUT;                       if (This.getusername (). Equals ("Yuewei") &&  this.getpassword (). Equals ("Yuewei") {            Actioncontext.getcontext (). GetSession (). Put (Constants.user_session,getusername ());            actioncontext.getcontext (). GetSession (). Put (Constants.pass,getpassword ());            return  "Success";        }       return  "error";  }  }  

Showaction

[Java] View plain copy package com.ywjava.action;      Import Com.opensymphony.xwork2.ActionSupport;    public class Showaction extends Actionsupport {public String execute () {return "success"; }   }

Third, Interceptor

[java]  View Plain copy package com.ywjava.interceptot;      import java.util.map ;      import com.opensymphony.xwork2.action;   import  com.opensymphony.xwork2.actioncontext;   import com.opensymphony.xwork2.actioninvocation;    import com.opensymphony.xwork2.interceptor.abstractinterceptor;   import  com.ywjava.utils.constants;      public class logininterceptor extends  abstractinterceptor {           @Override         public string intercept (actioninvocation invocation)  throws Exception  {              //  Obtaining request-related Actioncontext instances            ActionContext ctx =  Invocation.getinvocationcontext ();  &nbsP         map session = ctx.getsession ();           String user =  (String)  session.get ( constants.user_session);              //  if no landing , or login all the username is not Yuewei, return to login               if  ( User != null && user.equals ("Yuewei"))  {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("test");                return invocation.invoke ();            }               Ctx.put ("Tip",  "You haven't logged in");           return  action.login;          }     }  

Four Struts.xml

[c-sharp]  View Plain Copy <?xml version= "1.0"  encoding= "UTF-8"?>   <! doctype struts public        "-//apache software foundation// dtd struts configuration 2.1//en "       " http://struts.apache.org/ Dtds/struts-2.1.dtd ">   <struts>       <package name=" Authority " extends=" Struts-default ">                   <!--  Define an interceptor  -->            <interceptors>                <interceptor name= "authority"                     class= "Com.ywjava.interceptot.LoginInterceptor" >               </interceptor>                <!--  Interceptor Stack  -->                <interceptor-stack name= "Mydefault" >                     <interceptor-ref name= "Defaultstack"  />                    <interceptor-ref name= "authority"  />                </interceptor-stack>            </interceptors>              <!--  Define global result -->            <global-results>                <!--  Transfer to/login.jsp page  -->         when returning login view name        <result name= "Login" >/login.jsp</result>            </global-results>               <action name= "LoginForm"                 class= "Com.ywjava.action.LoginFormAction" >               <result name= "Success" >/ login.jsp</result>           </action>                        <action name= "LoGin " class=" com.ywjava.action.LoginAction ">                <result name= "Success" >/welcome.jsp</result>                <result name= "Error" >/login.jsp </result>               <result  name= "Input" >/login.jsp</result>            </action>              <action name= " Show " class=" Com.ywjava.action.ShowAction >                <result name= "Success" >/show.jsp</result>                <!--  Use this interceptor  -->               <interceptor-ref name= "Mydefault"  />            </action>                   </package>   </struts>   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.