dwr+spring的简单配置

上一篇把dwr的配置做了一下,此篇记录dwr+spring的配置,此篇扔需用到上一篇的dwr配置
1. spring 包引入
2. spring的配置
3. dwr对spring的配置
4. web.xml的spring配置

1.spring包引入
手动引入进来主要的引两个包就可以了(spring.jar和spring-core.jar)此时的包为:

dwr+spring的简单配置 - testblog - testblog1 的博客

 2.spring配置
在web-inf下建一个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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="book" class="ssh.com.bean.Book">
        <property name="name" value="dwr"/>
        <property name="isbm" value="xxxxxxxx" />
        <property name="author" value="force2002"/>
    </bean>
</beans>

spring能对bean里的的属性进行set和get操作,所在这里的确属性一定要有setter和getter方法,spring才能用这两个方法操作
Book.java的代码如下:
package ssh.com.bean;
public class Book {
    private String name;
    private String isbm;
    private String author;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getIsbm() {
        return isbm;
    }
    public void setIsbm(String isbm) {
        this.isbm = isbm;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getBookName(String userName){
        return "get the book: " +userName;
    }
    public String getBookinfo(){
        return "The book info: " +" bookName: "+this.getName()+" isbm: "+this.getIsbm()+" author: "+this.getAuthor();
    }
}
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>
        <create javascript="BookBean" creator="spring">
          <param name="beanName" value="book"/>
        </create>
        <convert match="ssh.com.bean.Book" converter="bean"/>
    </allow>
</dwr>
这里的book是在applicationContext.xml里的id为book的bean对应的映射关系。
这里的creator是spring,在加载dwr之前spring框加要事先加载好,这个需要在web.xml里,把spring的配置代码放在dwr配置之前
把所有的bean 都转化一下,如果bean很多的话
4.web.xml配置spring
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <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>    

    <servlet>
        <servlet-name>dwr</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dwr</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
5.测试
testdssh.html代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>testdssh.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=GB18030">
    <script type='text/javascript' src='http://testblog1.blog.163.com/blog/dwr/interface/BookBean.js'></script>
    <script type='text/javascript' src='http://testblog1.blog.163.com/blog/dwr/engine.js'></script>
    <script type='text/javascript' src='http://testblog1.blog.163.com/blog/dwr/util.js'></script>
    <script type="text/javascript" src="http://testblog1.blog.163.com/blog/testdssh.js"></script>
  </head>
  <body>
       <span>请输入书名:</span><input type="text" id="name"/><input id="getbook" type ="button" value="点击取书(dwr)"/>
     <input id="getbookinfo" type ="button" value="点击取书(spring)"/>
     <div id="result" style="color:red;background:#ccc;width:500px;margin-top:10px;"></div>
     <script>
            load()
     </script>
  </body>
</html>
testdssh.js代码如下:
function load(){
    var getBookBtn = document.getElementById('getbook');
    getBookBtn.onclick = function(event){
        getBook();
    };
    var getbookinfoBtn = document.getElementById('getbookinfo');
    getbookinfoBtn.onclick = function(event){
        getBookInfo();
    };

   
};
function getBook(){
    BookBean.getBookName(document.getElementById('name').value,getBookNameCallBack);
};
function getBookNameCallBack(result){
    document.getElementById('result').innerHTML=result;
};
function getBookInfo(){
    BookBean.getBookinfo(getBookInfoCallBack);
};
function getBookInfoCallBack(info){
    document.getElementById('result').innerHTML=info;
};

测试结果:

dwr+spring的简单配置 - testblog - testblog1 的博客
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值