﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-thinktalk-随笔分类-Groovy&amp;Grails</title><link>http://www.blogjava.net/ThinkingTalking/category/42146.html</link><description>Do what I can do!</description><language>zh-cn</language><lastBuildDate>Tue, 13 Oct 2009 17:13:42 GMT</lastBuildDate><pubDate>Tue, 13 Oct 2009 17:13:42 GMT</pubDate><ttl>60</ttl><item><title>【转】Grails开发者需要知道的10个技巧</title><link>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298035.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Tue, 13 Oct 2009 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298035.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/298035.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298035.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/298035.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/298035.html</trackback:ping><description><![CDATA[引用自：http://www.groovycn.com/2009/02/grails-10-tips/<br />
<p>如果要列举近些年来Java领域中最令人激动的事件，Grails的诞生绝对应该榜上有名。在打开<a href="http://www.grails.org/">Grails官方网站</a>时，
你会在浏览器的标题栏上看到这样的内容：&#8220;Grails - 寻找就此结束&#8221;。虽然这句话不见得适用于所有场合，但对于不少从事Java
Web开发的人来说，赞同者恐怕不在少数。由于具备Rails风格的框架、兼容绝大多数Java语法的Groovy动态语言、以业界口碑良好的框架为基
础、对企业现有Java开发资产的保护、Plugin架构等特点，再加上Spring社区的强力介入，Grails行情看涨应该是意料之中的。</p>
<p>然而Grails却没有看上去那么简单，在其包装的糖衣之下，有着许多作为Java Web开发者不得不了解的知识。最近，<a href="http://blog.peterdelahunty.com/">Peter Delahunty</a>发表了题为<a href="http://blog.peterdelahunty.com/2009/02/10-ten-tips-for-grails-developer.html">Grails开发者需要知道的10个技巧</a>的博文。不要被标题所迷惑了，这里所说的&#8220;技巧&#8221;可不是那些语言层面的&#8220;技巧&#8221;，把它们称为&#8220;建议&#8221;恐怕更合适一些。<br />
<br />
技巧1：把Grails的书籍买来读读。对此，Peter Delahunty推荐了已出版的《Definitive Guide to Grails
2nd edition》和《Groovy and Grails Recipes》，以及即将出版的《Grails in Action》。</p>
<p>技巧2：学习Groovy，并按照Groovy进行思考。Peter Delahunty通过一个例子说明了Java思维和Groovy思维的不同。</p>
<blockquote>
<p>这儿有个好例子：</p>
<p>传播操作符（spread operator）：*</p>
<p>在Java中，要想遍历一组人并收集人名。我可能得这样写：</p>
<pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; width: 100%; color: #000000; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; background-color: #eeeeee;"><code>List names = new ArrayList();<br />
<br />
for (Iterator iterator = people.iterator(); iterator.hasNext();) {<br />
<br />
Person person = (Person) iterator.next();<br />
<br />
names.add(person.getName());<br />
<br />
}<br />
<br />
System.out.println(names);<br />
<br />
</code></pre>
<p>用Groovy，我会这么写：</p>
<pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; width: 100%; color: #000000; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; background-color: #eeeeee;"><code>def names = people*.name<br />
<br />
println names<br />
<br />
</code></pre>
<p>呵呵，简单吧。我还可以这样写：</p>
<pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; width: 100%; color: #000000; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; background-color: #eeeeee;"><code>def names = people.collect {it.name}<br />
<br />
println names<br />
<br />
</code></pre>
</blockquote>
<p>在这个建议中，他列举了一些资源：</p>
<ul>
    <li><a href="http://groovy.codehaus.org/">Groovy官方网站</a></li>
    <li><a href="http://pleac.sourceforge.net/pleac_groovy/index.html">PLEAC</a></li>
</ul>
<p>以及一些书籍：</p>
<ul>
    <li>Programming Groovy</li>
    <li>Groovy Recipies</li>
    <li>Groovy in action</li>
</ul>
<p>技巧3：了解Grails的各个插件。Peter Delahunty给出的理由：Grails本身是个插件系统，而且现在已经有大量的插件可供下载。在自行实现之前，不妨看看其中是否有称手的。</p>
<p>技巧4：学习Hibernate和Spring。这一点不难理解，既然Grails是建立在这些工具之上的，了解这些工具本身的使用，对于熟练使用Grails自然大有脾益。</p>
<p>技巧5：使单元测试、集成测试和一般测试的自动化。Peter Delahunty在这里推荐了两个Grails的插件：<a href="http://www.grails.org/Testing+Plugin">Testing Plugin</a>和<a href="http://www.grails.org/Grails+Functional+Testing">Functional testing plugin</a>。前者将成为Grails 1.1的内置插件。</p>
<p>技巧6：使用好的IDE。Peter Delahunty表示，他自己使用的是Intellij，但对于Netbeans也寄予厚望。</p>
<p>技巧7：学习并使用HTML W3C标准。&#8220;遵循标准将消除你90%的问题&#8221;，Peter Delahunty如是说。</p>
<p>技巧8：看上去不错一定不错。这其实说的是&#8220;面子工程&#8221;，通过提高应用的外观设计来提高应用的竞争力。Peter Delahunty给出了2个资源：</p>
<ul>
    <li><a href="http://99designs.com/">99designs.com</a></li>
    <li><a href="http://www.roundedcornr.com/">圆角矩形产生器</a></li>
</ul>
<p>技巧9：使用Firefox上的Firebug。对于浏览器端的调试工作，Firebug绝对是一个必备工具。</p>
<p>技巧10：跟上Groovy和Grails社区的步伐。在这最后一个建议中，Peter Delahunty给出了获取Groovy和Grails信息的主要资源链接：</p>
<ul>
    <li><a href="http://www.grails.org/">Grails官方网站</a></li>
    <li><a href="http://groovyblogs.org/entries/recent">Groovy和Grails相关的博客</a></li>
    <li><a href="http://twitter.com/graemerocher">Grails创始人Graeme Rocher的Twitter</a></li>
    <li><a href="http://twitter.com/glaforge">Groovy项目领导人Guillaume Laforge的Twitter</a></li>
    <li><a href="http://twitter.com/glen_a_smith">Groovyblogs.org的创始人Glen Smith</a></li>
</ul>
<p>请务必阅读Peter Delahunty的<a href="http://blog.peterdelahunty.com/2009/02/10-ten-tips-for-grails-developer.html">原文</a>。对于Grails的新老使用者，你都可以从Peter Delahunty的建议中有所收获。关于InfoQ中文站Groovy和Grails的其它内容，请访问<a href="http://www.infoq.com/cn/groovy">这里</a>和<a href="http://www.infoq.com/cn/grails">这里</a>。</p>
<br />
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/298035.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2009-10-13 10:51 <a href="http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298035.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Grails in Eclipse</title><link>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298034.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Tue, 13 Oct 2009 02:44:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298034.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/298034.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298034.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/298034.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/298034.html</trackback:ping><description><![CDATA[<h2>Importing a Grails Project</h2>
<p class="paragraph">Grails
automatically creates Eclipse project and classpath files for you. The
Groovy plugin is helpful for working with Grails projects, but not
required in order to import a project: do not rely on it for
assistance.To import a Grails project just right-click in the "Package
Explorer" and select "Import" then "Existing project into Workspace"
and "Browse" to the location of your project.</p>
<p class="paragraph">Then
immediately click "Ok" followed by "Finish" and your project will be
set-up. Using other methods of importing the project may prove to be
problematic due to compile location.
</p>
<blockquote class="note">There is a known issue that Grails will
not run properly if you try to use the root directory of your drive as
your project, or paths with spaces.
</blockquote>
<br />
<br />
<p class="paragraph"><a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Setting up Eclipse Environment Variables"></a></p>
<h2>Setting up Eclipse Environment Variables</h2>
<p class="paragraph">You
will need to create an Eclipse classpath variable called GRAILS_HOME
that points to the location where you installed Grails for the project
to build successfullly by going to Windows -&gt; Preferences&#8230; -&gt;
Java -&gt; Build path -&gt; Classpath Variables -&gt; New.</p>
<p class="paragraph">If
you don't already have GRAILS_HOME as part of your standard
environment, in order to run your Grails project, you will need to
establish that value in the Run dialog (Run...) under the Environment
tab, to the same value as above.</p>
<p class="paragraph">If your Grails
project has Grails plugins installed then you will need to add the
directories directly under the plugins/src/ folder to your Eclipse
source path. This is done through right clicking on the folder (usually
called "Groovy" or "Java") then selecting build path -&gt; source
folder. Otherwise you may get errors in the import statements.</p>
<p class="paragraph">Also,
if you are using JSP you will need to add your JDK's tools.jar library
to your project classpath otherwise jetty will display compilation
errors. See this page for info: <a href="http://nuin.blogspot.com/2005/05/launch-jetty-from-eclipse-solving.html" class="pageLink">http://nuin.blogspot.com/2005/05/launch-jetty-from-eclipse-solving.html</a>
</p>
<blockquote class="note">
If you are using the <a href="http://groovy.codehaus.org/Eclipse+Plugin" class="pageLink">Eclipse Groovy plugin</a> then make sure you <strong class="bold">enable</strong>
the preference 'Disable Groovy Compiler Generating Class Files' (newer
versions: Project -&gt; Preferences -&gt; Groovy Project Preferences /
older versions: Windows -&gt; Preferences -&gt; Groovy Preferences). By
default this option is disabled and generates class files for your
groovy files, and stores them in the basedir of your Grails project.
When these class files are generated, unexpected behaviour is
encountered like not able to generate controllers and views for your
domain classes.
<p class="paragraph">THIS DOES NOT APPLY IF YOU INTEND TO USE DEBUGGING (read the section 'Step debugging' below)
</p>
</blockquote>
<br />
<br />
<p class="paragraph"><a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Running a Grails application"></a></p>
<h2>Running a Grails application</h2>
<p class="paragraph">Grails
also automatically creates an Eclipse run configuration for you. To run
the project you are in use the "Run" dropdown to open the "Run" dialog
then under "Java Applications" find the name of your project select it
and click "Run". The Grails application will load embedded inside
Eclipse, any changes made inside the Eclipse project can be <a href="http://www.grails.org/Auto+Reloading" class="pageLink">auto-reloaded</a>.</p>
<p class="paragraph">If you are getting exceptions like
</p>
<div class="code">
<pre>org.mortbay.util.MultiException[java.io.FileNotFoundException: {yourpath}/web-app]<br />
at org.mortbay.http.HttpServer.doStart(HttpServer.java:731)<br />
at org.mortbay.util.Container.start(Container.java:72)<br />
at grails.util.GrailsMain.main(GrailsMain.java:67)</pre>
</div>
then you need to do is run the ant target:
<br />
<div class="code">
<pre>grails dev <span class="java-keyword">package</span></pre>
</div>
which will package up grails into the web-app directory in the same way
as the "grails run-app" command will. This is then used as the base for
eclipse to execute.
<p class="paragraph">For some other errors or quirky behavior, it is often helpful to run the command
</p>
<div class="code">
<pre>grails clean</pre>
</div>
<p class="paragraph"><a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Step Debugging with Eclipse"></a></p>
<h2>Step Debugging with Eclipse</h2>
<p class="paragraph">You can step debug a Grails application if you install the latest build of the <a href="http://groovy.codehaus.org/Eclipse+Plugin" class="pageLink">Groovy Eclipse</a> plug-in.
</p>
<blockquote class="note">
Eclipse debugging depends on the class files generated by the Groovy
compiler, so make sure 'Disable Groovy Compiler Generating Class Files'
is <strong class="bold">not</strong>
checked (Project -&gt; Properties -&gt; Groovy Project Properties) and
the output folder is set to e.g. "bin-groovy". Then edit the
".classpath" file in your project directory and manually remove the
entry for "bin-groovy".
</blockquote>
To debug an application, do the following:
<br />
<ul class="star">
    <li>Open up the Groovy file you want to debug (lets say a controller
    called {{BookController.groovy}}) then set a break point on the line
    you want to stop at. (set a breakpoint by double clicking in the far
    left "gutter")</li>
    <li>Now debug the application in the same way as you did in the above
    section on "Running a Grails application" except with the "Debug" menu.</li>
    <li>Load your browser and navigate to the action that calls the code.</li>
</ul>
Eclipse should then stop at the appropriate place and you can then
inspect variables and step through the code. If you have the Grails
source code set-up on your Source path you can step into that code too.
<p class="paragraph"><a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Adding domain classes etc."></a></p>
<h2>Adding domain classes etc.</h2>
<p class="paragraph"> You can configure grails as an "External Tool", allowing you to create domain classes etc. from within Eclipse:
</p>
<ol>
    <li>Select "Run &gt; External Tools &gt; External Tools Configurations...".</li>
    <li>Double-click "Program"</li>
    <li>Enter a name of "Grails".</li>
    <li>Browse the file system for the location of grails or grails.bat.</li>
    <li>Set the working directory to "${project_loc}".</li>
    <li>Set the arguments as "${string_prompt}".</li>
    <li>Under the "Refresh" tab, set to refresh the "project containing the selected resource" upon completion.</li>
    <li>Under the "Common" tab, tick "Display in favorites menu".</li>
</ol>
You can then run Grails by clicking the down arrow next to the
external tool icon (run with a suitcase) and selecting "Grails", for
example:
<ol>
    <li>Choose "Grails" from the external tools drop-down.</li>
    <li>Enter "create-domain-class" in the dialogue box.</li>
    <li>In the console window, follow the prompts as normal.</li>
    <li>Open your new class from grails-app/domain.</li>
</ol>
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Editing GSP files"></a>
<h2>Editing GSP files</h2>
<p class="paragraph">The gsp files are just jsp files with a special taglib. The extension gsp has to be added as follows:
</p>
<ul class="star">
    <li>General -&gt; Editors -&gt; File Associations: add *.gsp and link the JSP Editor to it.</li>
    <li>General -&gt; Content Types Fold out Text -&gt; JSP and add *.gsp</li>
</ul>
This enables jsp editing for the gsp files. To enable code
completion add the following at the start of the gsp (Grails before
version 1.1 only):
<div class="code">
<pre>&lt;%@ taglib prefix=<span class="java-quote">"g"</span> uri=<span class="java-quote">"/web-app/WEB-INF/tld/grails.tld"</span> %&gt;</pre>
</div>
Since Grails 1.1 this <a href="http://jira.codehaus.org/browse/GRAILS-3264" class="pageLink">no longer works</a>.
<p class="paragraph">
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Unit Tests"></a></p>
<h2>Unit Tests</h2>
<p class="paragraph">To
run Grails/Groovy unit tests, the easiest way is just to create a junit
run configuration, but specifying "groovy.util.AllTestSuite" as the
main class. More info on general groovy unit testing in grails <a href="http://groovy.codehaus.org/Unit+Testing" class="pageLink">here</a></p>
<p class="paragraph">To
run a single test, create a run-config as described above or duplicate
an existing entry. Select "Run a single test", select your project and
the test class. Save and test your config. You can also rerun a single
test in the (g)unit test view by clicking with the right mouse button
on an single test and selecting "Run".</p>
<p class="paragraph"><a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Troubleshooting"></a></p>
<h2>Troubleshooting</h2>
<ol>
    <li>If you getting the following error every time you save *.groovy file in your project:</li>
</ol>
<div class="code">
<pre>An internal error occurred during: <span class="java-quote">"Building workspace"</span>.<br />
BUG! exception in phase 'conversion' in source unit '...' Error:<br />
duplicate class declaration <span class="java-keyword">for</span> name: &#8230; and class:<br />
org.codehaus.groovy.ast.ClassNode@...[name:...]</pre>
</div>
<br />
<br />
try the following:
<ol>
    <ul class="star">
        <li>Right click on package explorer's project name and open "Properties..." dialog.</li>
        <li>Select "Builders" and make sure that "Groovy Builder" is checked.</li>
        <li>Select "Java Compiler/Building" and check "Enable project specific settings".</li>
        <li>Append "<strong class="bold">*.groovy" to "Filtered Resources" text field, to prevent "</strong>.groovy" files being copied to the output directory. Note that the path separator is "," not ";"</li>
        <li>Apply changes and close dialog.</li>
        <li>Clean your project. Make sure that the output directory doesn't contain "*.groovy" files</li>
    </ul>
    <li>If Eclipse complains that it can not find the sources where breakpoints are set ensure the following:</li>
    <ul class="star">
        <li>Step filtering is enabled for groovy.*, org.codehaus.* (more about this <a href="http://groovy.codehaus.org/Debugging+with+Eclipse" class="pageLink">http://groovy.codehaus.org/Debugging+with+Eclipse</a> )</li>
        <li>Build out put for the project is in a separate folder (say /bin ).
        Make sure this is present for Project-&gt;Properties-&gt;Groovy Project
        Properties</li>
    </ul>
    <li>Few other recommendations:</li>
    <ul class="star">
        <li>Make sure to add all the libraries from grails-app/lib folder to the project</li>
        <li>Using verbose logging/tracing through log4j.properties until
        everything works from the Eclipse IDE. Sometimes if some library is not
        found, unless it is printed in the console, it is difficult to find out
        what went wrong.</li>
        <li>Make sure to use the same JDK for tools.jar as well as the runtime
        jar rt.jar. Do note sometimes using the JRE instead of rt.jar causes
        problems.</li>
        <li>Sometimes "launch configuration" generated by grails is not in sync
        with the way ant's build.xml works. In Grails 0.3 ensure that following
        VM parameters (do note not program arguments, but VM arguments ) are
        set "-Dorg.mortbay.xml.XmlParser.NotValidating=true". Without this
        there will be XML Parser exception thrown when launched from the IDE.</li>
    </ul>
</ol>
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="Brute Force method:"></a>
<h2>Brute Force method:</h2>
<p class="paragraph">If
running from IDE has issues, most likely it is because some library is
missing. Ensure that all libraries under GRAILS_HOME/dist,
GRAILS_HOME/ant/lib, GRAILS_HOME/lib and your project folder/lib are
added to the build path of the project. </p>
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/298034.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2009-10-13 10:44 <a href="http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298034.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>搭建Grails开发环境</title><link>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298033.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Tue, 13 Oct 2009 02:40:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298033.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/298033.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298033.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/298033.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/298033.html</trackback:ping><description><![CDATA[1、JDK安装。<br />
2、到官网上下载grails。http://www.grails.org/Download<br />
3、解压，配置GRAILS_HOME，Path<br />
4、在命令行输入：grails。如果出现帮助信息，说明安装成功。<br />
5、开始应用程序。<br />
选择一个工作目录，在命令行输入：grails create-app demo。<br />
会在当前目录下生成一个demo文件夹，里面有自动生成的好多文件，这就是一个grails项目。<br />
The "create-app" target created a Grails data source artifact for you
in the "&lt;..&gt;/grails-app/conf" directory called DataSource.groovy
with closures for each of the standard environments: Development,
TestData, and Production.<br />
这就是一个可运行的web项目。输入: grails run-app<br />
在浏览器输入:http://localhost:8080/demo<br />
6、cd 到demo目录，在命令行输入：<span style="color: #000099;">grails create-domain-class User</span>，创建一个User实体类。<br />
&nbsp;&nbsp; cd到demo\grails-app\domain目录下，这里存放所有的实体类。修改User.groovy文件为：<br />
<pre>class   User {<br />
String name<br />
String password<br />
<br />
String toString() {<br />
" $name : $password "<br />
}<br />
<br />
static  constraints  =  {<br />
name(blank:  false )<br />
password(blank:  false , size:  6 .. 16 )<br />
}</pre>
7、在命令行输入：<span style="color: #000099;">grails generate-all User。</span><span style="color: #000099;">为User产生所有CRUD操作需要的代码（如控制器UserController.groovy）和页面（如list.gsp）<br />
8、输入：</span><span style="color: #000099;">grails create-controller Login生成控制器</span><br />
<span style="color: #000099;">9、</span><span style="color: #000099;">修改demo\grails-app\controllers\LoginController.groovy的内容为：<br />
</span>
<pre>class LoginController {<br />
def loginService<br />
<br />
def index = { }<br />
<br />
def login = {<br />
User u = new User()<br />
u.properties = params<br />
if (params.name &amp;&amp; params.password) {<br />
<br />
def user = loginService.check(u)<br />
<br />
if (user) {<br />
flash.message = "Welcome ${user.name}"<br />
render(view: "ok", model: [user: user])<br />
} else {<br />
flash.error = "Invalid ${u.name} with ${u.password}"<br />
render(view: "login", model: [user: u])<br />
}<br />
} else {<br />
render(view: "login", model: [user: u])<br />
}<br />
}<br />
}</pre>
10、输入：<span style="color: #000099;">grails create-service Login。创建业务逻辑。</span><br />
<span style="color: #000099;">11、</span><span style="color: #000099;">修改demo\grails-app\services\LoginService.groovy的内容为：<br />
</span>
<pre>class LoginService {<br />
<br />
boolean transactional = true<br />
<br />
def check(User u) {<br />
def user = User.findWhere(name: u.name, password: u.password)<br />
return user<br />
}<br />
<br />
def serviceMethod() {<br />
// TODO<br />
}<br />
}<br />
12、<span style="color: #000099;">修改demo\grails-app\conf\ApplicationBootStrap.groovy，将一个User实例保存到数据库(grails自带hsqldb和jetty)中，内容如下：</span><br />
class ApplicationBootStrap {<br />
<br />
def init = { servletContext -&gt;<br />
new User(name: "demo", password: "123456").save()<br />
}<br />
def destroy = {<br />
}<br />
}<br />
13、<span style="color: #000099;">在&#8220;命令行&#8221;中，输入&#8220;grails run-app&#8221;，运行我们的Web应用</span><br />
<br />
</pre>
14、<span style="color: #000099;">打开浏览器，输入：</span><a href="http://localhost:8080/demo/login/login"><span style="color: #000099;">http://localhost:8080/demo/login/login</span></a><span style="color: #000099;"> ，在Name处输入demo，Password处输入123456，点击&#8216;Login&#8217;，</span><br />
<span style="color: #000099;"><br />
</span>
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/298033.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2009-10-13 10:40 <a href="http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298033.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>搭建Groovy开发环境</title><link>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298024.html</link><dc:creator>ThinkingTalking</dc:creator><author>ThinkingTalking</author><pubDate>Tue, 13 Oct 2009 01:58:00 GMT</pubDate><guid>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298024.html</guid><wfw:comment>http://www.blogjava.net/ThinkingTalking/comments/298024.html</wfw:comment><comments>http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298024.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ThinkingTalking/comments/commentRss/298024.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ThinkingTalking/services/trackbacks/298024.html</trackback:ping><description><![CDATA[1、安装JDK。不知道怎么安装的可百度一下。<br />
2、安装GDK。一共三步：<br />
（1）下载：http://dist.groovy.codehaus.org/distributions/groovy-binary-1.6.5.zip<br />
（2）配置GROOVY_HOME，和配置JAVA_HOME一样<br />
（3）配置Path。将GROOVY_HOME下的bin添加到path中。<br />
3、Hello World!<br />
在windows命令行环境下输入：groovyConsole ，回车。<br />
在文本区域输入：<span style="color: #000000;">println </span><span style="color: #000000;">&#8216;</span><span style="color: #000000;">Hello World!</span><span style="color: #000000;">&#8216;</span><span style="color: #000000;"> 然后回车。<br />
按ctrl+R组合键运行。<br />
</span><br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/ThinkingTalking/aggbug/298024.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ThinkingTalking/" target="_blank">ThinkingTalking</a> 2009-10-13 09:58 <a href="http://www.blogjava.net/ThinkingTalking/archive/2009/10/13/298024.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>