在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;

/*****************************************************************************
/**
 * Program Name: 拦截企业模块所有Action(共通)(DisplayInterceptor.java)
 * System: softJob中国软件人才网

 * Sub System: 系统共通模块

 * Description : 主要用于软件人才招聘拦截企业模块所有Action(共通)
 * @author 荣大建/嘉兴晟峰
 * @version 2008.10.02
 * @since 1.00
 */
//*****************************************************************************
public class DisplayInterceptor implements ActionHaveForwardInterceptor {

public ActionForward afterAction(Action arg0, ActionMapping arg1, ActionForm arg2,
HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
// TODO Auto-generated method stub
return null ;
}

/**
* 企业模块Action拦截器
* @param action
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
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中实现拦截器的功能啦!

(小弟也是第一次接触这个,如果有什么地方说得不对的,希望各位多多包涵,并加以指正,谢谢!)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值