JR Galia
JR Galia

Reputation: 17269

Restrict Struts2 Action to Post Method Only

How can we restrict a Struts2 Action to work only for Post method?

Upvotes: 3

Views: 3775

Answers (1)

Quaternion
Quaternion

Reputation: 10458

Why do you want to do this?

That aside here is how you could do it...

//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();
    }  
}

Then build an interceptor stack for a particular package and put your actions in that package or with annotations associate your action with the package using the ParentPackage annotation.

Upvotes: 13

Related Questions