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:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring公共配置</description>
<!-- 启用 annotation 配置模式 -->
<context:annotation-config />
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.wpms.user.*" />
<!-- DWR 配置 BEGIN -->
<!-- 启动 DWR 注解配置模式 -->
<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<!-- 开启dubug状态 -->
<dwr:controller debug="true" />
<!-- DWR 配置 END-->
</beans>
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 config url start-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/dwr3/applicationContext.xml
</param-value>
</context-param>
<!-- spring config url end-->
<!-- 著名的 Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--Hibernate Open Session in View Filter-->
<!-- 假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter
或者OpenSessionInViewInterceptor。session会在transaction结束后关闭,此时会抛出session is close 的异常-->
<!-- struts2 start-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 end -->
<!-- 过滤器映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- dwr配置 -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.spring.DwrSpringServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 扩展spring bean的作用域有request,session,global session等-->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- 开始页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/pagenotfound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="default" extends="struts-default">
</package>
</struts>
HTML source:
<head>
<title>DWR Test</title>
<script type='text/javascript' src='http://localhost:8080/wpms/dwr/interface/UserController.js'></script>
<script type='text/javascript' src='http://localhost:8080/wpms/dwr/engine.js'></script>
<script type='text/javascript' src='http://localhost:8080/wpms/dwr/util.js'></script>
</head>
<script type='text/javascript'>
function update() {
var name = dwr.util.getValue("demoName");
UserController.getUserName(name, reply0);
}
var reply0=function(data) {
dwr.util.setValue("forward", data, { escapeHtml:false });
}
</script>
<p>
String:
<input type="text" id="demoName" />
<input value="Send" type="button" onclick="update()" />
<br />
Reply:
<div id="forward"></div>
</p>
UserController.java
import java.io.IOException;
import javax.servlet.ServletException;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
@Controller
@RemoteProxy
public class UserController {
public String getUserName(String id) throws ServletException, IOException {
System.out.println("id=" + id);
WebContext wctx = WebContextFactory.get();
return wctx.forwardToString("/my/forward.html");
}
}
分享到:
相关推荐
对于初学者来说,这个项目提供了一个很好的实践平台,可以学习到如何结合JSP、Servlet、DWR和jQuery进行Web开发,以及如何设计和使用SQL2000数据库。通过逐步理解和修改源代码,可以加深对这些技术的理解,并提升...
Ti平台提供了一个统一的API,开发者可以通过它访问设备硬件和操作系统功能,而无需了解底层的原生代码。 ### AppengineJS **AppengineJS**可能是指使用JavaScript开发Google App Engine应用程序的能力。Google App...
DWR是一个用于简化AJAX技术应用的Java库,它允许JavaScript直接调用服务器端的Java方法。这使得在不重新加载页面的情况下,动态地获取和更新服务器端的数据成为可能。在本文提供的代码片段中,我们看到了如何在DWR...
最后利用一个商品信息管理系统和一个企业任务管理系统,向读者演示了ExtJS在实际项目中的应用以及实现流程。 《精通JS脚本之ExtJS框架》附有配套光盘,提供了书中实例的源代码和视频教学文件。此外,读者还可以...
最后利用一个商品信息管理系统和一个企业任务管理系统,向读者演示了ExtJS在实际项目中的应用以及实现流程。 《精通JS脚本之ExtJS框架》附有配套光盘,提供了书中实例的源代码和视频教学文件。此外,读者还可以...