DWR+SpringMVC整合的3种方式之三

来源:互联网 发布:ant java参数 编辑:程序博客网 时间:2024/06/04 18:10

方式三:这种方式和方式二差不多,主要使用annotation配置和注解 

说明:这种的耦合度也是和方式二差不多,本人最推荐用方式一,写的清楚,配置也清楚。这种方式的时候也遇到了一个很无语的问题,我原来使用的是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
在CODE上查看代码片派生到我的代码片
  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.   
  5.     <groupId>com.lessony</groupId>  
  6.     <artifactId>dwr-spring03</artifactId>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <packaging>war</packaging>  
  9.   
  10.     <name>dwr-spring03</name>  
  11.   
  12.     <properties>  
  13.         <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.         <spring.version>3.2.2.RELEASE</spring.version>  
  16.     </properties>  
  17.     
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>junit</groupId>  
  21.             <artifactId>junit</artifactId>  
  22.             <version>4.10</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.           
  26.         <!--spring的依赖-->  
  27.         <dependency>  
  28.             <groupId>org.springframework</groupId>  
  29.             <artifactId>spring-context</artifactId>  
  30.             <version>${spring.version}</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframework</groupId>  
  34.             <artifactId>spring-core</artifactId>  
  35.             <version>${spring.version}</version>  
  36.         </dependency>  
  37.         <dependency>  
  38.             <groupId>org.springframework</groupId>  
  39.             <artifactId>spring-beans</artifactId>  
  40.             <version>${spring.version}</version>  
  41.         </dependency>  
  42.         <dependency>  
  43.             <groupId>org.springframework</groupId>  
  44.             <artifactId>spring-webmvc</artifactId>  
  45.             <version>${spring.version}</version>  
  46.         </dependency>  
  47.           
  48.         <!--dwr的依赖,原来maven下载的dwr的jar包有问题,这里使用自己上传在nexus中的jar包-->  
  49.         <dependency>  
  50.             <groupId>org.directwebremoting</groupId>  
  51.             <artifactId>dwr</artifactId>  
  52.             <version>3.0.M1</version>  
  53.         </dependency>  
  54.     </dependencies>  
  55.   
  56.     <build>  
  57.         <plugins>  
  58.             <plugin>  
  59.                 <groupId>org.apache.maven.plugins</groupId>  
  60.                 <artifactId>maven-compiler-plugin</artifactId>  
  61.                 <version>3.1</version>  
  62.                 <configuration>  
  63.                     <source>1.7</source>  
  64.                     <target>1.7</target>  
  65.                     <compilerArguments>  
  66.                         <!-- endorseddirs<目录>覆盖签名的标准路径的位置 -->  
  67.                         <endorseddirs>${endorsed.dir}</endorseddirs>  
  68.                     </compilerArguments>  
  69.                 </configuration>  
  70.             </plugin>  
  71.             <plugin>  
  72.                 <groupId>org.apache.maven.plugins</groupId>  
  73.                 <artifactId>maven-war-plugin</artifactId>  
  74.                 <version>2.3</version>  
  75.                 <configuration>  
  76.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  77.                 </configuration>  
  78.             </plugin>  
  79.             <plugin>  
  80.                 <groupId>org.apache.maven.plugins</groupId>  
  81.                 <artifactId>maven-dependency-plugin</artifactId>  
  82.                 <version>2.6</version>  
  83.                 <executions>  
  84.                     <execution>  
  85.                         <phase>validate</phase>  
  86.                         <goals>  
  87.                             <goal>copy</goal>  
  88.                         </goals>  
  89.                         <configuration>  
  90.                             <outputDirectory>${endorsed.dir}</outputDirectory>  
  91.                             <silent>true</silent>  
  92.                             <artifactItems>  
  93.                                 <artifactItem>  
  94.                                     <groupId>javax</groupId>  
  95.                                     <artifactId>javaee-endorsed-api</artifactId>  
  96.                                     <version>7.0</version>  
  97.                                     <type>jar</type>  
  98.                                 </artifactItem>  
  99.                             </artifactItems>  
  100.                         </configuration>  
  101.                     </execution>  
  102.                 </executions>  
  103.             </plugin>  
  104.               
  105.             <!--jetty服务器-->  
  106.             <plugin>  
  107.                 <groupId>org.mortbay.jetty</groupId>  
  108.                 <artifactId>jetty-maven-plugin</artifactId>  
  109.                 <version>8.1.16.v20140903</version>  
  110.                 <configuration>  
  111.                     <scanIntervalSeconds>10</scanIntervalSeconds>  
  112.                     <webApp>  
  113.                         <contextPath>/dwr-spring03</contextPath>  
  114.                     </webApp>  
  115.                     <connectors>  
  116.                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  117.                             <port>8805</port>  
  118.                             <maxIdleTime>60000</maxIdleTime>  
  119.                         </connector>  
  120.                     </connectors>  
  121.                 </configuration>  
  122.             </plugin>  
  123.               
  124.             <!--打包插件,把web打成zip包-->  
  125.             <plugin>  
  126.                 <groupId>org.apache.maven.plugins</groupId>  
  127.                 <artifactId>maven-assembly-plugin</artifactId>  
  128.                 <version>2.4</version>  
  129.                 <configuration>  
  130.                     <descriptors>  
  131.                         <descriptor>assembly.xml</descriptor>  
  132.                     </descriptors>  
  133.                 </configuration>  
  134.                 <executions>  
  135.                     <!-- 当执行mvn package时才会打包 -->  
  136.                     <execution>  
  137.                         <id>make-assembly</id>  
  138.                         <phase>package</phase>  
  139.                         <goals>  
  140.                             <goal>single</goal>  
  141.                         </goals>  
  142.                     </execution>  
  143.                 </executions>  
  144.             </plugin>  
  145.               
  146.         </plugins>  
  147.     </build>  
  148.   
  149. </project>  
二、web.xml
[html] view plain copy
在CODE上查看代码片派生到我的代码片
  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.   
  4.     <!-- 创建Spring的监听器 -->  
  5.     <listener>  
  6.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  7.     </listener>  
  8.     <!-- Spring 的监听器可以通过这个上下文参数来获取beans.xml的位置 -->  
  9.     <init-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath*:beans.xml</param-value>  
  12.     </init-param>  
  13.       
  14.     <servlet>  
  15.         <servlet-name>dispatcher</servlet-name>  
  16.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  17.          <context-param>  
  18.             <param-name>contextConfigLocation</param-name>  
  19.             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
  20.         </context-param>  
  21.     </servlet>  
  22.     <servlet-mapping>  
  23.         <servlet-name>dispatcher</servlet-name>  
  24.         <url-pattern>/</url-pattern>  
  25.     </servlet-mapping>  
  26.     <filter>  
  27.         <filter-name>characterFilter</filter-name>  
  28.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  29.         <init-param>  
  30.             <param-name>encoding</param-name>  
  31.             <param-value>UTF-8</param-value>  
  32.         </init-param>  
  33.     </filter>  
  34.     <filter-mapping>  
  35.         <filter-name>characterFilter</filter-name>  
  36.         <url-pattern>/*</url-pattern>  
  37.     </filter-mapping>  
  38.       
  39.     <servlet-mapping>  
  40.         <servlet-name>dispatcher</servlet-name>  
  41.         <url-pattern>/dwr/*</url-pattern>  
  42.     </servlet-mapping>  
  43.        
  44. </web-app>  
3、java类
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package com.lessony.dwr.spring03.entity;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String name;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public User() {  
  19.     }  
  20.     public User(int id, String name) {  
  21.         super();  
  22.         this.id = id;  
  23.         this.name = name;  
  24.     }  
  25. }  
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package com.lessony.dwr.spring03.service;  
  2.   
  3. import com.lessony.dwr.spring03.entity.User;  
  4.   
  5. public interface IHelloService {  
  6.     public String sayHello(String name);  
  7.           
  8.         public User load();  
  9. }  
[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package com.lessony.dwr.spring03.service.impl;  
  2.   
  3. import com.lessony.dwr.spring03.entity.User;  
  4. import com.lessony.dwr.spring03.service.IHelloService;  
  5. import org.directwebremoting.annotations.RemoteMethod;  
  6. import org.directwebremoting.annotations.RemoteProxy;  
  7.   
  8. @RemoteProxy(name="helloService")  
  9. public class HelloService implements IHelloService {  
  10.   
  11.     @Override  
  12.     @RemoteMethod  
  13.     public String sayHello(String name) {  
  14.         System.out.println("hello " + name);  
  15.         return "hello: "+name;  
  16.     }  
  17.   
  18.     @Override  
  19.     @RemoteMethod  
  20.     public User load() {  
  21.         System.out.println("load abc");  
  22.         return new User(1,"abc");  
  23.     }  
  24.   
  25. }  
4、配置dwr
[html] view plain copy
在CODE上查看代码片派生到我的代码片
  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.       
  16.     <mvc:annotation-driven/>  
  17.     <mvc:resources location="/resources/" mapping="/resources/**"/>  
  18.       
  19.           
  20.         <!--dwr过滤-->  
  21.         <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  22.       <property value="true" name="alwaysUseFullPath"></property>   
  23.       <property name="mappings">  
  24.         <props>   
  25.           <prop key="/dwr/**/*">dwrController</prop>  
  26.         </props>  
  27.      </property>   
  28.     </bean>  
  29.       
  30.         <!--dwr控制器-->  
  31.     <dwr:controller id="dwrController" debug="true"/>  
  32.           
  33.         <!--设置需要dwr转化的实体类,格式为json传输到jsp页面-->  
  34.     <dwr:configuration>  
  35.         <dwr:convert type="bean" class="com.lessony.dwr.spring03.entity.User"/>  
  36.     </dwr:configuration>  
  37.       
  38.          
  39.     <dwr:annotation-config id="dwrAnnotationConfig" />  
  40.     <dwr:annotation-scan base-package="com.lessony.dwr.spring03.service.impl" scanDataTransferObject="true"/>  
  41.       
  42.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  43.         <property name="prefix" value="/WEB-INF/jsp/"/>  
  44.         <property name="suffix" value=".jsp"/>  
  45.     </bean>  
  46.       
  47.       
  48.       
  49. </beans>  
5jsp文件
[html] view plain copy
在CODE上查看代码片派生到我的代码片
  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.           
  10.         <script>              
  11.             helloService.load(function(data){  
  12.         alert(data.name);  
  13.             });  
  14.         </script>  
  15.     </head>  
  16.     <body>  
  17.         <h1>Hello World!</h1>  
  18.     </body>  
  19. </html>  
全部编辑完成之后,右键项目,选择Run As... 继续选择7 Maven build...  ,在弹出框的Goals中输入jetty:run ,然后点击RUN运行。
打开浏览器,输入http://localhost:8805/dwr-spring03/dwr03.jsp,就可以看到控制台输出了:load abc,jsp页面弹出了abc
最后附上zip包,链接:http://download.csdn.net/detail/lxn39830435731415926/8714183
0 0
原创粉丝点击