本文基于Spring MVC整合Mybatis。
(1) 导入包dwr.jar。
(2) 在web.xml中配置dwr。只需在配置DispatcherServlet下添加dwr的url-mapping即可,修改后的DispatcherServlet配置如下所示:
- <!-- servlet控制跳转 -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 配置文件 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:context-dispatcher.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
(3) 在context-dispatcher.xml中添加dwr的配置,修改后的文件如下所示:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans default-lazy-init="true"
- 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-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.directwebremoting.org/schema/spring-dwr
- http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 使用注解的包,包括子集 -->
- <context:component-scan base-package="com.geloin.spring" />
- <!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
- <mvc:annotation-driven />
- <!-- 视图解析器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/WEB-INF/pages/" />
- <property name="suffix" value=".jsp"></property>
- </bean>
- <!-- 要求dwr在spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。 -->
- <dwr:annotation-config id="dwr" />
- <!-- 要求DWR将util.js和engine.js映射到dwrController -->
- <dwr:url-mapping />
- <!-- 定义dwr -->
- <dwr:controller id="dwrController" debug="true">
- <dwr:config-param name="allowScriptTagRemoting"
- value="true" />
- <dwr:config-param name="crossDomainSessionSecurity"
- value="false" />
- </dwr:controller>
- </beans>
此处需要注意的是,mvc:annotation-driven会默认注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter和SimpleControllerHandlerAdapter,而使用Dwr时必须配置SimpleControllerHandlerAdapter,所以若不使用Mvc:annotation-driven时,请将dwr的配置改成:
- <!-- DWR配置 -->
- <bean
- class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
- <!-- 要求dwr在spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。 -->
- <dwr:annotation-config id="dwr" />
- <!-- 要求DWR将util.js和engine.js映射到dwrController -->
- <dwr:url-mapping />
- <!-- 定义dwr -->
- <dwr:controller id="dwrController" debug="true">
- <dwr:config-param name="allowScriptTagRemoting"
- value="true" />
- <dwr:config-param name="crossDomainSessionSecurity"
- value="false" />
- </dwr:controller>
- <!-- DWR配置 -->
(4) 在com.geloin.spring.controller下添加dwr控制器DirectController
- /**
- *
- * @author geloin
- * @date 2012-4-1 上午11:32:02
- */
- package com.geloin.spring.controller;
- import javax.annotation.Resource;
- import org.directwebremoting.annotations.RemoteMethod;
- import org.directwebremoting.annotations.RemoteProxy;
- import org.springframework.stereotype.Controller;
- import com.geloin.spring.entity.Menu;
- import com.geloin.spring.service.MenuService;
- /**
- * DWR
- *
- * @author geloin
- * @date 2012-4-1 上午11:32:02
- */
- @Controller
- @RemoteProxy(name = "directController")
- public class DirectController {
- @Resource(name = "menuService")
- private MenuService menuService;
- /**
- * 要删除的角色下是否含有用户
- *
- * @author geloin
- * @date 2012-4-1 下午4:00:03
- * @param ids
- * @return
- */
- @RemoteMethod
- public String showMenu(Integer id) {
- Menu menu = this.menuService.findById(id);
- StringBuilder builder = new StringBuilder();
- builder.append(id);
- builder.append("--");
- builder.append(menu.getName());
- builder.append("--");
- builder.append(menu.getParentId());
- builder.append("--");
- builder.append(menu.getUrl());
- builder.append("--");
- builder.append(menu.getIsShowLeft());
- return builder.toString();
- }
- }
@RemoteProxy表示该类为Dwr远程代理类,名称为directController,即在jsp可通过directController.js代表该类;@RemoteMethod代表showMenu方法为一个远程代理方法,该方法传入一个参数,返回一个字符串。
(5) 在jsp页面使用dwr,修改/WEB-INF/pages/background/menu.jsp页面,结果如下:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Insert title here</title>
- <script type='text/javascript' src='<c:url value="/dwr/engine.js"/>'></script>
- <script type='text/javascript' src='<c:url value="/dwr/util.js" />'></script>
- <script type='text/javascript'
- src='<c:url value="/dwr/interface/directController.js" />'></script>
- <script type="text/javascript">
- function view(obj) {
- var id = obj.value;
- directController.showMenu(id, function(data) {
- document.getElementById('view').innerHTML = data;
- });
- }
- </script>
- </head>
- <body>
- <select οnchange="javascript:view(this);">
- <c:forEach items="${result }" var="item">
- <option value="${item.id }">${item.name }</option>
- </c:forEach>
- </select>
- <div id="view"></div>
- </body>
- </html>
本文所展示的功能为,在下拉框中选择菜单后,显示其详细信息。
转自http://blog.csdn.net/geloin/article/details/7537148