DWR+SpringMVC整合的方式

简介: DWR+SpringMVC整合的方式http://www.bieryun.com/1497.html 这种方式的时候也遇到了一个很无语的问题,我原来使用的是maven下载的dwr-3.0.M1.jar包,然后运行jetty没错,显示jsp的时候就一直报下面这个错误: org.

DWR+SpringMVC整合的方式

这种方式的时候也遇到了一个很无语的问题,我原来使用的是maven下载的dwr-3.0.M1.jar包,然后运行jetty没错,显示jsp的时候就一直报下面这个错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [annotation-scan]|Offending resource: ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

然后自己也搞了很久,最后百度,网友说是dwr的包问题,mavne库中下载的jar包有问题,从网上下载了一个jar'包,自己上传到nexus的私有库中,然后再修改了pom.xml中的dwr,当使用了自己下载的jar包后,运行jetty,一切正常。

如何使用自己的jar包,

一、可以使用maven的命令安装jar包到maven的库中,一定到该jar包的文件夹下执行如下命令,否则会提示没有pom文件。如:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=F:/JAR Pack/ojdbc14.jar
二、到nexus的管理后台,直接使用nexus的添加jar包到私有库的功能。
选中一个宿主库(hosted),然后点击Artifact Upload一栏,在Artifact Upload下填写jar的groupId、artifactId、version,然后从电脑上选择要上传的jar包,然后点击Add Artifact按钮,在点击Upload Artifact(s)按钮即可。
 
 
项目目录结构:

 
一、pom.xml

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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/xsd/maven-4.0.0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.lessony</groupId>
  5.     <artifactId>dwr-spring03</artifactId>
  6.     <version>1.0-SNAPSHOT</version>
  7.     <packaging>war</packaging>
  8.     <name>dwr-spring03</name>
  9.     <properties>
  10.         <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12.         <spring.version>3.2.2.RELEASE</spring.version>
  13.     </properties>
  14.     <dependencies>
  15.         <dependency>
  16.             <groupId>junit</groupId>
  17.             <artifactId>junit</artifactId>
  18.             <version>4.10</version>
  19.             <scope>test</scope>
  20.         </dependency>
  21.         <!--spring的依赖-->
  22.         <dependency>
  23.             <groupId>org.springframework</groupId>
  24.             <artifactId>spring-context</artifactId>
  25.             <version>${spring.version}</version>
  26.         </dependency>
  27.         <dependency>
  28.             <groupId>org.springframework</groupId>
  29.             <artifactId>spring-core</artifactId>
  30.             <version>${spring.version}</version>
  31.         </dependency>
  32.         <dependency>
  33.             <groupId>org.springframework</groupId>
  34.             <artifactId>spring-beans</artifactId>
  35.             <version>${spring.version}</version>
  36.         </dependency>
  37.         <dependency>
  38.             <groupId>org.springframework</groupId>
  39.             <artifactId>spring-webmvc</artifactId>
  40.             <version>${spring.version}</version>
  41.         </dependency>
  42.         <!--dwr的依赖,原来maven下载的dwr的jar包有问题,这里使用自己上传在nexus中的jar包-->
  43.         <dependency>
  44.             <groupId>org.directwebremoting</groupId>
  45.             <artifactId>dwr</artifactId>
  46.             <version>3.0.M1</version>
  47.         </dependency>
  48.     </dependencies>
  49.     <build>
  50.         <plugins>
  51.             <plugin>
  52.                 <groupId>org.apache.maven.plugins</groupId>
  53.                 <artifactId>maven-compiler-plugin</artifactId>
  54.                 <version>3.1</version>
  55.                 <configuration>
  56.                     <source>1.7</source>
  57.                     <target>1.7</target>
  58.                     <compilerArguments>
  59.                         <!-- endorseddirs<目录>覆盖签名的标准路径的位置 -->
  60.                         <endorseddirs>${endorsed.dir}</endorseddirs>
  61.                     </compilerArguments>
  62.                 </configuration>
  63.             </plugin>
  64.             <plugin>
  65.                 <groupId>org.apache.maven.plugins</groupId>
  66.                 <artifactId>maven-war-plugin</artifactId>
  67.                 <version>2.3</version>
  68.                 <configuration>
  69.                     <failOnMissingWebXml>false</failOnMissingWebXml>
  70.                 </configuration>
  71.             </plugin>
  72.             <plugin>
  73.                 <groupId>org.apache.maven.plugins</groupId>
  74.                 <artifactId>maven-dependency-plugin</artifactId>
  75.                 <version>2.6</version>
  76.                 <executions>
  77.                     <execution>
  78.                         <phase>validate</phase>
  79.                         <goals>
  80.                             <goal>copy</goal>
  81.                         </goals>
  82.                         <configuration>
  83.                             <outputDirectory>${endorsed.dir}</outputDirectory>
  84.                             <silent>true</silent>
  85.                             <artifactItems>
  86.                                 <artifactItem>
  87.                                     <groupId>javax</groupId>
  88.                                     <artifactId>javaee-endorsed-api</artifactId>
  89.                                     <version>7.0</version>
  90.                                     <type>jar</type>
  91.                                 </artifactItem>
  92.                             </artifactItems>
  93.                         </configuration>
  94.                     </execution>
  95.                 </executions>
  96.             </plugin>
  97.             <!--jetty服务器-->
  98.             <plugin>
  99.                 <groupId>org.mortbay.jetty</groupId>
  100.                 <artifactId>jetty-maven-plugin</artifactId>
  101.                 <version>8.1.16.v20140903</version>
  102.                 <configuration>
  103.                     <scanIntervalSeconds>10</scanIntervalSeconds>
  104.                     <webApp>
  105.                         <contextPath>/dwr-spring03</contextPath>
  106.                     </webApp>
  107.                     <connectors>
  108.                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
  109.                             <port>8805</port>
  110.                             <maxIdleTime>60000</maxIdleTime>
  111.                         </connector>
  112.                     </connectors>
  113.                 </configuration>
  114.             </plugin>
  115.             <!--打包插件,把web打成zip包-->
  116.             <plugin>
  117.                 <groupId>org.apache.maven.plugins</groupId>
  118.                 <artifactId>maven-assembly-plugin</artifactId>
  119.                 <version>2.4</version>
  120.                 <configuration>
  121.                     <descriptors>
  122.                         <descriptor>assembly.xml</descriptor>
  123.                     </descriptors>
  124.                 </configuration>
  125.                 <executions>
  126.                     <!-- 当执行mvn package时才会打包 -->
  127.                     <execution>
  128.                         <id>make-assembly</id>
  129.                         <phase>package</phase>
  130.                         <goals>
  131.                             <goal>single</goal>
  132.                         </goals>
  133.                     </execution>
  134.                 </executions>
  135.             </plugin>
  136.         </plugins>
  137.     </build>
  138. </project>
二、web.xml

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  3.     <!-- 创建Spring的监听器 -->
  4.     <listener>
  5.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  6.     </listener>
  7.     <!-- Spring 的监听器可以通过这个上下文参数来获取beans.xml的位置 -->
  8.     <init-param>
  9.         <param-name>contextConfigLocation</param-name>
  10.         <param-value>classpath*:beans.xml</param-value>
  11.     </init-param>
  12.     <servlet>
  13.         <servlet-name>dispatcher</servlet-name>
  14.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  15.          <context-param>
  16.             <param-name>contextConfigLocation</param-name>
  17.             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  18.         </context-param>
  19.     </servlet>
  20.     <servlet-mapping>
  21.         <servlet-name>dispatcher</servlet-name>
  22.         <url-pattern>/</url-pattern>
  23.     </servlet-mapping>
  24.     <filter>
  25.         <filter-name>characterFilter</filter-name>
  26.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  27.         <init-param>
  28.             <param-name>encoding</param-name>
  29.             <param-value>UTF-8</param-value>
  30.         </init-param>
  31.     </filter>
  32.     <filter-mapping>
  33.         <filter-name>characterFilter</filter-name>
  34.         <url-pattern>/*</url-pattern>
  35.     </filter-mapping>
  36.     <servlet-mapping>
  37.         <servlet-name>dispatcher</servlet-name>
  38.         <url-pattern>/dwr/*</url-pattern>
  39.     </servlet-mapping>
  40. </web-app>
3、java类

[java] view plain copy

  1. package com.lessony.dwr.spring03.entity;
  2. public class User {
  3.     private int id;
  4.     private String name;
  5.     public int getId() {
  6.         return id;
  7.     }
  8.     public void setId(int id) {
  9.         this.id = id;
  10.     }
  11.     public String getName() {
  12.         return name;
  13.     }
  14.     public void setName(String name) {
  15.         this.name = name;
  16.     }
  17.     public User() {
  18.     }
  19.     public User(int id, String name) {
  20.         super();
  21.         this.id = id;
  22.         this.name = name;
  23.     }
  24. }
 

[java] view plain copy

  1. package com.lessony.dwr.spring03.service;
  2. import com.lessony.dwr.spring03.entity.User;
  3. public interface IHelloService {
  4.     public String sayHello(String name);
  5.         public User load();
  6. }
 

[java] view plain copy

  1. package com.lessony.dwr.spring03.service.impl;
  2. import com.lessony.dwr.spring03.entity.User;
  3. import com.lessony.dwr.spring03.service.IHelloService;
  4. import org.directwebremoting.annotations.RemoteMethod;
  5. import org.directwebremoting.annotations.RemoteProxy;
  6. @RemoteProxy(name="helloService")
  7. public class HelloService implements IHelloService {
  8.     @Override
  9.     @RemoteMethod
  10.     public String sayHello(String name) {
  11.         System.out.println("hello " + name);
  12.         return "hello: "+name;
  13.     }
  14.     @Override
  15.     @RemoteMethod
  16.     public User load() {
  17.         System.out.println("load abc");
  18.         return new User(1,"abc");
  19.     }
  20. }
4、配置dwr

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.         xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.             http://www.springframework.org/schema/beans/spring-beans.xsd
  9.             http://www.springframework.org/schema/context
  10.             http://www.springframework.org/schema/context/spring-context-3.2.xsd
  11.             http://www.springframework.org/schema/mvc
  12.             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  13.             http://www.directwebremoting.org/schema/spring-dwr
  14.             http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
  15.     <mvc:annotation-driven/>
  16.     <mvc:resources location="/resources/" mapping="/resources/**"/>
  17.         <!--dwr过滤-->
  18.         <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  19.       <property value="true" name="alwaysUseFullPath"></property>
  20.       <property name="mappings">
  21.         <props>
  22.           <prop key="/dwr/**/*">dwrController</prop>
  23.         </props>
  24.      </property>
  25.     </bean>
  26.         <!--dwr控制器-->
  27.     <dwr:controller id="dwrController" debug="true"/>
  28.         <!--设置需要dwr转化的实体类,格式为json传输到jsp页面-->
  29.     <dwr:configuration>
  30.         <dwr:convert type="bean" class="com.lessony.dwr.spring03.entity.User"/>
  31.     </dwr:configuration>
  32.     <dwr:annotation-config id="dwrAnnotationConfig" />
  33.     <dwr:annotation-scan base-package="com.lessony.dwr.spring03.service.impl" scanDataTransferObject="true"/>
  34.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  35.         <property name="prefix" value="/WEB-INF/jsp/"/>
  36.         <property name="suffix" value=".jsp"/>
  37.     </bean>
  38. </beans>
 
5jsp文件

[html] view plain copy

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4.     <head>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <title>JSP Page</title>
  7.         <script src="<%=request.getContextPath()%>/dwr/engine.js"></script>
  8.         <script src="<%=request.getContextPath()%>/dwr/interface/helloService.js"></script>
  9.         <script>
  10.             helloService.load(function(data){
  11.         alert(data.name);
  12.             });
  13.         </script>
  14.     </head>
  15.     <body>
  16.         <h1>Hello World!</h1>
  17.     </body>
  18. </html>
全部编辑完成之后,右键项目,选择Run As... 继续选择7 Maven build...  ,在弹出框的Goals中输入jetty:run ,然后点击RUN运行。
打开浏览器,输入http://localhost:8805/dwr-spring03/dwr03.jsp,就可以看到控制台输出了:load abc,jsp页面弹出了abc
相关文章
|
Java Spring
15dwr - DWR 与 Spring整合
15dwr - DWR 与 Spring整合
58 0
16dwr - DWR 与 JSF整合
16dwr - DWR 与 JSF整合
52 0
|
前端开发 Java 数据库连接
19dwr - DWR 与 Hibernate整合
19dwr - DWR 与 Hibernate整合
33 0
|
前端开发 JavaScript Java
01dwr - dwr简介
01dwr - dwr简介
42 0
|
开发框架 安全 JavaScript
51dwr - 安全
51dwr - 安全
38 0
|
Java PHP
17dwr - DWR 与 Struts整合
17dwr - DWR 与 Struts整合
36 0
09dwr - dwr.xml配置(纵览)
09dwr - dwr.xml配置(纵览)
62 0
|
6月前
|
JavaScript 前端开发 Java
DWR3使用
DWR3使用
|
前端开发 JavaScript Java
20dwr - DWR 与 WebWork整合
20dwr - DWR 与 WebWork整合
50 0
|
缓存 JavaScript 前端开发
14dwr - DWR 与 Servlet整合
14dwr - DWR 与 Servlet整合
58 0