Jav*_*ner 6 java struts struts2 java-ee
我使用了Struts 2框架,并创建了一个具有登录页面的Web应用程序.我有三个不同的Action类,名称Action1,Action2和Action3,以及通过在Action类中运行一些业务逻辑呈现的JSP页面的不同视图.
现在,我想检查用户是否在Action类执行处理之前已登录.所以,我在下面创建了一个拦截器工作正常.
public String intercept(ActionInvocation invocation) throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
if(session.isNew())
{
response.sendRedirect("Login.action");
}
System.out.println("Interceptor Fired");
String result = invocation.invoke();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我想要的struts.xml不是为下面的所有动作添加拦截器
<interceptor-ref name="newStack"/>
Run Code Online (Sandbox Code Playgroud)
我的struts.xml档案有
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="printMsgInterceptor" class="LoginInterceptor"></interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="printMsgInterceptor"/>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="actone" class="Action1">
<result name="success">/success.jsp</result>
<interceptor-ref name="newStack"/>
</action>
<action name="acttwo" class="Action2">
<result name="success">/success.jsp</result>
<interceptor-ref name="newStack"/>
</action>
<action name="actthree" class="Action3">
<result name="success">/success.jsp</result>
<interceptor-ref name="newStack"/>
</action>
</package>
Run Code Online (Sandbox Code Playgroud)
对于每个动作我想要写一些定义struts.xml来运行拦截器而不是手动添加
<interceptor-ref name="newStack"/>
Run Code Online (Sandbox Code Playgroud)
PSR*_*PSR 13
<interceptor name="test" class="Full path for LoginInterceptor" />
<interceptor-stack name="testStack">
<interceptor-ref name="test"/>
<interceptor-ref name="defaultStack"/> //here you are including default stack
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="testStack"></default-interceptor-ref>
Run Code Online (Sandbox Code Playgroud)
现在 testStack将执行每个请求
使用
<default-interceptor-ref name="newStack"/>
Run Code Online (Sandbox Code Playgroud)
如果您没有interceptor-ref手动为每个操作添加操作,则可以使用它default-interceptor-ref来拦截未明确定义拦截器配置的所有操作.请参阅如何配置要与每个Action一起使用的Interceptor.
我们可以创建自己的命名堆栈,甚至为包声明一个新的默认拦截器堆栈
Run Code Online (Sandbox Code Playgroud)<package name="default" extends="struts-default" > <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors>
但是,如果您说上面的拦截器工作正常,我会鼓励您对业务逻辑保持谨慎,如果第一次执行失败则不会执行登录操作.您应该检查已验证用户的结果,并将这些结果保存在您可以在拦截器中检查的会话中,而不是检查新会话.例如,请参阅此问题.
编写使用经过身份验证的用户信息的拦截器的示例,您可以在此处找到该会话.
| 归档时间: |
|
| 查看次数: |
11594 次 |
| 最近记录: |