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

2024-06-20 17:08

本文主要是介绍DWR+SpringMVC整合的3种方式之三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

方式三:这种方式和方式二差不多,主要使用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 plaincopy
在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 plaincopy
在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 plaincopy
在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 plaincopy
在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 plaincopy
在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 plaincopy
在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 plaincopy
在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

这篇关于DWR+SpringMVC整合的3种方式之三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078735

相关文章

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

Debian系和Redhat系防火墙配置方式

《Debian系和Redhat系防火墙配置方式》文章对比了Debian系UFW和Redhat系Firewalld防火墙的安装、启用禁用、端口管理、规则查看及注意事项,强调SSH端口需开放、规则持久化,... 目录Debian系UFW防火墙1. 安装2. 启用与禁用3. 基本命令4. 注意事项5. 示例配置R

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

Python获取浏览器Cookies的四种方式小结

《Python获取浏览器Cookies的四种方式小结》在进行Web应用程序测试和开发时,获取浏览器Cookies是一项重要任务,本文我们介绍四种用Python获取浏览器Cookies的方式,具有一定的... 目录什么是 Cookie?1.使用Selenium库获取浏览器Cookies2.使用浏览器开发者工具

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语