首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

dwr+spring整合有关问题

2012-10-18 
dwr+spring整合问题applicationContext.xml?xml version1.0 encodingUTF-8?beansxmlnshttp://w

dwr+spring整合问题
applicationContext.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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  <bean id="impl" class="test.DWRimpl"></bean>
  <bean id="dwrtest" class="test.DWRtest" scope="prototype">
  <property name="bean" ref="impl"></property>
  </bean>
</beans>

dwr.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> 
<dwr> 
<allow> 
<!--javascript="前台引用的函数名" creator="spring"指定让Spring代理 name="beanName"固定 value的值为Spring配置文件的bean的id值 -->
<create creator="spring" javascript="Test" >  
<param name="beanName" value="dwrtest"/>
</create> 
<!--配置dwr交互 中所用到的bean实类 -->
<convert converter="bean" match="test.TestBean">
</convert>
</allow> 
</dwr>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 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_2_5.xsd">
  <!-- Spring配置 指定spring配置文件位置-->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
  <!-- 开启监听 -->  
  <listener>  
  <listener-class>
  org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>
 <!-- 配置DWR -->  
 <servlet> 
<servlet-name>dwr-invoker</servlet-name> 
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> 
</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>

applicationContext.xml,dwr.xml,web.xml三文件放在WebRoot/WEB-INF下


DWRimpl.java

Java code
package test;import java.util.ArrayList;import java.util.List;public class DWRimpl{    public String TestUserName(TestBean bean){    //检验用户名        if(bean.getName().trim().equals("admin")){            return  "恭喜,你输入的用户名正确!";        }                return  "你输入的用户名错误,请输入admin!";    }        public List<TestBean>  FindAllNames(){    //获得所有用户记录        //此例子中没用到hibernater,那就模拟一个数据,若用到hibernater就根据自己逻辑操作数据        List<TestBean> tt=new ArrayList<TestBean>();                for(int i=0;i<5;i++){            TestBean tb= new TestBean();            tb.setName("用户名"+(i+1));            tt.add(tb);        }                return tt;    } } 



DWRtest.java
Java code
package test;import java.util.List;public class DWRtest{    private DWRimpl bean;        public DWRimpl getBean(){        return bean;    }        public void setBean(DWRimpl bean){        this.bean = bean;    }        public String TestName(TestBean testbean){        return this.bean.TestUserName(testbean);//校验用户名    }        public List<TestBean>  getData(){        return this.bean.FindAllNames();//获得所有用户记录    }}


TestBean.java
Java code
package test;public class TestBean{    private String name;//用户名        public TestBean(){              }        public String getName(){        return name;    }        public void setName(String name){        this.name = name;    }    }


index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>DWR+Spring实例</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />  
<script type='text/javascript' src='dwr/interface/Test.js'></script>  
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript">
function TestMyName(){ //测试用户名
if(DWRUtil.getValue("name")==""){
alert("用户名不能为空!");
return;
}

var ss=DWRUtil.getValue("name");
var str={name:ss} //获得文本框的值,用来传给后台

Test.TestName(str,function(mes){
alert(mes);
});  
}
 
function findAllName(){
Test.getData(function(names){  

var str="";
 
for(var i=0;i<names.length;i++){ //解析数据
str+=names[i].name+"\t";
}

alert("总用户记录:"+names.length+"\n"+str);
});
}
</script> 
</head> 
<body>
<h1>DWR+Spring实例</h1>  
<input type="text" name="name"><br>
<input type="button" value="提交用户" onClick="javascript:TestMyName()"> <br> 
<input type="button" value="显示所有用户" onClick="javascript:findAllName()">  
</body> 
</html>


前台提交时总是提示我“DWRUtil”未定义 或 “Test”未定义 ,哪错了?
哪位大虾知道,谢谢了。

[解决办法]
给你个例子看看
http://blog.csdn.net/zuxianghuang/article/details/7376247
下载
http://download.csdn.net/detail/zuxianghuang/4158486

热点排行