spring中使用dwr和普通java类中使用差不多只不过要把dwr配置文件中这样写
<!-- 从Spring中获取Java对象 -->
<create javascript="deptSrv" creator="spring"><!-- 此处写spring生成 -->
<param name="beanName" value="deptServices"></param> <!-- 此处写spring 中bean的名字就可以了 -->
就可以了
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Spring上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resource/app*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DWR前端控制器 -->
<servlet>
<servlet-name>dwrServlet</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 指定配置文件 -->
<init-param>
<param-name>config</param-name>
<!-- 如果有多个用","分开 -->
<param-value>
/WEB-INF/classes/config/dwr.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwrServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
<bean id="deptServices" class="services.DeptServices"></bean>
<bean id="loginSrv" class="services.LoginService"></bean>
</beans>
dwr.xm配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<!-- 通用dwr配置 -->
<dwr>
<allow>
<!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
<create javascript="helloSrv" creator="new">
<param name="class" value="services.HelloServices"></param>
</create>
<!-- 从Spring中获取Java对象 -->
<create javascript="deptSrv" creator="spring"><!-- 此处写spring生成 -->
<param name="beanName" value="deptServices"></param> <!-- 此处写spring 中bean的名字就可以了 -->
<!-- 禁止执行 -->
<exclude method="deleteDept" />
</create>
<create javascript="loginSrv" creator="spring">
<param name="beanName" value="loginSrv"></param>
</create>
<!-- 指定针对于特定对象的转换器 -->
<convert match="entity.*" converter="bean"></convert>
<convert match="java.lang.Throwable" converter="bean">
<param name="include" value="message"></param>
</convert>
</allow>
</dwr>