濡傛灉瑕佸垪涓捐繎浜涘勾鏉ava棰嗗煙涓渶浠や漢嬋鍔ㄧ殑浜嬩歡錛孏rails鐨勮癁鐢熺粷瀵瑰簲璇ユ涓婃湁鍚嶃傚湪鎵撳紑Grails瀹樻柟緗戠珯鏃訛紝 浣犱細鍦ㄦ祻瑙堝櫒鐨勬爣棰樻爮涓婄湅鍒拌繖鏍風殑鍐呭錛?#8220;Grails - 瀵繪壘灝辨緇撴潫”銆傝櫧鐒惰繖鍙ヨ瘽涓嶈寰楅傜敤浜庢墍鏈夊満鍚堬紝浣嗗浜庝笉灝戜粠浜婮ava Web寮鍙戠殑浜烘潵璇達紝璧炲悓鑰呮亹鎬曚笉鍦ㄥ皯鏁般傜敱浜庡叿澶嘡ails椋庢牸鐨勬鏋躲佸吋瀹圭粷澶у鏁癑ava璇硶鐨凣roovy鍔ㄦ佽璦銆佷互涓氱晫鍙g鑹ソ鐨勬鏋朵負鍩? 紜銆佸浼佷笟鐜版湁Java寮鍙戣祫浜х殑淇濇姢銆丳lugin鏋舵瀯絳夌壒鐐癸紝鍐嶅姞涓奡pring紺懼尯鐨勫己鍔涗粙鍏ワ紝Grails琛屾儏鐪嬫定搴旇鏄剰鏂欎箣涓殑銆?/p>
鐒惰孏rails鍗存病鏈夌湅涓婂幓閭d箞綆鍗曪紝鍦ㄥ叾鍖呰鐨勭硸琛d箣涓嬶紝鏈夌潃璁稿浣滀負Java Web寮鍙戣呬笉寰椾笉浜嗚В鐨勭煡璇嗐傛渶榪戯紝Peter Delahunty鍙戣〃浜嗛涓?a >Grails寮鍙戣呴渶瑕佺煡閬撶殑10涓妧宸?/a>鐨勫崥鏂囥備笉瑕佽鏍囬鎵榪鋒儜浜嗭紝榪欓噷鎵璇寸殑“鎶宸?#8221;鍙笉鏄偅浜涜璦灞傞潰鐨?#8220;鎶宸?#8221;錛屾妸瀹冧滑縐頒負“寤鴻”鎭愭曟洿鍚堥備竴浜涖?br />
鎶宸?錛氭妸Grails鐨勪功綾嶄拱鏉ヨ璇匯傚姝わ紝Peter Delahunty鎺ㄨ崘浜嗗凡鍑虹増鐨勩奃efinitive Guide to Grails
2nd edition銆嬪拰銆奊roovy and Grails Recipes銆嬶紝浠ュ強鍗沖皢鍑虹増鐨勩奊rails in Action銆嬨?/p>
鎶宸?錛氬涔燝roovy錛屽茍鎸夌収Groovy榪涜鎬濊冦侾eter Delahunty閫氳繃涓涓緥瀛愯鏄庝簡Java鎬濈淮鍜孏roovy鎬濈淮鐨勪笉鍚屻?/p>
榪欏効鏈変釜濂戒緥瀛愶細
浼犳挱鎿嶄綔絎︼紙spread operator錛夛細*
鍦↗ava涓紝瑕佹兂閬嶅巻涓緇勪漢騫舵敹闆嗕漢鍚嶃傛垜鍙兘寰楄繖鏍峰啓錛?/p>
List names = new ArrayList();
for (Iterator iterator = people.iterator(); iterator.hasNext();) {
Person person = (Person) iterator.next();
names.add(person.getName());
}
System.out.println(names);
鐢℅roovy錛屾垜浼氳繖涔堝啓錛?/p>
def names = people*.name
println names
鍛靛懙錛岀畝鍗曞惂銆傛垜榪樺彲浠ヨ繖鏍峰啓錛?/p>
def names = people.collect {it.name}
println names
鍦ㄨ繖涓緩璁腑錛屼粬鍒椾婦浜嗕竴浜涜祫婧愶細
浠ュ強涓浜涗功綾嶏細
鎶宸?錛氫簡瑙rails鐨勫悇涓彃浠躲侾eter Delahunty緇欏嚭鐨勭悊鐢憋細Grails鏈韓鏄釜鎻掍歡緋葷粺錛岃屼笖鐜板湪宸茬粡鏈夊ぇ閲忕殑鎻掍歡鍙緵涓嬭澆銆傚湪鑷瀹炵幇涔嬪墠錛屼笉濡ㄧ湅鐪嬪叾涓槸鍚︽湁縐版墜鐨勩?/p>
鎶宸?錛氬涔燞ibernate鍜孲pring銆傝繖涓鐐逛笉闅劇悊瑙o紝鏃㈢劧Grails鏄緩绔嬪湪榪欎簺宸ュ叿涔嬩笂鐨勶紝浜嗚В榪欎簺宸ュ叿鏈韓鐨勪嬌鐢紝瀵逛簬鐔熺粌浣跨敤Grails鑷劧澶ф湁鑴劇泭銆?/p>
鎶宸?錛氫嬌鍗曞厓嫻嬭瘯銆侀泦鎴愭祴璇曞拰涓鑸祴璇曠殑鑷姩鍖栥侾eter Delahunty鍦ㄨ繖閲屾帹鑽愪簡涓や釜Grails鐨勬彃浠訛細Testing Plugin鍜?a >Functional testing plugin銆傚墠鑰呭皢鎴愪負Grails 1.1鐨勫唴緗彃浠躲?/p>
鎶宸?錛氫嬌鐢ㄥソ鐨処DE銆侾eter Delahunty琛ㄧず錛屼粬鑷繁浣跨敤鐨勬槸Intellij錛屼絾瀵逛簬Netbeans涔熷瘎浜堝帤鏈涖?/p>
鎶宸?錛氬涔犲茍浣跨敤HTML W3C鏍囧噯銆?#8220;閬靛驚鏍囧噯灝嗘秷闄や綘90%鐨勯棶棰?#8221;錛孭eter Delahunty濡傛槸璇淬?/p>
鎶宸?錛氱湅涓婂幓涓嶉敊涓瀹氫笉閿欍傝繖鍏跺疄璇寸殑鏄?#8220;闈㈠瓙宸ョ▼”錛岄氳繃鎻愰珮搴旂敤鐨勫瑙傝璁℃潵鎻愰珮搴旂敤鐨勭珵浜夊姏銆侾eter Delahunty緇欏嚭浜?涓祫婧愶細
鎶宸?錛氫嬌鐢‵irefox涓婄殑Firebug銆傚浜庢祻瑙堝櫒绔殑璋冭瘯宸ヤ綔錛孎irebug緇濆鏄竴涓繀澶囧伐鍏楓?/p>
鎶宸?0錛氳窡涓奊roovy鍜孏rails紺懼尯鐨勬浼愩傚湪榪欐渶鍚庝竴涓緩璁腑錛孭eter Delahunty緇欏嚭浜嗚幏鍙朑roovy鍜孏rails淇℃伅鐨勪富瑕佽祫婧愰摼鎺ワ細
璇峰姟蹇呴槄璇籔eter Delahunty鐨?a >鍘熸枃銆傚浜嶨rails鐨勬柊鑰佷嬌鐢ㄨ咃紝浣犻兘鍙互浠嶱eter Delahunty鐨勫緩璁腑鏈夋墍鏀惰幏銆傚叧浜嶪nfoQ涓枃绔橤roovy鍜孏rails鐨勫叾瀹冨唴瀹癸紝璇瘋闂?a >榪欓噷鍜?a >榪欓噷銆?/p>
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.
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.
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.
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 -> Preferences… -> Java -> Build path -> Classpath Variables -> New.
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.
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 -> source folder. Otherwise you may get errors in the import statements.
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: http://nuin.blogspot.com/2005/05/launch-jetty-from-eclipse-solving.html
If you are using the Eclipse Groovy plugin then make sure you enable the preference 'Disable Groovy Compiler Generating Class Files' (newer versions: Project -> Preferences -> Groovy Project Preferences / older versions: Windows -> Preferences -> 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.THIS DOES NOT APPLY IF YOU INTEND TO USE DEBUGGING (read the section 'Step debugging' below)
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 auto-reloaded.
If you are getting exceptions like
org.mortbay.util.MultiException[java.io.FileNotFoundException: {yourpath}/web-app]
at org.mortbay.http.HttpServer.doStart(HttpServer.java:731)
at org.mortbay.util.Container.start(Container.java:72)
at grails.util.GrailsMain.main(GrailsMain.java:67)
grails dev package
For some other errors or quirky behavior, it is often helpful to run the command
grails clean
You can step debug a Grails application if you install the latest build of the Groovy Eclipse plug-in.
Eclipse debugging depends on the class files generated by the Groovy compiler, so make sure 'Disable Groovy Compiler Generating Class Files' is not checked (Project -> Properties -> 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".To debug an application, do the following:
You can configure grails as an "External Tool", allowing you to create domain classes etc. from within Eclipse:
The gsp files are just jsp files with a special taglib. The extension gsp has to be added as follows:
<%@ taglib prefix="g" uri="/web-app/WEB-INF/tld/grails.tld" %>
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 here
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".
An internal error occurred during: "Building workspace".
BUG! exception in phase 'conversion' in source unit '...' Error:
duplicate class declaration for name: … and class:
org.codehaus.groovy.ast.ClassNode@...[name:...]
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.
class User {7銆佸湪鍛戒護琛岃緭鍏ワ細grails generate-all User銆?/span>涓篣ser浜х敓鎵鏈塁RUD鎿嶄綔闇瑕佺殑浠g爜錛堝鎺у埗鍣║serController.groovy錛夊拰欏甸潰錛堝list.gsp錛?br /> 8銆佽緭鍏ワ細grails create-controller Login鐢熸垚鎺у埗鍣?/span>
String name
String password
String toString() {
" $name : $password "
}
static constraints = {
name(blank: false )
password(blank: false , size: 6 .. 16 )
}
class LoginController {10銆佽緭鍏ワ細grails create-service Login銆傚垱寤轟笟鍔¢昏緫銆?/span>
def loginService
def index = { }
def login = {
User u = new User()
u.properties = params
if (params.name && params.password) {
def user = loginService.check(u)
if (user) {
flash.message = "Welcome ${user.name}"
render(view: "ok", model: [user: user])
} else {
flash.error = "Invalid ${u.name} with ${u.password}"
render(view: "login", model: [user: u])
}
} else {
render(view: "login", model: [user: u])
}
}
}
class LoginService {
boolean transactional = true
def check(User u) {
def user = User.findWhere(name: u.name, password: u.password)
return user
}
def serviceMethod() {
// TODO
}
}
12銆?span style="color: #000099;">淇敼demo\grails-app\conf\ApplicationBootStrap.groovy錛屽皢涓涓猆ser瀹炰緥淇濆瓨鍒版暟鎹簱(grails鑷甫hsqldb鍜宩etty)涓紝鍐呭濡備笅錛?/span>
class ApplicationBootStrap {
def init = { servletContext ->
new User(name: "demo", password: "123456").save()
}
def destroy = {
}
}
13銆?span style="color: #000099;">鍦?#8220;鍛戒護琛?#8221;涓紝杈撳叆“grails run-app”錛岃繍琛屾垜浠殑Web搴旂敤