First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評(píng)論-1047  文章-0  trackbacks-0
          很多用戶(其中也包括我)害怕失去那些自己所熟悉的東西,比如框架,開(kāi)發(fā)環(huán)境等。在這篇隨筆中,您將看到 如何 在Grails中利用我們所熟知的框架。其中涉及到配置Spring,配置web.xml和配置dwr.xml,而配置Hibernate在 Groovy輕松入門(mén)——Grails實(shí)戰(zhàn)之遺留數(shù)據(jù)庫(kù)處理篇 》中已經(jīng)講解過(guò)了,所以本篇隨筆不再累述。 我以利用DWR框架為例稍作講解。

          Grails無(wú)需任何配置,但不阻止我們配置

          在Grails0.6+中,配置稍有不同,詳見(jiàn) 朝花夕拾——Groovy & Grails

          1,“grails create-app dwrDemo”,創(chuàng)建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添加如下內(nèi)容(
          我們可以通過(guò)修改web.template.xml來(lái)達(dá)到配置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的最終內(nèi)容如下所示:
          <? 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下,建立如下目錄結(jié)構(gòu):
          ????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的內(nèi)容 我們可以通過(guò)修改 resources.xml 來(lái)達(dá)到配置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,修改內(nèi)容如下:
          <? 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,修改內(nèi)容如下:
          function ?hello()?{
          ???
          var ?name? = ?$('name').value;
          ????welcome.hello(name,?callback);
          }
          ?
          function ?callback(msg)?{
          ??? DWRUtil.setValue('result',?msg);
          }?

          8,“grails create-controller User”創(chuàng)建一個(gè)UserController
          9,修改grails-app\controllers\UserController.groovy內(nèi)容:

          class ?UserController?{
          ????def?hello?
          = ?{}
          }

          10,在grails-app\views\user目錄下新建新建hello.gsp,修改內(nèi)容如下:
          < 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”,啟動(dòng)這個(gè)Web應(yīng)用程序
          12,訪問(wèn)http://localhost:8080/dwrDemo/user/hello,并輸入Daniel,注意觀察效果:

          Name:
          Hello Dan

          Name:
          Welcome Daniel


          附:朝花夕拾——Groovy & Grails
          posted on 2007-06-22 21:44 山風(fēng)小子 閱讀(4055) 評(píng)論(2)  編輯  收藏 所屬分類: Groovy & GrailsAjax
          主站蜘蛛池模板: 仪征市| 云林县| 海口市| 巴彦淖尔市| 闽侯县| 永平县| 临清市| 灵寿县| 武平县| 永州市| 永善县| 麦盖提县| 翁牛特旗| 莱阳市| 徐汇区| 井陉县| 嘉义市| 霍州市| 图木舒克市| 涡阳县| 内江市| 万载县| 郸城县| 广州市| 仁化县| 原阳县| 慈溪市| 勐海县| 青田县| 天峻县| 泗水县| 鹤庆县| 牙克石市| 宁化县| 屯昌县| 赫章县| 皋兰县| 股票| 交城县| 富锦市| 东台市|