我前台是ext做的,我想实现当用户没有登陆或session失效,那么用户请求数据的时候在ActionSupport里就让其返回到一个指定页面。但是我用HttpServletResponse.sendRedirect("/error.jsp")
[b]问题补充:[/b]
return inter.invoke();
inter是什么定义?
[b]问题补充:[/b]
不好意思,看到了,我试一下。
[b]问题补充:[/b]
我已经写了,打了断点,为什么没有执行,是要在web.xml配置吗?
[b]问题补充:[/b]
我看懂了,这样是不是每一个action要引入啊?如果这样,岂不是很麻烦?
[b]问题补充:[/b]
确是进来了,不过我该了一点点东西,
[code="java"]
login
init
[/code]
我现在的问题是,includeMethods如何配置,基本上除了一个login我不用拦截以外,其他全部拦截。我不可能把所有的东西都拦截吧。
还有一点是,我在Web下的文件如何拦截防止用户直接去在浏览器中获取,
http://localhost:8084/OA2/manag/index.jsp.
init 这个不用配置,除了login的,全都会走拦截器。
另,可将JSP文件放到WEB-INF下,这样就可以了。 :wink:
加一个拦截器就可以了。
在拦截器里判断用户是否已经登录或SESSION是否有效。
[code="xml"]
<interceptor name="authentication"
class="authenticationInterceptor" />
[/code]
下面是public class AuthenticationInterceptor extends MethodFilterInterceptor中拦截方法的实现例子,请参考:
[code="java"]
protected String doIntercept(ActionInvocation inter) throws Exception {
SessionMap session = (SessionMap) ActionContext.getContext().get(ActionContext.SESSION);
Object object = session.get(USER_SESSION_KEY);
if (null == object) {
return Action.LOGIN;
}
return inter.invoke();
}
[/code]
其中Action.LOGIN为:
[code="xml"]
logout
/
login.jsp
[/code]
第一,要在struts的配置文件中配置。
第二,还要在你所要执行的Action上使用这个拦截器
给你个配置文件的例子:
[code="xml"]
class="authenticationInterceptor" />
<action name="main" class="mainAction" method="execute">
<interceptor-ref name="authentication"/>
<result name="success">/WEB-INF/pages/main.jsp</result>
</action>
[/code]
可以设置默认的拦截器/拦截器组的。
设置上default-interceptor-ref就不用为每个Action设置了。
[code="xml"]
class="authenticationInterceptor" />
<default-interceptor-ref name="authentication" />
<action name="main" class="mainAction" method="execute">
<result name="success">/WEB-INF/pages/main.jsp</result>
</action>
[/code]