The Interceptor is triggered when Ajax initiates an action.

Source: Internet
Author: User

A problem occurred during project creation. When Ajax initiates an action and triggers the interceptor, the returned value will not initiate a jump if it does not receive the same result, the POS form t can be used for submission.


After a long struggle, I have found a method that is not very scientific. I hope that someone with good intentions will find a solution to the experts later.

The feasible solution is as follows:

The struts2 configuration is as follows:

<Span style = "font-size: 12px;"> <span style = "color: # ff0000;"> </span> <span style = "color: #330033; "> <package name =" register "namespace ="/"extends =" JSON-Default "> <interceptors> <interceptor name =" methodinterceptor "class =" nwsuaf. intercept. methodinterceptor "/> <Interceptor-stack name =" register "> <Interceptor-ref name =" defaultstack "/> <! -- Call a custom interceptor --> <Interceptor-ref name = "methodinterceptor"/> </Interceptor-stack> </interceptors> <default-interceptor-ref name = "register "/> <global-Results> <result name = "nologin" type = "JSON"> <Param name = "includeproperties"> nologin </param> </result> </Global- results> </span>
<span style="font-size:12px;color:#330033;">         </package></span>
<Span style = "font-size: 12px; color: #330033; "> <package name =" defaulddd "namespace ="/"extends =" register "> // note that the preceding package name <action name =" insertfocus "class = "nwsuaf. action. systemservice. focusaction "method =" insert "> <result name =" insertfocus "type =" JSON "> <Param name =" includeproperties "> message </param> </result> </ action> </package> </span>
The JS Code is as follows:

<Span style = "font-size: 14px;"> <span style = "color: #330033;"> function focusinsert (detailid, liketype) {var uid = document. getelementbyid ("idaaa "). value; // alert (UID); If (confirm ("are you sure you want to follow this information? ") {$. Post ("insertfocus. action ", // you can change the name of the following parameter to {" mfocus. UID ": uid," mfocus. detailid ": detailid," mfocus. liketype ": liketype}, function (result) {If (result = NULL) {alert ('System error'); Return false;} else if (result. nologin) {alert ("You have not logged on. Please continue after logging on... ");} else {alert (" followed successfully "); location. reload () ;}}, 'json') ;}</span> <span style = "color: # ff6600;" ></span> </span>

The. Java class of the interceptor is as follows:

<Span style = "font-size: 14px; color: #330033;"> package nwsuaf. intercept; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts2.servletactioncontext; import nwsuaf. bean. loginbean; import COM. opensymphony. xwork2.action; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. abstractinterceptor; public class Methodinterceptor extends actinterceptor {/*** interceptor */Private Static final long serialversionuid = 1l; @ overridepublic string intercept (actioninvocation Invocation) throws exception {// todo auto-generated method stubsystem. out. println ("Interceptor takes effect! "); Loginbean u = (loginbean) actioncontext. getcontext (). getsession (). Get (" logineduser "); If (u! = NULL) {system. Out. println ("Interceptor u! = NULL: "+ U); Return invocation. invoke ();} system. out. println ("Interceptor u = NULL:" + U); // httpservletresponse response = servletactioncontext. getresponse (); // response. getwriter (). println ("{\" nolgoin \ ": true}"); Return "nologin" ;}}</span>

The action class is as follows:

 

<span style="font-size:14px;color:#330033;">public class FocusAction extends BaseAction {   </span>
<Span style = "font-size: 14px; color: #330033;"> // do not change the method in action, you only need to add a flag field. Be sure to use the set method private Boolean nologin = true; </span>

Global Interceptor:

<Span style = "font-size: 14px; color: #330033; "> <package name =" mypackage "namespace ="/"extends =" struts-Default "> <interceptors> <interceptor name =" logininterceptor "class =" biz. clD. framework. interceptor. logininterceptor "/> <Interceptor-stack name =" mystack "> <Interceptor-ref name =" defaultstack "/> <! -- Call a custom interceptor --> <Interceptor-ref name = "logininterceptor"/> </Interceptor-stack> </interceptors> <default-interceptor-ref name = "mystack "/> <global-Results> <result name = "login">/EC/user/userlogin. JSP </result> </span> <span style = "font-size: 14px; color: # ff0000; "> // the difference between this and above is that all the interceptors that need to be implemented only need to inherit the" mypackage "package. </span> <span style =" font-size: 14px; color: #330033; "> </Global-Results> </package> </span>


<span style="color:#330033;">public class LoginInterceptor extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation arg0) throws Exception {Map session=arg0.getInvocationContext().getSession();User user=(User)session.get("user");System.out.println(user);if(user!=null){return arg0.invoke();}elsereturn Action.LOGIN;}</span><span style="color:#ff0000;">}</span>


The above is all about it. The problem is solved, but I am not satisfied with it,Hope to share your experience

Related Article

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.