struts1.2中使用拦截器

要在struts1.2中使用拦截器,必须使用到以下三个包:
 1:saif-0.1.jar
2:saif-spring.jar
 3:spring.jar
把以上三个包放入到自己J2EE工程中的web-inf/lib目录下,然后建立拦截器类,如:
package cn.softjob.util;


import java.io.IOException;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.struts.saif.ActionHaveForwardInterceptor;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import cn.softjob.ap.enterNormalExitCheck.business.EnterNormalExitCheckBusiness;
import cn.softjob.ap.enterNormalExitCheck.form.EnterNormalExitCheckForm;


 
/
public ActionForward beforeAction(Action action, ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
   //获得用户请求URL
   String url = request.getRequestURL().toString();
   if (request.getQueryString() != null && !"".equals(request.getQueryString())) {
   url = url + "?" +request.getQueryString();
   }
   EnterNormalExitCheckForm enterNormalExitCheckForm = new EnterNormalExitCheckForm();
 
   //从session中取得企业用户ID


   String enterid = (String)request.getSession().getAttribute("enterprise_id") ;
 
   //向form中设置企业用户ID


   enterNormalExitCheckForm.setEnterId(enterid) ;
 
   EnterNormalExitCheckBusiness enterNormal = new EnterNormalExitCheckBusiness();
   String forward = "" ;
 
   //断断企业用户是否已经登陆
   if ((enterid == null || "".equals(enterid)) || (url.indexOf("enterprisesMain.do") != -1)) {
  
    //判断是否从登陆页面过来


    if ((url.indexOf("enterprisesLogin.do") != -1) || (url.indexOf("enterprisesMain.do") != -1)) {
   
     String enName = String.valueOf(request.getParameter("username")) ;
     request.setAttribute("enterName", enName) ;
     //得到企业用户名


     enterNormalExitCheckForm.setEnterName(enName) ;
   
     //得到企业用户密码
     enterNormalExitCheckForm.setEtnerPwd(String.valueOf(request.getParameter("password"))) ;
   
     //验证企业用户是否存在
     boolean isExit = enterNormal.checkEnterUser(enterNormalExitCheckForm, request) ;
     request.getSession().setAttribute("enterprise_id", enterNormalExitCheckForm.getEnterId()) ;
     if (isExit) {
      //验证企业用户直接从登陆画面过来


      boolean flag = enterNormal.checkNormalExitForLogin(enterNormalExitCheckForm ,request) ;
      if (flag) {
       //上次没有正常退出或者在其他地方被登陆,不能进行后续操作
       request.setAttribute("flagEnter", "noexit") ;
       //request.setAttribute("lastLoginTime", enterNormalExitCheckForm.getLastLoginTime()) ;
       forward = "notNormalExite" ;
      } else {
       request.setAttribute("enterUrl", url) ;
       forward = "successNormal" ;
      }
     } else {
      //如果此用户不存在,回到登陆页面,并显示错误提示信息


      request.setAttribute("enterUrl", url) ;
      forward = "successNormal" ;
     }
    } else {
     //如果没有登陆就转发到登陆页面
     forward = "enterLoginfailure" ;
    }
   } else {
    //验证企业用户上次是否正常退出,如果退出时间为空返回true,否则返回false
    boolean flag = enterNormal.checkNormalExitForOther(enterNormalExitCheckForm ,request) ;
    if (flag) {
     //已经正常退出,可以进行后续操作
     request.setAttribute("enterUrl", url) ;
     forward = "successNormal" ;
    } else {
     //被用户强制退出或者在其他地方被登陆,不能进行后续操作
     request.setAttribute("flagEnter", "ztexit") ;
     request.setAttribute("enterName", enterNormalExitCheckForm.getEnterName()) ;
     request.setAttribute("lastLoginTime", enterNormalExitCheckForm.getLastLoginTime()) ;
     forward = "notNormalExite" ;
    }
   }
   try {
    enterNormal.closeConnection() ;
   } catch (SQLException e) {
    e.printStackTrace();
   }
   return mapping.findForward(forward);
}


}




值得注意的是:是否正常退出验证,最好就放在这儿做,如果放到其他action中做的话,可能出现循环,我刚开始就只在这个拦截器中什么验证也没有做,只把请求转发到我自己写的一个分发式action,这个分发式action才实现了是否正常退出的验证,结果就现了死循环.最后想了想,觉得这样做不对,应该把验证放到拦截器中,如果验证通过了,则进行后续操作,反之则进入错误画面.


第二步:配置interceptor-config.xml文件,在此文件中添加要拦截的Action:

?xml version="1.0"?>


<interceptor-config>
<interceptor name="displayInterceptor" type="cn.softjob.util.DisplayInterceptor"/>


<!-- 用户登陆 -->
<action type="/enterprisesLogin">
   <interceptor name="displayInterceptor" />
</action>

<!-- 职位发布 -->
<action type="/postJob">
   <interceptor name="displayInterceptor" />
</action>
<action type="/postsManage">
   <interceptor name="displayInterceptor" />
</action>

<!-- 应聘管理 -->
<action type="/candiResume">
   <interceptor name="displayInterceptor" />
</action>
<action type="/statisticsCandidates">
   <interceptor name="displayInterceptor" />
</action>
<action type="/favoritesTalent">
   <interceptor name="displayInterceptor" />
</action>
<action type="/interviewRecords">
   <interceptor name="displayInterceptor" />
</action>
<action type="/enterpriseRecycle">
   <interceptor name="displayInterceptor" />
</action>

<!-- 无忧简历 -->
<action type="/KeyWordSearchInitalsAction">
   <interceptor name="displayInterceptor" />
</action>
<action type="/AdvancedSearchInitalsAction">
   <interceptor name="displayInterceptor" />
</action>
<action type="/enterpriseSearch">
   <interceptor name="displayInterceptor" />
</action>
   <action type="/newResumeResultAction">
   <interceptor name="displayInterceptor" />
</action>
<action type="/studentssearch">
   <interceptor name="displayInterceptor" />
</action>

<!-- 系统设置 -->
<action type="/qyInforManagement">
   <interceptor name="displayInterceptor" />
</action>
</interceptor-config>
第三步:配置struts-config.xml:
<plug-in className="net.sf.struts.saif.SAIFSpringPlugin">
    <set-property property="interceptor-config" value="/WEB-INF/interceptor-config.xml" />
</plug-in>
最后,重启服务器服务器,就可以在struts中实现拦截器的功能啦!

你可能感兴趣的:(apache,spring,struts,servlet,配置管理)