SpringMVC整合DWR(Maven项目+jetty插件运行)(一)

2014-11-23 23:22:25 · 作者: · 浏览: 0

SpringMVC的maven项目创建可以参考这篇文章:http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357619.html


建立一个SpringMVC项目如下:

\

\\

\


1.pom.xml

  
  
   
    4.0.0
   
  
   
    com.kxw.web
   
  
   
    springmvc
   
  
   
    war
   
  
   
    0.0.1-SNAPSHOT
   
  
   
    springmvc Maven Webapp
   
  
   
    http://maven.apache.org
   
  
  
    
     
     
      central
      
     
      Central Repository
      
     
      https://nexus.sourcesense.com/nexus/content/repositories/public/
      
     
      default
      
      
      
       false
       
      
     
   
  
   
    
    
     UTF-8
     
    
     3.0.5.RELEASE
     
   
  
  
  
    
     
     
      junit
      
     
      junit
      
     
      4.10
      
     
      test
      
     
     
     
      org.springframework
      
     
      spring-webmvc
      
     
      ${org.springframework.version}
      
     
   
  
    
    
     springmvc
     
     
      
      
       org.mortbay.jetty
       
      
       jetty-maven-plugin
       
      
       8.1.14.v20131031
       
       
       
        200
        
        
        
         /springmvc
         
        
         src/main/resources/webdefault.xml
         
        
        
         
         
          7878
          
         
          60000
          
         
        
       
      
     
   

  

2.web.xml:



  
  
   
    Archetype Created Web Application
   
  
  
    
    
     mvcServlet
     
    
     org.springframework.web.servlet.DispatcherServlet
     
     
     
      contextConfigLocation
      
     
      classpath*:/servlet-context.xml
      
     
    
     1
     
   

    
    
    
     mvcServlet
     
    
     *.action
     
   
  

  
Spring-mvc缺省是在WEB-INF目录下找名称未${servlet-name}-context.xml的配置文件,这里通过contextconfiglocation的配置,将spring-mvc所需的配置文件指向到src\resources\spring\web目录下。


3.servlet-context.xml

  

  
    
   
    
    
     
     
     
   


  

4.Controller类HelloWorld.java:

package com.kxw.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/helloworld")
public class Helloworld {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("helloworld");
        return mv;
    }
}

5.helloworld.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>



    
        
  
        JSP Page
    
    
        

Hello World!


6.webdefault.xml:

可去下载jetty-distribution-7.6.12.v20130726.zip,里面便有这个文件


7.运行mvn jetty:run 启动工程后,访问http://localhost:7878/springmvc/helloworld.action ,可以看到Hello World!


创建一个SpringMVC项目之后,接下来来整合DWR

可参考此文章:http://blog.csdn.net/geloin/article/details/7537148

\

1.导入DWR包和其他必须包,pom.xml:

  
  
   
    4.0.0
   
  
   
    com.kxw.web
   
  
   
    springmvc
   
  
   
    war
   
  
   
    0.0.1-SNAPSHOT
   
  
   
    springmvc Maven Webapp
   
  
   
    http://maven.apache.org
   
  
  
    
     
     
      central
      
     
      Central Repository
      
     
      https://nexus.sourcesense.com/nexus/content/repositories/public/
      
     
      default
      
      
      
       false
       
      
     
   
  
   
    
    
     UTF-8
     
    
     3.0.5.RELEASE
     
   
  
  
  
    
     
     
      junit
      
     
      junit
      
     
      4.10
      
     
      test
      
     
     
     
      org.springframework
      
     
      spring-webmvc
      
     
      ${org.springframework.version}
      
     
     
     
      org.directwebremoting
      
     
      dwr
      
     
      3.0.M1
      
     
     
     
      org.aspectj
      
     
      aspectjweaver
      
     
      1.7.4
      
     
   
  
    
    
     springmvc
     
     
      
      
       org.mortbay.jetty
       
      
       jetty-maven-plugin
       
      
       8.1.14.v20131031
       
       
       
        200
        
        
        
         /springmvc_dwr
         
        
         src/main/resources/webdefault.xml
         
        
        
         
         
          7878
          
         
          60000
          
         
        
       
      
     
   

  


2.在web.xml中配置dwr。只需在配置DispatcherServlet下添加dwr的url-mapping即可,修改后的DispatcherServlet配置如下所示:

  

  
  
   
    Archetype Created Web Application
   
  
    
    
     mvcServlet
     
    
     org.springframework.web.servlet.DispatcherServlet
     
     
     
      contextConfigLocation
      
     
      classpath*:/servlet-context.xml
      
     
    
     1
     
   
  
    
    
     mvcServlet
     
    
     *.action
     
   
  
    
    
     mvcServlet
     
    
     /dwr/*
     
   

  

3. 在servlet-context.xml中添加dwr的配置,修改后的文件如下所示:

  

  

	
   
	
   

	
   
	
   
	
    
     
     
     
   

	
   

	
    
     
   
	
    
     
   
	
   
	
   
	
    
     
     
     
      
      
       dwrController
       
      
     
   
	
    
     
     
   
	
    
     
     
   

	
   
	
   


  

4.Controller类:

package com