`

DWR3嵌入一个外部页面

    博客分类:
  • DWR
阅读更多
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");
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics