First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-219  评论-1047  文章-0  trackbacks-0
很多用户(其中也包括我)害怕失去那些自己所熟悉的东西,比如框架,开发环境等。在这篇随笔中,您将看到 如何 在Grails中利用我们所熟知的框架。其中涉及到配置Spring,配置web.xml和配置dwr.xml,而配置Hibernate在 Groovy轻松入门——Grails实战之遗留数据库处理篇 》中已经讲解过了,所以本篇随笔不再累述。 我以利用DWR框架为例稍作讲解。

Grails无需任何配置,但不阻止我们配置

在Grails0.6+中,配置稍有不同,详见 朝花夕拾——Groovy & Grails

1,“grails create-app dwrDemo”,创建Grails工程
2,将dwr.jar(https://dwr.dev.java.net/files/documents/2427/56756/dwr.jar)放到lib目录中(我使用的是DWR2)
3,给web-app\WEB-INF\web.template.xml添加如下内容(
我们可以通过修改web.template.xml来达到配置web.xml的目的 ):
     < servlet >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< servlet-class > org.directwebremoting.servlet.DwrServlet </ servlet-class >
        
< init-param >
            
< param-name > debug </ param-name >
            
< param-value > true </ param-value >
        
</ init-param >
    
</ servlet >
    
< servlet-mapping >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< url-pattern > /dwr/* </ url-pattern >
    
</ servlet-mapping >
web-app\WEB-INF\web.template.xml的最终内容如下所示:
<? xml version="1.0" encoding="UTF-8" ?>
< 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 > log4jConfigLocation </ param-name >
        
< param-value > /WEB-INF/classes/log4j.properties </ param-value >
    
</ context-param >   
    
    
< context-param >
      
< param-name > contextConfigLocation </ param-name >
      
< param-value > /WEB-INF/applicationContext.xml </ param-value >
    
</ context-param >

    
< context-param >
        
< param-name > webAppRootKey </ param-name >
        
< param-value > dwrtest </ param-value >
    
</ context-param >

    
< filter >
           
< filter-name > sitemesh </ filter-name >
        
< filter-class > org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter </ filter-class >
    
</ filter >

     
< filter >
        
< filter-name > charEncodingFilter </ filter-name >
        
< filter-class > org.springframework.web.filter.DelegatingFilterProxy </ filter-class >
        
< init-param >
          
< param-name > targetBeanName </ param-name >
          
< param-value > characterEncodingFilter </ param-value >
        
</ init-param >
        
< init-param >
          
< param-name > targetFilterLifecycle </ param-name >
          
< param-value > true </ param-value >
        
</ init-param >
      
</ filter >

    
< filter-mapping >
        
< filter-name > charEncodingFilter </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >

    
< filter-mapping >
        
< filter-name > sitemesh </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >

    
< listener >
      
< listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >
    
</ listener >     
    
    
< listener >
      
< listener-class > org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener </ listener-class >
    
</ listener >       

    
<!--  Grails dispatcher servlet  -->
    
< servlet >
        
< servlet-name > grails </ servlet-name >
        
< servlet-class > org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet </ servlet-class >
        
< load-on-startup > 1 </ load-on-startup >
    
</ servlet >

    
<!--  The Groovy Server Pages servlet  -->
      
< servlet >
        
< servlet-name > gsp </ servlet-name >
        
< servlet-class > org.codehaus.groovy.grails.web.pages.GroovyPagesServlet </ servlet-class >
     
</ servlet >

    
< servlet-mapping >
        
< servlet-name > gsp </ servlet-name >
        
< url-pattern > *.gsp </ url-pattern >
    
</ servlet-mapping >
   
   
<!-- DWR servlet  -->
     < servlet >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< servlet-class > org.directwebremoting.servlet.DwrServlet </ servlet-class >
        
< init-param >
            
< param-name > debug </ param-name >
            
< param-value > true </ param-value >
        
</ init-param >
    
</ servlet >
    
< servlet-mapping >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< url-pattern > /dwr/* </ url-pattern >
    
</ servlet-mapping >

    
< welcome-file-list >
         
<!--
         The order of the welcome pages is important.  JBoss deployment will
         break if index.gsp is first in the list.
         
-->
         
< welcome-file > index.jsp </ welcome-file >
         
< welcome-file > index.gsp </ welcome-file >
    
</ welcome-file-list >

    
< jsp-config >
        
< taglib >
            
< taglib-uri > http://java.sun.com/jsp/jstl/core </ taglib-uri >
            
< taglib-location > /WEB-INF/tld/c.tld </ taglib-location >
        
</ taglib >
        
< taglib >
            
< taglib-uri > http://java.sun.com/jsp/jstl/fmt </ taglib-uri >
            
< taglib-location > /WEB-INF/tld/fmt.tld </ taglib-location >
        
</ taglib >
        
< taglib >
            
< taglib-uri > http://www.springframework.org/tags </ taglib-uri >
            
< taglib-location > /WEB-INF/tld/spring.tld </ taglib-location >
        
</ taglib >
        
< taglib >
            
< taglib-uri > http://grails.codehaus.org/tags </ taglib-uri >
            
< taglib-location > /WEB-INF/tld/grails.tld </ taglib-location >
        
</ taglib >
    
</ jsp-config >

</ web-app >

4,在src\java下,建立如下目录结构:
    edu
    └─ecust
        ├─service
        │  │  Service.java
        │  │  
        │  └─impl
        │          SomeServiceImpl.java
        │          
        └─test
                Hello.java

Hello.java
package  edu.ecust.test;

import  edu.ecust.service.Service;

public   class  Hello {
    
private  Service service;
    
    
public   void  setService(Service service) {
        
this .service  =  service;
    }
    
    
public  String hello(String name) {
        
boolean  result  =  service.doSomething(name);
        
return  result  ?   " Welcome  "   +  name :  " Hello  "   +  name;
    }
}

Service.java
package  edu.ecust.service;

public   interface  Service {
    
public   boolean  doSomething(String name);
}

SomeServiceImpl.java
package  edu.ecust.service.impl;

import  edu.ecust.service.Service;

public   class  SomeServiceImpl  implements  Service {
    
public   boolean  doSomething(String name) {
        
if  ( " Daniel " .equals(name)) {
            
return   true ;
        } 
else  {
            
return   false ;
        }
    }
}

5,修改spring\resources.xml的内容 我们可以通过修改 resources.xml 来达到配置Spring的目的 ):
<? 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.0.xsd"
>
    
    
< bean  id ="someService"  class ="edu.ecust.service.impl.SomeServiceImpl"   />
    
< bean  id ="welcome"  class ="edu.ecust.test.Hello" >
        
< property  name ="service"  ref ="someService"   />
    
</ bean >
</ beans >

6,在web-app\WEB-INF目录下新建dwr.xml,修改内容如下:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd" >
< dwr >
    
< allow >
        
< create  creator ="spring"  javascript ="welcome" >
            
< param  name ="beanName"  value ="welcome"   />
        
</ create >
    
</ allow >
</ dwr >

7,在web-app\js目录下新建hello.js,修改内容如下:
function  hello() {
   
var  name  =  $('name').value;
    welcome.hello(name, callback);
}
 
function  callback(msg) {
    DWRUtil.setValue('result', msg);

8,“grails create-controller User”创建一个UserController
9,修改grails-app\controllers\UserController.groovy内容:

class  UserController {
    def hello 
=  {}
}

10,在grails-app\views\user目录下新建新建hello.gsp,修改内容如下:
< html >
    
< head >
      
< script  type ='text/javascript'  src ='<%=request.getContextPath()% > / dwr / interface / welcome.js' > </ script >
      
< script  type ='text/javascript'  src ='<%=request.getContextPath()% > / dwr / engine.js' > </ script >
      
< script  type ='text/javascript'  src ='<%=request.getContextPath()% > / dwr / util.js' > </ script >
      
< script  type ='text/javascript'  src ='<%=request.getContextPath()% > / js / hello.js' > </ script >
    
</ head >
    
< body >
     
        Name: 
< input  id ="name"  type ="text"  onKeyup ='hello();' />
         
        
< div  id ="result" ></ div >
    
</ body >
</ html >
11,“grails run-app”,启动这个Web应用程序
12,访问http://localhost:8080/dwrDemo/user/hello,并输入Daniel,注意观察效果:

Name:
Hello Dan

Name:
Welcome Daniel


附:朝花夕拾——Groovy & Grails
posted on 2007-06-22 21:44 山风小子 阅读(3845) 评论(2)  编辑  收藏 所属分类: Groovy & GrailsAjax