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
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; } }
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();//获得所有用户记录 }}
package test;public class TestBean{ private String name;//用户名 public TestBean(){ } public String getName(){ return name; } public void setName(String name){ this.name = name; } }