Struts2 's struts2-2.5.5 Interceptor

Source: Internet
Author: User

The struts2-2.5.5 version is the latest version so far, and the new version has changed a lot compared to the previous version 2.3 and the previous versions.

All right, nonsense, GO code!.

Basic JAR Package:

Web. XML Core configuration, here to pay attention to!

<!--STRUTS2 Core Controller--<filter> <filter-name>struts2</filter-name> <!-- Take extra care here--<filter-class>org.apache.struts2.dispatcher.filter.strutsprepareandexecutefilter</filter-class> <!--settings struts2 default encoding set UTF-8--<init-param> <param-name>struts.il8.encoding </param-name> <param-value>UTF-8</param-value> </init-param> <!--setting up St Ruts.xml file Location-<init-param> <param-name>filterConfig</param-name> < param-value>classpath:struts.xml</param-value> </init-param> </filter> <filter-mappin G> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Web. XML

Next up is struts.xml.

<struts> <!--Set the default encoding set to UTF-8---<constant name= "struts.il8n.encoding" value= "UTF-8"/> <!--Set Developer Mode--<constant name= "Struts.devmode" value= "true"/> <!--when the Struts profile is modified, the system automatically reloads the file, the default value is False     (used in production), the development phase is best opened--<constant name= "Struts.configuration.xml.reload" value= "true"/> <!--setting Theme-- <!--<constant name= "Struts.ui.theme" value= "simple"/>-<!--This property specifies the request suffix required for Struts 2 processing, the default value of which is the Act Ion, i.e. all matches *the. Action request is handled by Struts 2. If the user needs to specify multiple request suffixes, the multiple suffixes are separated by commas (,)--<constant name= "struts.action.extension" value= "action,"/> <!--Set Whether dynamic method calls are supported, True is supported, false is not supported. --<constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/> <!--set whether the browser caches static content, the default value is True ( Production environment), the development phase is best off--<constant name= "Struts.serve.static.browserCache" value= "false"/> <constant name= "Struts.convention.default.parent.package" value= "common"/> <!--public Package--< PackageName= "Common" namespace= "/"extends= "Struts-default" > <!--Configuring Interceptors--<interceptors> <!--registering interceptors--& Lt;interceptor name= "Logininterceptor"class= "com. Struts2Dashboard.action.LoginInterceptor "/> <!--reference has registered interceptors, forms a custom interceptor stack, customizes the interceptor stack, overwrites the default stack, and must manually re-customize the interceptor stack to refer to the Stack--<interceptor-stack name= "Loginstack" > <!--Default--<inter Ceptor-ref name= "Defaultstack" ></interceptor-ref> <!--Customize-<intercepto R-ref name= "Logininterceptor" ></interceptor-ref> </interceptor-stack> &LT;/INTERCEPTORS&G        T <!--Modify Current < Package> Default stack, all action under modified package will use this stack--<default-interceptor-ref name= "Loginstack" ></default-interceptor-ref> <!--global result set-<global-results> <!--not logged----& Lt;result name= "Nologin" >/WEB-INF/pages/sweet/404.jsp</result> </global-results> </ Package></struts>
Struts.xml

Optional, do not be afraid of trouble, the prophase is to prepare for the maintenance of the late.

Bean is not pasted ha, if necessary, can comment on the message ha.

/*** Login Blocker action*/@SuppressWarnings ("Serial") Public classLogininterceptorImplementsInterceptor {//Initialize     Public voidinit () {}//Intercept     PublicString Intercept (actioninvocation invocation)throwsException {/*if (Loginaction.class = = Invocation.getaction (). GetClass ()) {return Invocation.invoke (); }*/Object Action=invocation.getaction (); if(Actioninstanceofloginaction) {              //if it is loginaction, execute it, not intercept .            returnInvocation.invoke (); }        //To interpret if a user exists in the sessionUser user = (user) Actioncontext.getcontext (). GetSession (). Get ("User");                SYSTEM.OUT.PRINTLN (user); if(User = =NULL) {System.out.println ("Action"); return"Nologin"; }        //Release        returnInvocation.invoke (); }    //destroyed     Public voiddestroy () {}}
Logininterceptor
 Public classLoginactionextendsActionsupportImplementsModeldriven<user> {    PrivateUser User =NewUser (); @Override PublicUser Getmodel () {//TODO auto-generated Method Stub        returnuser; } @Action (Value= "Login")     PublicString Execute ()throwsException {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

Just a part of the key code, need the project, comment Office left contact information.

Do not consider the situation of the expiration of the session ha, is only the most basic login blocker just, please advise thank you.

Reprint please indicate the source, thank you! Do not have permission to add a watermark to the relevant pictures of this article without authorization!

Struts2 's struts2-2.5.5 Interceptor

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.