当前位置: 代码迷 >> Web前端 >> DWR Spring 引语 整合
  详细解决方案

DWR Spring 引语 整合

热度:532   发布时间:2012-08-30 09:55:54.0
DWR Spring 注解 整合

1、添加相应jar包

2、配置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">
?<servlet>
??<servlet-name>dwr-invoker</servlet-name>
??<servlet-class>
???org.directwebremoting.servlet.DwrServlet
??</servlet-class>
??<init-param>
???<param-name>config-dwr</param-name>
???<param-value>
????/WEB-INF/dwr.xml
???</param-value>
??</init-param>
??
??<init-param>
???<param-name>debug</param-name>
???<param-value>true</param-value>
??</init-param>
??<!--
??<init-param>
???<param-name>activeReverseAjaxEnabled</param-name>
???<param-value>true</param-value>
??</init-param>
??<init-param>
?? ??<param-name>org.directwebremoting.extend.Remoter</param-name>
?? ??<param-value>
?? ???com.eastcom.sspm.servlet.DWRSessionService
?? ??</param-value>
?? </init-param>
??? -->
?</servlet>
?<servlet-mapping>
??<servlet-name>dwr-invoker</servlet-name>
??<url-pattern>/dwr/*</url-pattern>
?</servlet-mapping>

?<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>


?<welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>

?

3、增加dwr.xml文件

<?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>
?
?<allow>

<!-- 创建实体bean 供DWR转换成js-->
?<convert converter="bean"
??match="com.eastcom.sgs.bean.user.UserInfo">
?</convert>
?<convert converter="bean"
???match="com.eastcom.sgs.common.AllistRange">
?</convert>
?
<!-- ?<create creator="new" javascript="UserInfoAction">
??<param name="class"
???value="com.eastcom.sgs.action.user.UserInfoAction" />
?</create>
?-->

<!-- 因为此处是整合spring 来做的,所以creator = 'spring'? javascript="UserInfoAction"

?????? 表示实例转换成javascript语言后以UserInfoAction 命名,前台页面可以通过(<script type="text/javascript"

?????? src=<%=basePath%>'/dwr/interface/UserInfoAction.js'/>来调用。param 元素的name属性值可以是class、beanName等,此处用beanName、value的值是定义在applicationContext.xml中的某个bean的id值,此处使用spring注解来自动生成bean,默认为类名字 首个字母小写

-->


?<create creator="spring" javascript="UserInfoAction">
??<param name="beanName"
???value="userInfoAction" />
?</create>
?</allow>
</dwr>

?

4、创建一个实体bean 供DWR转换成js,

?

5、

在类中使用注解驱动ioc

@Controller
public class UserInfoAction {

?

?@Autowired
?private IUserInfoManager userInfoManager;

?

无需写 setter/getter方法 spring 容器 自动生成id为 userInfoAction 的bean

?

}

?

?

@Service
public class UserInfoManagerImpl implements IUserInfoManager {
?@Autowired
?private IUserInfoDao userinfoDao;

?

}

?

@Repository
public class UserInfoDaoImpl implements IUserInfoDao {

?

}

?

在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"
?xmlns:context="http://www.springframework.org/schema/context"
?xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
?xmlns:tx="http://www.springframework.org/schema/tx"
?xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
?????? http://www.springframework.org/schema/aop
?????? http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
?????? http://www.springframework.org/schema/task
?????? http://www.springframework.org/schema/task/spring-task-3.0.xsd
?????? http://www.springframework.org/schema/context
??? http://www.springframework.org/schema/context/spring-context-3.0.xsd
??? http://www.springframework.org/schema/tx
??? http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 对web包中的所有类进行扫描,以完成bean创建和自动依赖注入的功能-->

?<context:component-scan base-package="com.eastcom.sgs" />
?<context:annotation-config /><!-- 隐藏注册了多个对注释进行解析处理的处理器-->

</beans>

6、前台页面处理

  相关解决方案