Posted on 2007-04-23 23:31
javabright 阅读(2799)
评论(4) 编辑 收藏 所属分类:
编程语言
下面讲一下DWR与Spring的集成:
还是上面那个例子用Spring搞定大部分和上面一样
新建类User(包换了)
代码
- package org.li.dwr.spring;
-
- import java.util.List;
-
- import org.li.dwr.Book;
-
- public class User
- {
- private String welcome;
- private String username;
- private String address;
- private List<Book> books;
- private int age;
- public String getAddress()
- {
- return address;
- }
- public void setAddress(String address)
- {
- this.address = address;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public String getUsername()
- {
- return username;
- }
- public void setUsername(String username)
- {
- this.username = username;
- }
- public String getWelcome()
- {
- return welcome;
- }
- public void setWelcome(String welcome)
- {
- this.welcome = welcome;
- }
- public List<Book> getBooks()
- {
- return books;
- }
- public void setBooks(List<Book> books)
- {
- this.books = books;
- }
- public User getUser(String welcome)
- {
- this.welcome=welcome;
- return this;
- }
- }
Book还是以前那个
在src下新建application.xml文件(spring的配置文件)
代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
-
- <beans>
- <bean id="user" class="org.li.dwr.spring.User">
- <property name="username">
- <value>javafish</value>
- </property>
- <property name="age">
- <value>21</value>
- </property>
- <property name="address">
- <value>北京市海淀区</value>
- </property>
- </bean>
- </beans>
在dwr.xml加入:
代码
- <create javascript="springUser" creator="spring">
- <param name="beanName" value="user"></param>
-
- <param name="location" value="applicationContext.xml"></param>
- </create><convert match="org.li.dwr.spring.User" converter="bean"></convert>
新建springUserInfo.html
代码
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>UserInfo.html</title>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <script type='text/javascript' src='dwr/interface/springUser.js'></script>
- <script type='text/javascript' src='dwr/engine.js'></script>
- <script type='text/javascript' src='dwr/util.js'></script>
- <script type="text/javascript" src="springuserInfo.js"></script>
-
- </head>
-
- <body>
- 请输入你的名字:
- <input id="name" type="text"/>
- <input id="jbutton" type="button" value="得到javafish(spring)的信息"/>
- <div id="result"></div>
- <script>load()</script>
- </body>
- </html>
新建springUserInfo.js
代码
- function load()
- {
- var jbutton = $("jbutton");
- jbutton.onclick=function(event)
- {
- bOnClick();
- };
- var sbutton = $("sbutton");
- sbutton.onclick=function(event)
- {
- sOnClick();
- };
- }
- function bOnClick()
- {
- springUser.getUser($("name").value,callback);
- }
- function callback(msg)
- {
- var user = msg;
- DWRUtil.setValue('result',"欢迎你!"+user.welcome+" 姓名:"+user.username+",年龄:"+user.age+",住址:"+user.address);
- }
在web.xml中加入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
运行服务器输入http://localhost:8080/DWRStudy/springUserInfo.html,可以看到运行结果
主要的区别是dwr.xml的配置,需要在creator=spring,然后里面的<param>的name用beanName,值是spring的配置文件里的bean的id。Name=location是指定spring的配置文件的路径(如果不指定DWR可以根据web.xml自动找到spring的主配置文件)。