Groovy輕鬆入門—Grails實戰之遺留框架利用

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  很多用戶(其中也包括我)害怕失去那些自己所熟悉的東西,比如框架,開發環境等.在這篇隨筆中,您將看到 如何 在Grails中利用我們所熟知的框架.其中涉及到配置Spring,配置web.xml和配置dwr.xml,而配置Hibernate在 《 Groovy輕鬆入門——Grails實戰之遺留資料庫處理篇 》中已經講解過了,所以本篇隨筆不再累述. 我以利用DWR框架為例稍作講解.

  Grails無需任何配置,但不阻止我們配置

  在Grails0.6 中,配置稍有不同

  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的目的 ):

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/     < servlet >     < servlet-name > dwr-invoker     < servlet-class > org.directwebremoting.servlet.DwrServlet     < init-param >     < param-name > debug     < param-value > true        

     < servlet-mapping >   < servlet-name > dwr-invoker   < url-pattern > /dwr/*

  

  web-app\WEB-INF\web.template.xml的最終內容如下所示:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/   <? 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下,建立如下目錄結構:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/       edu   └─ecust   ├─service   │  │  Service.java   │  │   │  └─impl   │          SomeServiceImpl.java   │   └─test   Hello.java    

  Hello.java

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/     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的目的 ):

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/           

<? 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,修改內容如下:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/   <? 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,修改內容如下:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/     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內容:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/           

  class UserController {   def hello = {}   }

  10,在grails-app\views\user目錄下新建新建hello.gsp,修改內容如下:

   Code highlighting produced by Actipro CodeHighlighter (freeware)   http://www.CodeHighlighter.com/   < 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,注意觀察效果:





[火星人 ] Groovy輕鬆入門—Grails實戰之遺留框架利用已經有648次圍觀

http://coctec.com/docs/java/show-post-59779.html