How to intercept the navigation between Struts 2 Actions?
I´m using Struts 2. Is it possible to 开发者_运维知识库identify and intercept when a user navigates do another Action?
I need to identify if the user is going to an Action different then the one he is currently on.
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.
精彩评论