2012-11-12 35 views

回答

9

你爲什麼要這樣做?

這且不說這裏是你如何能做到這一點...

//Following has not been tested 
package com.quaternion.interceptor; 

import com.opensymphony.xwork2.Action; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 
import javax.servlet.http.HttpServletRequest; 
import org.apache.struts2.ServletActionContext; 

public class PostOnlyInterceptor extends AbstractInterceptor { 
    @Override 
    public String intercept(ActionInvocation ai) throws Exception { 
     HttpServletRequest request = ServletActionContext.getRequest(); 
     if (!request.getMethod().equals("POST")){ 
      return Action.ERROR; 
     } 
     return ai.invoke(); 
    } 
} 

然後建立一個特定包的攔截器堆棧,並把你的行動在包裝或註釋你的行動與包中的關聯ParentPackage註釋。