我选择通过Interceptor实现权限拦截,即判断session中时候存在登录的用户,如没有(即用户没有登录),直接Action.LOGIN返回到登录页面,不再调用拦截的acion方法,但返回的login页面没有任何“用户未登录”的提示信息,请问如何实现,能在登录页面显示“您未登录或是请重新登录”呢?
问题补充:
在action中可以使用类似addActionMessage()的功能,我是想知道在连接器里如何实现。
以下是拦截器中的代码,判断完是否登陆以后直接put一个message到session中。
[code="java"]package com.test.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@SuppressWarnings("serial")
public class CheckLoginInterceptor extends AbstractInterceptor {
@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map map = invocation.getInvocationContext().getSession();
if (map.get("user") == null) {
map.put("message", "please login");
return Action.LOGIN;
} else {
return Action.SUCCESS;
}
}
}
[/code]
然后在JSP也就是登陆页面中增加如下一行显示。
[code="html"]
就是用到了 struts2的标签,好好学一下吧!
:-),偶也不太熟。
不调用 execute 直接返回即可。
[code="java"]
class="com.core.interceptor.PrivilegeInterceptor"/>
<interceptor-stack name="my-stack" >
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="privilegeInterceptor"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="my-stack"/>
<global-results>
<result name="notpermission">/无权限.jsp</result>
<result name="message">/消息.jsp</result>
<result name="error">/错误.jsp</result>
</global-results>
</package>[/code]写个自己定义的拦截器就可以了