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
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
   <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" ?-->
<beans xmlns= "http://www.springframework.org/schema/beans" xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
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
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
   <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" ?-->
<beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:dwr= "http://www.directwebremoting.org/schema/spring-dwr" xmlns:tx= "http://www.springframework.org/schema/tx" xsi:schemalocation="
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

连续显示四个对话框

SpringMVC整合DWR(Maven项目+jetty插件运行)相关推荐

  1. Eclipse下通过Maven的Jetty插件运行Web工程的配置,包括启动https

    摘要: 之前写了Eclipse下怎么通过 Maven 的 Tomcat 插件来运行 Web 工程.近日听说 Jetty 可以动态加载修改后的类, 为了能够更快提高开发效率, 绝对要尝试一下. 引用来源 ...

  2. 如何通过Maven的Jetty插件运行Web工程

    From: https://blog.wuwii.com/maven-jetty.html Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行 ...

  3. Eclipse运行maven的jetty插件内存溢出解决

    系统运行在MAVEN中的jetty插件下,当在ECLIPSE运 clean jetty:run时系统提示 OutOfMemoryError: PermGen space. 解决办法: 设置run as ...

  4. Jetty插件运行报500错误

    Jetty插件运行报500错误 在配置jetty,点击运行后,发现web页: 百度后发现是因为项目运行jdk版本高(jdk13.0.2) 所以找到了jdk1.8.0_231版本,配置环境变量后,修改项 ...

  5. 解决maven项目打包后运行jar,提示没有主清单属性

    问题: Maven项目生成jar运行时提示"没有主清单属性" 解决: pom文件添加一下配置 <build><plugins><plugin>& ...

  6. maven项目如何打包运行指定java程序(maven-shade-plugin插件的使用)

    其实maven项目的打包就是将项目代码打包成可执行文件,在maven中有默认的打包插件,但是想要运行指定java程序就要使用maven-shade-plugin插件 maven-jar-plugin是 ...

  7. springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】

    项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  8. java maven jetty_maven jetty 插件使用

    本机环境 JDK 7 Maven 3.2 Jetty 9.2 Eclipse Luna pom.xml 配置 在你的 pom.xml 文件中添加 jetty 插件的描述信息(查看Jetty更多的版本信 ...

  9. maven项目打包插件:将maven项目打包成一个可执行的jar(瘦jar)

    通过maven-dependency-plugin插件和maven-jar-plugin来组合,maven-jar-plugin其实是maven项目自带的,在pom文件里面不依赖也是可以的,但是我需要 ...

最新文章

  1. 4个重要的量子理论实验综述
  2. linux wget命令详解
  3. Linux cp 实现强行覆盖
  4. Oracle-内存管理解读
  5. 阿里内网热搜开发者工具在线教程推荐
  6. java中堆栈溢出_java – 由于堆栈溢出,C中通常会发生什么?
  7. VTK:图片之Colored2DImageFusion
  8. SAP Fiori Elements 本地项目的 annotations.xml 文件
  9. python矩阵教程_numpy教程:矩阵matrix及其运算
  10. linux 大数字 进制转换,Linux下用bc快速进行数字进制转换
  11. 能运行lsdyna的服务器,ansys ls-dyna运行出错 - 仿真模拟 - 小木虫 - 学术 科研 互动社区...
  12. IP地址与子网掩码划分的心得
  13. 文件夹1KB快捷方式(暴风一号)病毒的解决办法
  14. 用线性探测法处理冲突时的散列表_案例
  15. 闰月c语言函数,中国农历闰月怎么闰 一般都是哪几个月?
  16. java sqlite sqlite_busy_SQLite 关闭时SQLITE_BUSY
  17. 几个名词解释 TBB VPP KKK
  18. scratch编程三级--小猫和笔合作画正方形
  19. 【编译原理】【实验】THOMPSON 算法的实现
  20. 黑客知识之7种DoS攻击方法简述

热门文章

  1. android的timertask,Android TimerTask 的简单应用及注意事项
  2. 二维码扫码就能报修单位电脑故障
  3. 加窗函数后频谱幅值发生了变化的修正技巧
  4. 工程伦理第十三章习题答案
  5. 适应多场景的客流量统计-人流量统计算法
  6. Poco::Exception. Code: 1000, e.code() = 0, e.displayText() = Host not found
  7. 数据库SQL实战-查找所有员工的last_name和first_name以及对应的dept_name(mysql)
  8. 缺失的运维,困顿的共享单车
  9. 通过压力测试提升服务器并发性能实例
  10. linux查看网卡bridge还是tap,linux 中网络设备的那些事 bridge TAP Veth Vlan br