0

我正在使用 Struts 2。当用户导航执行另一个操作时,是否可以识别和拦截?

我需要确定用户是否要执行与他当前正在执行的操作不同的操作。

4

1 回答 1

1

It's easy. Just use Struts 2 interceptor.

public class TestInterceptor extends AbstractInterceptor {

    public String intercept(ActionInvocation invocation) throws Exception {
        // Do checking here, get user from session or from parameter is OK.
        // Example:
        // string returned below will be redirected to result of Struts 2 action
        String roleName = user.getRole().getName();
        if ("admin".equals(roleName) {
            return "admin";
        } else if("user".equals(roleName)) {
            return "user";
        }

        // This means if roleName is not admin or user, the action will be invoked
        return invocation.invoke();
    }

}

Then just add above interceptor in struts.xml configuration. I think this is what you want, right? I always use this interceptor.

See http://struts.apache.org/2.x/docs/interceptors.html or http://www.benmccann.com/blog/struts-2-tutorial-interceptors/ for the samples.

于 2011-04-16T18:40:36.057 回答