当前位置: 代码迷 >> Web前端 >> dwr札记整理(三)
  详细解决方案

dwr札记整理(三)

热度:60   发布时间:2012-11-05 09:35:12.0
dwr笔记整理(三)

使用dwr做了一个用户注册名校验的小例子,却发现在dwr配置文件中,dwr与Spring整合时location参数不起作用的问题:

在Spring中配置UserService:

<bean id="userServiceImpl" class="com.kevin.samples.dwr.register.service.impl.UserServiceImpl">
		<property name="userDao">
			<ref bean="userDAO" />
		</property>
	</bean>
	
	<bean id="userService" parent="txProxyTemplate">
		<property name="proxyInterfaces">
			<list>
				<value>com.kevin.samples.dwr.register.service.UserService</value>
			</list>
		</property>
		<property name="target">
			<ref bean="userServiceImpl" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

?在Spring中配置DAO:

<bean id="userDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
	 	<property name="interceptorNames">
		       <value>hibernateInterceptor,userDAOImpl</value>
 		</property>
 	</bean>
 	<bean id="userDAOImpl" class="com.kevin.samples.dwr.register.dao.impl.UserDAOImpl">	
		<property name="sessionFactory">
			<ref bean="hibernateSessionFactory" />
		</property>	   
	</bean>

?

html页面代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="dwr/interface/UserService.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 checkUsername() {
		var username = dwr.util.getValue("username");
		//FirstDWR.sayHello(name,callBack);

		UserService.isValid(username,{
            callback : isValidCallback,
            errorHandler : function(msg){alert("timeout!");},
            timeout : 10000
		});
	}
	var isValidCallback = function(data){
		if (data == true) {dwr.util.setValue("msg","该用户名可用");}
		else if (data == false) {dwr.util.setValue("msg","用户名已被注册过,请重新输入!");}
		//alert(data);
	}
	
</script>
</head>
<body>
<pre>
你的用户名:
<input type="text" id="username" value=""/>&nbsp;
<input type="button" value="校验用户名是否可用" onclick="checkUsername();">
<br>
<span id="msg"></span>
</pre>
</body>
</html>

?

在dwr配置文件中配置UserService,使用代理的UserService接口:

<!-- 注册查看用户名是否已存在示例 -->
<create javascript="UserService" creator="spring" >
<param name="beanName" value="userService"/>
<!-- 为什么不使用location参数时候才起作用? 现在Spring的所有配置文件统一放在classpath:/META-INF/目录下,但是为了测试location参数,将配置文件都移至classpath下时同样不起作用。-->
<!--<param name="location" value="applilcationContext-dao.xml"/>-->
</create>

?

现在的问题是直接配置location参数时候html页面根本找不到UserService, 必须在web.xml中配置Spring文件监听器:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath*:/META-INF/applicationContext-*.xml</param-value>
</context-param>
	
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

?

据说直接配置location参数去找寻Spring配置文件的时候速度上会比从web.xml文件中读取稍快点。究竟怎样location参数才起作用呢,求解!!!

?

  相关解决方案