方式三:这种方式和方式二差不多,主要使用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
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <groupId>com.lessony</groupId> <artifactId>dwr-spring03</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>dwr-spring03</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!--spring的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--dwr的依赖,原来maven下载的dwr的jar包有问题,这里使用自己上传在nexus中的jar包--> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.M1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <!-- endorseddirs<目录>覆盖签名的标准路径的位置 --> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>7.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!--jetty服务器--> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/dwr-spring03</contextPath> </webApp> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8805</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin> <!--打包插件,把web打成zip包--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> <executions> <!-- 当执行mvn package时才会打包 --> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
二、web.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 创建Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring 的监听器可以通过这个上下文参数来获取beans.xml的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml</param-value> </init-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
3、java类
package com.lessony.dwr.spring03.entity; public class User { private int id; private String name; 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; } public User() { } public User(int id, String name) { super(); this.id = id; this.name = name; } }
package com.lessony.dwr.spring03.service; import com.lessony.dwr.spring03.entity.User; public interface IHelloService { public String sayHello(String name); public User load(); }
package com.lessony.dwr.spring03.service.impl; import com.lessony.dwr.spring03.entity.User; import com.lessony.dwr.spring03.service.IHelloService; import org.directwebremoting.annotations.RemoteMethod; import org.directwebremoting.annotations.RemoteProxy; @RemoteProxy(name="helloService") public class HelloService implements IHelloService { @Override @RemoteMethod public String sayHello(String name) { System.out.println("hello " + name); return "hello: "+name; } @Override @RemoteMethod public User load() { System.out.println("load abc"); return new User(1,"abc"); } }
4、配置dwr
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> <mvc:annotation-driven/> <mvc:resources location="/resources/" mapping="/resources/**"/> <!--dwr过滤--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property value="true" name="alwaysUseFullPath"></property> <property name="mappings"> <props> <prop key="/dwr/**/*">dwrController</prop> </props> </property> </bean> <!--dwr控制器--> <dwr:controller id="dwrController" debug="true"/> <!--设置需要dwr转化的实体类,格式为json传输到jsp页面--> <dwr:configuration> <dwr:convert type="bean" class="com.lessony.dwr.spring03.entity.User"/> </dwr:configuration> <dwr:annotation-config id="dwrAnnotationConfig" /> <dwr:annotation-scan base-package="com.lessony.dwr.spring03.service.impl" scanDataTransferObject="true"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
5jsp文件
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <script src="<%=request.getContextPath()%>/dwr/engine.js"></script> <script src="<%=request.getContextPath()%>/dwr/interface/helloService.js"></script> <script> helloService.load(function(data){ alert(data.name); }); </script> </head> <body> <h1>Hello World!</h1> </body> </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