核心提示:Spring和Dwr的整合,这个和Spring的整合方式,只是创建Bean的方式不一样,不是通过new 的方式,而是通过Spring注入,但是Dwr和SpringMvc的配置,就完全不一样了。目录结构...
Spring和Dwr的整合,这个和Spring的整合方式,只是创建Bean的方式不一样,不是通过new 的方式,而是通过Spring注入,但是Dwr和SpringMvc的配置,就完全不一样了。
目录结构
环境搭建
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yellowcong</groupId> <artifactId>day11_30</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>day11_30 Maven Webapp</name> <url>https://maven.apache.org</url> <!-- 共用的配置说明,比如spring版本, 项目名称, jdk版本等 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- Dwr框架 --> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.2-RELEASE</version> </dependency> <!-- 必须也连带加上这个日志包,不然报错 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.0.4</version> </dependency> <!-- 文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- Spring BEGIN--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- 导入Spring的orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- Slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!-- 配置切面 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!---aspectj 面向切向 --> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- Spring需要的注解 --> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.2</version> </dependency> <!-- Spring 的测试类 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring END--> <!-- 配置Spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>day11_30</finalName> </build> </project>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>day1_27_dwr</display-name> <!-- 设定字符编码 --> <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> <!-- spring的监听器,可以通过上下文来获取参数 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- a)配置DwrListener 监听器--> <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <!-- b)配置DwrServlet --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
dwr.xml
创建的Bean的方式不一样了
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "https://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <!-- 用户操作的Service --> <create creator="spring" javascript="UserService"> <!-- 公布我们的 Java 文件 --> <param name="beanName" value="userService" /> <exclude method="sayHello"/> <exclude method="load"/> <!-- 只可以访问 load方法, 其中 javascript 是我们在 js中调用的那个, 如果 配置了 include 和 exclude --> <!-- <include method="sayHello"/> --> <!-- 除了load 方法,其他方法都可以用 --> <exclude method="sayHello"/> </create> <!-- 创建 bean,通过bean来传值 --> <convert converter="bean" match="com.yellowcong.entity.User" /> <!-- 配置错误处理, 添加这两个类就可以了 --> <convert converter="exception" match="java.lang.Exception" /> <convert converter="bean" match="java.lang.StackTraceElement"/> </allow> </dwr>
Spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" xsi:schemaLocation=" https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!-- 自动扫描(自动注入) --> <context:annotation-config/> <context:component-scan base-package="com.yellowcong.*"/> </beans>
实体类User
package com.yellowcong.entity; public class User { private String username; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
UserService
服务类,需要加入注解,表示UserService
package com.yellowcong.service; import org.springframework.stereotype.Service; import com.yellowcong.entity.User; @Service("userService") public class UserService { public String sayHello(String name){ System.out.println(name); /*Browser.withPage("", new Runnable() { public void run() { // TODO Auto-generated method stub } });*/ return "你好"+name; } public String addUser(User user){ System.out.println(user.getAge()); System.out.println(user.getUsername()); String username = user.getUsername(); if(username == null || "".equals(username == null)){ throw new RuntimeException("用户名不为空"); } return "添加成功"; } }
界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/UserService.js"></script> <!-- 调用我们的JAVA代码 --> <script type="text/javascript"> function addUser(age,username){ var user ={age:age,username:username}; UserService.addUser(user,{ callback:function(data){ alert("处理结果\t"+data); }, errorHandler:function(message,error){ //相当于一个Map 中获取key 和 value for(var key in error){ alert(key+"__"+error[key]); } } }); } </script> </head> <body> <h2>异常处理</h2> <input type="button" value="正常数据" onclick="addUser('21','yellowcong')" /> <input type="button" value="异常数据" onclick="addUser()" /> </body> </html>