The Interceptor is triggered when ajax initiates an action.

Source: Internet
Author: User

The Interceptor is triggered when ajax initiates an action.

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:

 

          
 
  
   
    
     
      
      
     
    
   
  
  
   
    
     Nologin
    
   
  
 
         
        
 
  
// Note that the above package name is inherited
  
   Message
  
 
The JS Code is as follows:

 

 

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 log on and continue to operate ...);} else {alert (followed successfully); location. reload () ;}, 'json ');}}

 

The. java class of the interceptor is as follows:

 

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 actintercepto R {/*** interceptor */private static final long serialVersionUID = 1L; @ Overridepublic String intercept (ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubSystem. out. println (the interceptor works !); 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 ({olgoin: true}); return nologin ;}}

 

The action class is as follows:

 

public class FocusAction extends BaseAction {   
// The method in action does not need to be changed. You only need to add a flag field. Note that the set method must be private boolean nologin = true;

 

Global Interceptor:

 

 
  
   
    
     
      
      
     
    
   
  
  
   
    
     
/EC/user/userLogin. jsp
       
   
  
 // The difference between this and above is that all the interceptors that need to be implemented only need to inherit the "myPackage" package.

 

 

 

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;}}


 

 

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.