Spring MVC整合DWR

时间:2020-12-24 18:43:31

http://blog.csdn.net/geloin/article/details/7537148

基本上与上文描述的情况一致;

在Controller中可以进行Service层的调用;

如果需要返回对象,需要在spring-mvc.xml中进行配置:

<dwr:configuration>
<dwr:convert type="bean" class="com.srie.ljddml.orga.model.client.ClientUser"></dwr:convert>
</dwr:configuration>

springmvc.xml:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<!-- 注册XmlViewResolver,用于iReport & JasperReports报表生成 -->
<bean id="jasperReportResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order">
<value>0</value>
</property>
<property name="location">
<value>classpath:jasper-views.xml</value>
</property>
</bean>
<!-- spring mvc 注解驱动 -->
<mvc:annotation-driven />
<!-- 扫描器 -->
<context:component-scan base-package="com.srie.ljddml.controller" />
<!-- 配置视图 解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- 前缀和后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<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>
<!--设置需要dwr转化的实体类,格式为json传输到jsp页面 -->
<dwr:configuration>
<dwr:convert type="bean" class="com.srie.ljddml.orga.model.client.ClientUser"></dwr:convert>
</dwr:configuration>
<!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000" />
</bean>
<!-- 从请求和响应读取/编写字符串 -->
<bean id="stringHttpMessage" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 用于将对象转换为JSON -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessage" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
</beans>

controller:

package com.srie.ljddml.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.srie.ljddml.ddml.service.DdmlServiceI;
import com.srie.ljddml.orga.model.client.ClientUser;
import com.srie.ljddml.orga.service.OrgaServiceI;
@Controller
@RemoteProxy(name = "directController")
public class DirectController {
@Autowired
OrgaServiceI orgaService;
@Autowired
DdmlServiceI ddmlServiceI;
@RemoteMethod
public String getTest(String a) {
return "test" + a;
}
@RemoteMethod
public List<String> getList(HttpSession session) {
ClientUser user = (ClientUser) session.getAttribute("clientUser");
return orgaService.getUserAuthKeys(user);
}
@RemoteMethod
public ClientUser getUser(HttpSession session) {
ClientUser attribute = (ClientUser) session.getAttribute("clientUser");
return attribute;
}
@RemoteMethod
public List<ClientUser> getUserList(HttpSession session) {
ClientUser attribute = (ClientUser) session.getAttribute("clientUser");
List<ClientUser> list = new ArrayList<ClientUser>();
list.add(attribute);
return list;
}
@RemoteMethod
public Map<String, Object> getMsg(HttpSession session){
ClientUser user = (ClientUser) session.getAttribute("clientUser");
return ddmlServiceI.getMsg(user);
}
}

HTML:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="js/b/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/mlbj.css">
<script src="js/b/js/jquery-1.11.1.min.js"></script>
<script src="js/b/js/bootstrap.min.js"></script>
<script src="js/login.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/directController.js"></script> </head>
<body class="container-fluid">
<div class="row">
<div class="left_menu col-xs-3 col-sm-3 col-md-2" id='divNavId' style="background:#ffffff;">
<div id='mlSxxx'>----xxx<span ></span>----</div> </div>
<div class="col-xs-9 col-sm-9 col-md-10">
<iframe id="rightIframe" scrolling="no" frameborder="0" src="uri/DdmlTransact.do" style="width:100%;height:99%;"></iframe>
</div>
</div>
</body>
</html>

js:

/**
* 页面加载之后执行的函数;
*/
$(function() {
loadMLAuths();
divMlSearchClick();
keepSession();
setInterval("getMsg()", 60000);
});
/**
* 全局变量定义部分;
*/
var i = 0;
/**
* 事件编写部分;
*/
// 加载消息函数
function getMsg() {
directController.getMsg(function(data) {
$.each(data, function(i, n) {
$("#" + i +" span").text(n);
});
});
}
/**
* 进行会话保持;
*/
function keepSession() {
window.setInterval("$.getJSON('orga/mlbj.do');", 120000);
}