DWR与Spring的结合原来如此简单

1. 修改spring的bean定义文件头部声明:

<?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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
......


2. 添加<dwr:configuration>,就算没有内容,也必须声明一个空的:

<dwr:configuration>
<dwr:convert type="bean" class="com.et.commerce.gwt.client.etProduct.model.EtProduct">
<dwr:include method="productId,productName,partNo,costPrice,basicPrice"/>
</dwr:convert>
</dwr:configuration>

3. 在实现类的bean定义里添加<dwr:remote>。[b][color=red]一定要加在实现类的定义里,如果加在接口定义里会报错。[/color][/b]

<bean id="IEtProductService" parent="transactionProxyFactoryBean">
<property name="target">
<ref bean="EtProductServiceImpl" />
</property>
</bean>
<bean id="EtProductServiceImpl"
class="com.et.commerce.gwt.server.etProduct.service.spring.EtProductServiceImpl">
<dwr:remote javascript="DWRIEtProductService" />


4. 在jsp中的使用

<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/DWRIEtProductService.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
<script type="text/javascript">
function onPartNoChanged(partNo){
DWRIEtProductService.getEtProductByPartNo(partNo,productChanged);
}
function productChanged(data){
if(data.length==1){
alert(data[0].productName);
}else if(data.length>1){
alert("找到多个商品与此Part No 对应,请重新选择");
DWRUtil.setValue("${actionName}_model_product_partNo","");
document.getElementById("${actionName}_model_product_partNo").focus();
}else{
alert("没有找到与此Part No 对应的商品,请重新输入");
DWRUtil.setValue("${actionName}_model_product_partNo","");
document.getElementById("${actionName}_model_product_partNo").focus();
}
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页