struts2-Rights Interceptor, log Blocker, execandwait (progress bar) Interceptor Configuration

Source: Internet
Author: User

1. Permission blocker
 Packagelogin;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.struts2.ServletActionContext;Importcom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.AbstractInterceptor; Public classLogininterceptorextendsAbstractinterceptor {Private Static Final LongSerialversionuid = 1L; @Override PublicString intercept (actioninvocation ai)throwsException {//URL to get requestString URL =servletactioncontext.getrequest (). Getrequesturl (). toString (); HttpServletResponse Response=Servletactioncontext.getresponse (); Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setheader ("Cache-control", "No-store"); Response.setdateheader ("Expires", 0); System.out.println ("Execute Logininterceptor"); //Direct release of login and cancellation requests without interception        if(Url.indexof ("login.action")!=-1 | | url.indexof ("logout.action")!=-1)            {                  returnAi.invoke (); }          Else        {              //Verify that the session is out of date            if(!servletactioncontext.getrequest (). Isrequestedsessionidvalid ()) {                      //session expires, turn to session Expiration prompt page, and finally jump to login page                    return"Tologin"; }              Else{String systemuser= (String) servletactioncontext.getrequest (). GetSession (). getattribute ("User"); //Verify that you are signed in                    if(systemuser==NULL)                        {                              //not logged in, jump to login page                            return"Tologin"; }                    Else                        {                                                  returnAi.invoke (); }                                  }                      }              }}
Logininterceptor2. Log blocker
ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Map;ImportJava.util.Set;Importcom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.Interceptor;ImportCom.opensymphony.xwork2.interceptor.PreResultListener; Public classDologinterceptorImplementsInterceptor {Private Static Final LongSerialversionuid = 1L; @Override Public voiddestroy () {//TODO auto-generated Method Stub} @Override Public voidinit () {//TODO auto-generated Method Stub} Logdao LD=NewLogdao (); @Override PublicString intercept (actioninvocation ai)throwsException {Ai.addpreresultlistener (NewPreresultlistener () { Public voidBeforeresult (actioninvocation ai, String arg1) {Try {                                      Booleanparam=true; System.out.println ("Execute Dologintercepter"); Operatelog Log=NewOperatelog (); LongTime=System.currenttimemillis ();                                      Log.setid (time); SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd h:m:s"); String Operatetime=format.format (NewDate ()). ToString ();                                      Log.setoperatetime (Operatetime); Map<string, object> session =Ai.getinvocationcontext (). GetSession (); String User= (String) session.get ("User"); if(User! =NULL) {log.setoperator (user); }                                       Else{log.setoperator ("Operator:can ' t get form system"); } String Operation= "ClassName:" + ai.getaction () + ""; Operation=operation+ "MethodName:" + ai.getinvocationcontext (). GetName () + ""; Map<string, object> map =Ai.getinvocationcontext (). GetParameters (); Set<String> keys =Map.keyset (); if(keys==NULL|| Keys.size () ==0) {param=false; System.out.println ("Parameters Null"); }                                      Else{Operation=operation+ "Parameters:";  for(String Key:keys) { Operation=operation+key + "=" + ((object[]) Map.get (key)) [0]+ "#"; }} Operation=operation+ "Executeresult:" + ai.getresultcode () + "";                                      Log.setoperation (operation); if(param) { (log); System.out.println ("Print the Log object:" +log); }                              }                                                             Catch(Exception e) {e.printstacktrace ();                      }                                        }                      }); returnAi.invoke (); }    }
Dologinterceptor3.execAndWait for struts2 with Interceptor, no source code attached

Front desk with execandwait page code:

Key points are : <meta http-equiv= "Refresh" content= "3;url=generatepath.action" >

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><%@ taglib prefix= "s" uri= "/struts-tags"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://" >. query_hint{border:5px Solid #939393;    width:400px;    height:50px; Line-height:55px; padding:020px;    Position:absolute; Left:60g; Margin-left:-140px; Top:30%; Margin-top:-40px; Font-size:15px; color:#333; Font-Weight:bold; Text-Align:center; Background-color: #f9f9f9;}. Query_hint Img{position:relative;top:10px;left:-8px;}</style> class= "Query_hint" >  is executing the query, please wait ... <%--<s:property value= "complete"/>-- %> </div>indicate_waiting.jsp4. Configuration of interceptors in Struts.xml
< PackageName= "Generatepath" namespace= "/"extends= "Struts-default" > <interceptors> <!--define rights control interceptors--<interceptor name= "loginverify "class= "Login. Logininterceptor "></interceptor> <!--definition log blocker-<interceptor name=" Dolog "class= "Dolog." Dologinterceptor "></interceptor> <!--define an interceptor stack with login control and log Management--<interceptor-stack name = "Mydefaultstack" > <interceptor-ref name= "loginverify" ></interceptor-ref> <i Nterceptor-ref name= "Dolog" ></interceptor-ref> <interceptor-ref name= "Defaultstack" ></inte rceptor-ref> <interceptor-ref name= "execandwait" > <param name= "delay" >1000</           param> <param name= "Delaysleepinterval" >1000</param> </interceptor-ref> </interceptor-stack> </interceptors> <!--define default interceptors--<default-interceptor-ref name= "Mydefaultstack" ></default-interceptor-ref> <!--define global processing results-<global-results> <result name= "Tologin" Type= "redirect" >/session.jsp</result> </global-results> <action name= "Generatepath"class= "neo4j." Pathindicationaction "method=" Doindicatepath "> <result name=" Wait "&GT;/WORK/SEARCH/INDICATE_WAITING.JSP&L t;/result> <result name= "Indicateresult" >/work/search/pathIndication.jsp</result> </ac tion></ Package>
View Code5. Background implementation code slightly.

struts2-Rights Interceptor, log Blocker, execandwait (progress bar) Interceptor Configuration

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: 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.