SpringMVC integrates DWR (run Maven project + jetty plug-in)

Source: Internet
Author: User

SpringMVC maven Project Creation for reference this article: http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357619.html


Create a SpringMVC project as follows:





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
       
    
 
By default, Spring-mvc finds the name under the WEB-INF directory without $ {servlet-name}-context. xml configuration file. The configuration file required by spring-mvc is directed to the src \ resources \ spring \ web directory through the configuration of contextconfiglocation.


3. servlet-context.xml

 
     
      
          
           
           
       
  
 

4. Controller class 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:

You can download jetty-distribution-7.6.12.v201302.16.zip, which contains this file.


7. run mvn jetty: run to start the project and access http: // localhost: 7878/springmvc/helloworld. action. You can see Hello World!


After creating a SpringMVC project, integrate DWR

See this article: http://blog.csdn.net/geloin/article/details/7537148


1. Import the DWR package and other required packages. 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. Configure dwr in web. xml. You only need to add the dwr url-mapping under the DispatcherServlet configuration. The modified DispatcherServlet configuration is as follows:

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

3. Add the dwr configuration in the servlet-context.xml, the modified file is as follows:

 
 
  
  
  
  
   
   
   
  
  
  
   
   
    
    
    
    
     
     
     
      
       
         DwrController
       
      
     
    
    
     
     
    
    
     
     
    
    
    
   
  
 

4. Controller class:

Package com. kxw. dwr. model; import java. util. arrayList; import java. util. list; import org. directwebremoting. annotations. remoteMethod; import org. directwebremoting. annotations. remoteProxy; import org. springframework. stereotype. controller; @ Controller @ RemoteProxy (name = "MyDwr") public class MyDwr {@ RemoteMethodpublic String hello (String world) {System. out. println ("hello" + world); return "hello" + world ;}@ RemoteMethodpublic User load () {User user = new User (10, "Messi ", new Group (1, "Bacelona FC"); return user ;}@ RemoteMethodpublic List
 
  
List () {List
  
   
Users = new ArrayList
   
    
(); Users. add (new User (7, "Ronaldo", new Group (3, "Real Madrid"); users. add (new User (11, "Ozil", new Group (8, "Asenal"); users. add (new User (20, "Van Persie", new Group (2, "Manchester United"); users. add (new User (9, "Torress", new Group (5, "Chelsea"); return users ;}@ RemoteMethodpublic void add (User user) {System. out. println (user) ;}@ RemoteMethodpublic void deleteUser () {throw new MyException ("delete user !! ") ;}@ RemoteMethodpublic int add (int a, int B) {return a + B ;}}
   
  
 

package com.kxw.dwr.model;public class User {private int id;private String username;private Group group;public User(int id, String username, Group group) {super();this.id = id;this.username = username;this.group = group;}public User() {super();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Group getGroup() {return group;}public void setGroup(Group group) {this.group = group;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", group=" + group+ "]";}}

package com.kxw.dwr.model;public class Group {private int id;private String name;public Group(int id, String name) {super();this.id = id;this.name = name;}public Group() {super();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Group [id=" + id + ", name=" + name + "]";}}

5. dwr. jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
 Insert title here<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script><script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script><script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script><script type="text/javascript">MyDwr.list(listUser);function listUser(users){for(var i=0;i
 

6. run the project run: jetty

Enter http: // localhost: 7878/springmvc_dwr/dwr. jsp in the browser


Show four dialog boxes consecutively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.