J2EE,struts2 拦截器中获取 server
来源:互联网 发布:紫峰抢票软件 编辑:程序博客网 时间:2024/05/20 02:50
xml配置,监听器一定要在ContextLoaderListener后面
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 定时器 --><listener><listener-class>com.alpha.util.TimerListener</listener-class></listener>
推荐方法:
import java.util.Timer;import java.util.TimerTask;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.springframework.context.ApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;/** * 安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行 * @author JavaAlpha * @date 2013-10-30 13:46:15 */public class TimerListener implements ServletContextListener{private Timer timer = null;public void contextInitialized(ServletContextEvent servletContextEvent) {//在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能timer = new Timer(true);//添加日志,可在tomcat日志中查看到servletContextEvent.getServletContext().log("定时发布Timer已启动!");ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContextEvent.getServletContext());//ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());final EquipmentMaintainService emSvc = (EquipmentMaintainService) ac1.getBean("emService");//Servicetimer.scheduleAtFixedRate(new TimerTask() { public void run() { //设备维护管理--定时提醒EquipmentMaintainAction em = new EquipmentMaintainAction();System.out.println("-------定期检修提醒任务--------");em.timerTaskTbFTServiceRemind(emSvc);System.out.println("-------定期注油提醒任务--------");em.timerTaskTbFTOilingRemind(emSvc);System.out.println("-------维修工单提醒任务--------");em.timerTaskTbFTMaintainlistRemind(emSvc);System.out.println("-------报警提醒任务--------");em.timerTaskTbAlarmRemind(emSvc); }}, 30000, 2000);// 这里设定将延时每十分钟固定执行//}, 1000 * 60 * 60 * 1, 1000 * 60 * 60 * 1);// 这里设定将延时每小时固定执行 //}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行//添加日志,可在tomcat日志中查看到servletContextEvent.getServletContext().log("已经添加定时发布任务!");}public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。timer.cancel();event.getServletContext().log("定时发布定时器销毁!");}}// web.xml配置内容//<!-- 定时器 -->//<listener>//<listener-class>com.alpha.util.TimerListener</listener-class>//</listener>
/** * 拦截器方法 */@SuppressWarnings({ "unchecked", "static-access" })public String intercept(ActionInvocation invocation) {String result = null;HttpServletRequest request = ServletActionContext.getRequest();BaseAction action = (BaseAction) invocation.getAction();ServletContext sc = ServletActionContext.getServletContext();//ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);ModuleService moduleService = (ModuleService) ctx.getBean("moduleService");//模块serviceOperationLogService logService = (OperationLogService) ctx.getBean("operationService");//模块servicetry {result = invocation.invoke();// 判断是否需要写日志if (applyMethod(excludeMethods, includeMethods, invocation.getProxy().getMethod())) {this.saveLog(getModule(request, moduleService),AlphaUtil.getIP(request), action.getEmployee().getEId(), action.getEmployee().getEName(), logService);}} catch (Exception e) {e.printStackTrace();return action.SUCCESS;}return result;}
- J2EE,struts2 拦截器中获取 server
- 【Struts2】Struts2中拦截器
- Struts2拦截器获取session
- struts2拦截器中获取jsp页面的表单值
- struts2中拦截器使用
- Struts2中自定义拦截器
- struts2 中自定义拦截器
- struts2中自定义拦截器
- Struts2中自定义拦截器
- Struts2中自定义拦截器
- struts2 中 paramsPrepareParamsStack 拦截器
- struts2中拦截器的全局拦截和自定义拦截
- struts2拦截器获取请求相关内容
- struts2拦截器获取http请求参数
- struts2 Interceptor(拦截器)中获取HTTP 参数的方法
- 在Struts2拦截器中获取请求action名称及参数
- 在Struts2拦截器中获取请求action名称及参数
- struts2拦截器中 如果获取当前请求action 和请求的方法
- boost any
- Objective-C对象模型及应用
- 机房收费系统——上下机
- mint安装cx_Oracle拓展
- pat basic level 1018
- J2EE,struts2 拦截器中获取 server
- Linux————王者时代即将到来 !!!
- Resource is out of sync with the file system解决办法
- SpringMVC接收页面表单参数
- <Leetcode>Interleaving String
- velcoity使用说明:velocity基础
- vim快捷键整理
- android音乐播放器的音频焦点控制
- 开源代码分析技巧之——gdb单步调试