springmvc quartz maven web

1、首先把目录结构列出来(这里项目是建立在springmvc demo项目上的:点击打开链接):

springmvc quartz maven web_第1张图片


2、pom文件:


  4.0.0
  timertask
  timertask
  0.0.1-SNAPSHOT
  war
  
    src
    
      
        maven-compiler-plugin
        3.3
        
          1.7
          1.7
        
      
      
        maven-war-plugin
        2.6
        
          WebContent
          false
        
      
    
  
  
      
      
        jstl  
        jstl  
        1.2  
      
      
        org.springframework  
        spring-web  
        3.1.1.RELEASE  
      
      
        org.springframework  
        spring-webmvc  
        3.1.1.RELEASE  
      
      
		org.springframework  
		spring-context-support  
		3.1.1.RELEASE  
    
    
		org.springframework
		spring-tx
		3.1.1.RELEASE
	
      
		org.quartz-scheduler  
		quartz  
		1.8.4  
      
      
  


3、web.xml 文件:

  
  
  
    Spring3MVC  
      
        spring  
        org.springframework.web.servlet.DispatcherServlet  
        1  
      
  
      
        spring  
        *.html  
       
  
       
        index.jsp  
       


4、spring-servlet.xml 文件:




	  
	
		
		
		
	

	
	
	
		
			
		
		  
			printMethod
		
	

	
	
		
		
	

	
	
		
			
				
			
		
	



5、index.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>  
  
  
  
 Spring 3.0 MVC demo  
  
  
 Say Hello  
  


6、hello1.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
  
  
  
  
Insert title here  
  
  

hello1



7、timerController.java :

package com.busymonkey;

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
public class timerController {   
    @RequestMapping("/hello")  
    public ModelAndView hello() {   
        String message = "Hello World, Spring 3.0!";  
        System.out.println(message);  
        return new ModelAndView("hello1", "message", message);  
    }   
}


8、timerTask.java :

package com.quartz;

public class timerTask {
	public void printMethod() {
		System.out.println("hello!");
	}
}



这里说一下遇到的问题:

1、少依赖:


		org.springframework
		spring-tx
		3.1.1.RELEASE
	

如果没有加这个依赖,就会报这个错误:

严重: Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.quartz] for bean with name 'timerTaskJob' defined in 

ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.quartz



2、在bean配置的时候,如果没有选中要定时运行的类所在完整包的路径(加上类名),会报错:

严重: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException
	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
	at java.lang.Class.getDeclaredConstructors(Class.java:1906)

springmvc quartz maven web_第2张图片




补:上面关于quartz配置的添加是直接放在spring-servlet.xml中的,如果想要和此文件分开,那么就在同样是 /WEB-INF 目录下新建一个 quartz-config.xml 的文件内容如下:




	
	
		
			
		
		  
			printMethod
		
	

	
	
		
		
	

	
	
		
			
				
			
		
	



然后在 web.xml 文件中加入监听此文件的配置,内容如下:

  
  
  
    Spring3MVC  
      
        spring  
        org.springframework.web.servlet.DispatcherServlet  
        1  
      
    
	
    	contextConfigLocation
    	
      		/WEB-INF/quartz-config.xml
    	
  	
  	
  	  
        org.springframework.web.context.ContextLoaderListener  
    
  
      
        spring  
        *.html  
       
  
       
        index.jsp  
       



你可能感兴趣的:(Spring)