struts用户拦截器

来源:互联网 发布:新闻资讯源码 作家 编辑:程序博客网 时间:2024/06/10 00:17

1. 所谓登录;拦截器 首先 是 

                Login.action  和 业务acion 分开,

                     不能对 登录action 进行 拦截

                   必须 要 配置在不同的包,

  已经配置了的  就是 所有的 包中的action  都拦截

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>   <constant name="struts.locale" value="zh_CN" />  <constant name="struts.objectFactory" value="spring"></constant><constant name="struts.ui.theme" value="simple" />    <package name="defaultxiao" namespace="/" extends="struts-default">      <!-- 将拦截器配置进来 -->          <interceptors>              <interceptor name="checklogin" class="com.onlineTest.util.CheckLoginIntercepter"></interceptor>              <!-- 合为一个栈 -->              <interceptor-stack name="myStack">                  <interceptor-ref name="checklogin"></interceptor-ref>                  <!-- Struts默认的 -->                  <interceptor-ref name="defaultStack"></interceptor-ref>              </interceptor-stack>          </interceptors>                        <!-- 设置默认拦截器 -->          <default-interceptor-ref name="myStack"></default-interceptor-ref>                    <global-results>                  <result name="Login">/index.jsp</result>          </global-results>                      <action name="TestBook_*" class="xiaoTextBookAction" method="{1}"><result name="TextBook_index">/WEB-INF/Jsp/StudentChangeTestBookIndex.jsp</result>    <result name="error">index.jsp</result></action></package><package name="defaultxiaod" namespace="/" extends="</span><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">struts-default></span><span style="font-size:14px;"></span><pre name="code" class="html"><span style="font-size: 14px;"></span>
<action name="login_*" class="ExerciseTestAction" method="{1}">
<span style="font-size: 14px;"><result name="admin_index">/WEB-INF/Jsp/Adminindex.jsp</result></span>
</action> </package></struts>



d 2. 然后就是  在  Login_login 里面  this.getSession.setAttribute("user",user) ,

        

<span style="font-size:18px;">package com.onlineTest.util;import java.util.Map;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.onlineTest.po.Student;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class CheckLoginIntercepter extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception {HttpSessionmap= ServletActionContext.getRequest().getSession();Student ss=(Student)map.getAttribute("user");        if (ss==null)        {            return  "Login";        }        else        {          //继续执行 不拦截          return invocation.invoke();                } }}</span><span style="font-size:32px;"></span>










0 0
原创粉丝点击