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

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


建立一个SpringMVC项目如下:

\

\\

\


1.pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   <modelversion> 4.0 . 0 </modelversion>
   <groupid>com.kxw.web</groupid>
   springmvc</artifactid>
   <packaging>war</packaging>
   <version> 0.0 . 1 -SNAPSHOT</version>
   <name>springmvc Maven Webapp</name>
   <url>http: //maven.apache.org</url>
   
   <repositories>
         <repository>
             <id>central</id>
             <name>Central Repository</name>
             <url>https: //nexus.sourcesense.com/nexus/content/repositories/public/</url>
             <layout> default </layout>
             <snapshots>
                 <enabled> false </enabled>
             </snapshots>
         </repository>
     </repositories>
   
    <properties>
     <project.build.sourceencoding>UTF- 8 </project.build.sourceencoding>
     <org.springframework.version> 3.0 . 5 .RELEASE</org.springframework.version>
</properties>
   
   
   <dependencies>
     <dependency>
       <groupid>junit</groupid>
       junit</artifactid>
       <version> 4.10 </version>
       <scope>test</scope>
     </dependency>
     
     <dependency>
     <groupid>org.springframework</groupid>
     spring-webmvc</artifactid>
     <version>${org.springframework.version}</version>
</dependency>
             
     
   </dependencies>
   <build>
     <finalname>springmvc</finalname>
     
     <plugins>
       <plugin>
     <groupid>org.mortbay.jetty</groupid>
     jetty-maven-plugin</artifactid>
     <version> 8.1 . 14 .v20131031</version>
     <configuration>
          <scanintervalseconds> 200 </scanintervalseconds>
          <webappconfig>
               <contextpath>/springmvc</contextpath>
               <defaultsdescriptor>src/main/resources/webdefault.xml</defaultsdescriptor>
          </webappconfig>
          
             <connectors>
                <connector implementation= "org.eclipse.jetty.server.nio.SelectChannelConnector" >
                   <port> 7878 </port>
                   <maxidletime> 60000 </maxidletime>
                </connector>
              </connectors>
          
     </configuration>
</plugin>
         
     </plugins>
     
   </build>
</project>

2.web.xml:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<web-app>
   <display-name>Archetype Created Web Application</display-name>
   
   <servlet>
         <servlet-name>mvcServlet</servlet-name>
         <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
         
         <init-param> 
             <param-name>contextConfigLocation</param-name> 
             <param-value>classpath*:/servlet-context.xml</param-value> 
         </init-param> 
         <load-on-startup> 1 </load-on-startup>
     </servlet>
 
     <servlet-mapping>
         <servlet-name>mvcServlet</servlet-name>
         <url-pattern>*.action</url-pattern>
     </servlet-mapping>
   
</web-app>
Spring-mvc缺省是在WEB-INF目录下找名称未${servlet-name}-context.xml的配置文件,这里通过contextconfiglocation的配置,将spring-mvc所需的配置文件指向到src\resources\spring\web目录下。


3.servlet-context.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--?xml version= "1.0" encoding= "UTF-8" ?-->
http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.0.xsd
http: //www.springframework.org/schema/mvc
http: //www.springframework.org/schema/mvc/spring-mvc.xsd
">
     <context:component-scan base- package = "com.kxw.springmvc.controller" >
     <bean id= "viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver" >
         <property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView" >
         <property name= "prefix" value= "/WEB-INF/jsp/" >
         <property name= "suffix" value= ".jsp" >
     </property></property></property></bean>
 
</context:component-scan></beans>

4.Controller类HelloWorld.java:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:
?
1
2
3
4
5
6
7
8
9
10
11
<% @page contentType= "text/html" pageEncoding= "UTF-8" %>
 
 
 
     
         <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
         <title>JSP Page</title>
     
     
         <h1>Hello World!</h1>
    


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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
   <modelversion> 4.0 . 0 </modelversion>
   <groupid>com.kxw.web</groupid>
   springmvc</artifactid>
   <packaging>war</packaging>
   <version> 0.0 . 1 -SNAPSHOT</version>
   <name>springmvc Maven Webapp</name>
   <url>http: //maven.apache.org</url>
   
   <repositories>
         <repository>
             <id>central</id>
             <name>Central Repository</name>
             <url>https: //nexus.sourcesense.com/nexus/content/repositories/public/</url>
             <layout> default </layout>
             <snapshots>
                 <enabled> false </enabled>
             </snapshots>
         </repository>
     </repositories>
   
    <properties>
     <project.build.sourceencoding>UTF- 8 </project.build.sourceencoding>
     <org.springframework.version> 3.0 . 5 .RELEASE</org.springframework.version>
</properties>
   
   
   <dependencies>
     <dependency>
       <groupid>junit</groupid>
       junit</artifactid>
       <version> 4.10 </version>
       <scope>test</scope>
     </dependency>
     
     <dependency>
     <groupid>org.springframework</groupid>
     spring-webmvc</artifactid>
     <version>${org.springframework.version}</version>
</dependency>
             
       <dependency>
                 <groupid>org.directwebremoting</groupid>
                 dwr</artifactid>
                 <version> 3.0 .M1</version>
             </dependency>
             
 
         <dependency>
     <groupid>org.aspectj</groupid>
     aspectjweaver</artifactid>
     <version> 1.7 . 4 </version>
</dependency>
             
   </dependencies>
   <build>
     <finalname>springmvc</finalname>
     
     <plugins>
       <plugin>
     <groupid>org.mortbay.jetty</groupid>
     jetty-maven-plugin</artifactid>
     <version> 8.1 . 14 .v20131031</version>
     <configuration>
          <scanintervalseconds> 200 </scanintervalseconds>
          <webappconfig>
               <contextpath>/springmvc_dwr</contextpath>
               <defaultsdescriptor>src/main/resources/webdefault.xml</defaultsdescriptor>
          </webappconfig>
          
             <connectors>
                <connector implementation= "org.eclipse.jetty.server.nio.SelectChannelConnector" >
                   <port> 7878 </port>
                   <maxidletime> 60000 </maxidletime>
                </connector>
              </connectors>
          
     </configuration>
</plugin>
         
     </plugins>
     
   </build>
</project>


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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!--?xml version= "1.0" encoding= "UTF-8" ?-->
<web-app xmlns:web= "http://xmlns.jcp.org/xml/ns/javaee" >
   <display-name>Archetype Created Web Application</display-name>
   <servlet>
     <servlet-name>mvcServlet</servlet-name>
     <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
     <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath*:/servlet-context.xml</param-value>
     </init-param>
     <load-on-startup> 1 </load-on-startup>
   </servlet>
   <servlet-mapping>
     <servlet-name>mvcServlet</servlet-name>
     <url-pattern>*.action</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
     <servlet-name>mvcServlet</servlet-name>
     <url-pattern>/dwr/*</url-pattern>
   </servlet-mapping>
</web-app>

3. 在servlet-context.xml中添加dwr的配置,修改后的文件如下所示:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!--?xml version= "1.0" encoding= "UTF-8" ?-->
http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.0.xsd
http: //www.springframework.org/schema/mvc
http: //www.springframework.org/schema/mvc/spring-mvc.xsd
http: //www.springframework.org/schema/aop  
         http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http: //www.directwebremoting.org/schema/spring-dwr     
         http: //directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd
       
         http: //www.springframework.org/schema/tx  
         http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd 
 
">
 
     
     <mvc:annotation-driven>
 
     <context:component-scan base- package = "com.kxw.springmvc.controller" >
     <context:component-scan base- package = "com.kxw.dwr.model" >
     <bean id= "viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver" >
         <property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView" >
         <property name= "prefix" value= "/WEB-INF/jsp/" >
         <property name= "suffix" value= ".jsp" >
     </property></property></property></bean>
 
     <!-- DWR配置 -->
 
     <bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
         <property name= "order" value= "1" >
     </property></bean>
     <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >
         <property name= "order" value= "2" >
     </property></bean>
     <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
     <bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" >
     <bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
         <property name= "order" value= "3" >
         <property value= "true" name= "alwaysUseFullPath" ></property>
         <property name= "mappings" >
             <props>
                 <prop key= "/dwr/**" >dwrController</prop>
             </props>
         </property>
     </property></bean>
     <dwr:configuration>
         <dwr:convert type= "bean" class = "com.kxw.dwr.model.User" ></dwr:convert>
         <dwr:convert type= "bean" class = "com.kxw.dwr.model.Group" ></dwr:convert>
     </dwr:configuration>
     <dwr:controller id= "dwrController" debug= "true" >
         <dwr:config-param name= "allowScriptTagRemoting" value= "true" >
         <dwr:config-param name= "crossDomainSessionSecurity" value= "false" >
 
     </dwr:config-param></dwr:config-param></dwr:controller>
 
     <dwr:annotation-config id= "dwr" >
     <dwr:annotation-scan scanremoteproxy= "true" scandatatransferobject= "true" base- package = "com.kxw.dwr.model" >
 
</dwr:annotation-scan></dwr:annotation-config></bean></bean></context:component-scan></context:component-scan></mvc:annotation-driven></aop:aspectj-autoproxy></beans>

4.Controller类:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 {
 
     @RemoteMethod
     public String hello(String world){
         
         System.out.println( "hello" +world);
         
         return "hello" +world;
     }
     
     @RemoteMethod
     public User load(){
         
         User user= new User( 10 , "Messi" , new Group( 1 , "Bacelona FC" ));
         
         return user;
         
     }
     
     @RemoteMethod
     public List<user> list(){
         
         List<user> users= new ArrayList<user>();
         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;
     }
     @RemoteMethod
     public void add(User user){
         
         System.out.println(user);
     }
     @RemoteMethod
     public void deleteUser(){
         throw new MyException( "删除用户!!" );
     }
     @RemoteMethod
     public int add( int a, int b){
         
         return a+b;
     }
}
</user></user></user>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
     }
 
 
     @Override
     public String toString() {
         return "User [id=" + id + ", username=" + username + ", group=" + group
                 + "]" ;
     }
     
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
     }
 
 
     @Override
     public String toString() {
         return "Group [id=" + id + ", name=" + name + "]" ;
     }
 
     
}

5.dwr.jsp:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language= "java" contentType= "text/html; charset=UTF-8"
     pageEncoding= "UTF-8" %>
 
 
 
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
<title>Insert title here</title>
<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<users.length;i++){ alert(users[i].username+ "," +users[i].group.name);= "" }= "" <= "" script>= "" <= "" head= "" >
 
 
 
</users.length;i++){>

6.运行项目run:jetty

浏览器敲入:http://localhost:7878/springmvc_dwr/dwr.jsp

\

连续显示四个对话框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值