Thursday, May 17, 2007

Grails + Wicket: The Wonders of the Grails Plug-in system

Update: A shorter Wicket plug-in installation guide has been added here

A few people have been asking me recently to integrate the Wicket project as a view technology for Grails. What is Wicket? It is a component oriented framework kind of like JSF, but unlike JSF it uses convention-over-configuration and doesn't require you to edit reams of XML. So in this sense its aims are more inline with Grails' aims.

So the question was how do we integrate these two frameworks? Well I thought it couldn't be that difficult and as it turns out, it isn't. So what I did was create a plug-in with "grails create-plugin wicket". The next thing I needed were some jars, so I got Wicket 1.2.6, Wicket Extensions 1.2.6 jar and the Wicket Spring integration jars for 1.2.6 and put them in the plug-ins lib directory.



Job done, now we need to set-up a convention that makes sense for both Grails and Wicket:

1) Since controllers are essentially roughly analogous to the Wicket Application object I decided that the convention would be to have a WebApplication.groovy file in the grails-app/controllers directory.
2) Wickets HTML components are like the views, so they can live in the grails-app/views directory

So to follow the HelloWorld example on the Wicket page we end up with something like this being the structure:



So just for clarify the WebApplication.groovy file looks like this:

import wicket.Page;
import wicket.spring.*;

public class WebApplication extends SpringWebApplication
{
public Class getHomePage()
{
return HelloWorld.class;
}
}

Whilst HelloWorld.groovy looks like this:

import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;

public class HelloWorld extends WebPage
{
public HelloWorld()
{
add(new Label("message", "Hello World!"))
}
}

Finally, the HelloWorld.html file looks like this:

<html>
<body>
<span wicket:id="message">Message goes here</span>
</body>
</html>

Ok so with that done, let's take advantage of the conventions we have in place here. When I created the plug-in with "grails create-plugin" it also created me a WicketGrailsPlugin.groovy file in the root of the plugin project. Since the normal controller mechanism no longer makes sense we modify the plug-in to "evict" the controllers plug-in when installed:


class WicketGrailsPlugin {
def version = 0.1
def dependsOn = [:]
def evicts = ['controllers'] // evict the controllers plugin
...
}


With that done we now need to configure the Wicket "applicationBean" in Spring. To do this we're going to use the GrailsApplication object and find a class that is an instance of a Wicket Application. This class just happens to be the one we defined earlier in grails-app/controllers:


import wicket.*
class WicketGrailsPlugin {
...
def doWithSpring = {
def applicationClass = application
.allClasses
.find { Application.class.isAssignableFrom(it) }

if(applicationClass) {
applicationBean(applicationClass) // defines the spring bean
}
}
}


We're of course using the Spring BeanBuilder here to define the bean definition if the Spring context. Ok job done.

Now we need to modify web.xml.. but wait. In Grails even web.xml is created on the fly so other plugins can participate in its generation. So we can do this in the plug-in file again:


import wicket.*
class WicketGrailsPlugin {
...
def doWithWebDescriptor = { xml ->
def servlets = xml.servlet[0]

servlets + {
servlet {
'servlet-name'('wicket')
'servlet-class'('wicket.protocol.http.WicketServlet')
'init-param' {
'param-name'('applicationFactoryClassName')
'param-value'('wicket.spring.SpringWebApplicationFactory')
}
'load-on-startup'(1)
}
}

def mappings = xml.'servlet-mapping'[0]
mappings + {
'servlet-mapping' {
'servlet-name'('wicket')
'url-pattern'('/app/*')
}
}
}
}


Here we use Groovy's XmlSlurper DSL to modify and the XML simply by using the + operator and some Groovy mark-up. We again use the Wicket Spring integration support to get it all working, so when Wicket loads it will actually look for the bean created by the Grails plug-in. And that's it, to test the plug-in we can type "grails run-app" and navigate to http://localhost:8080/grails-wicket/app and we see:



Not hugely impressive I know, but what it does show is Wicket running as the view tech in Grails which means that Wicket components and applications can use GORM to simplify the ORM layer of Wicket applications and other features of Grails like Services and Jobs. This took me all of 20 minutes to write, in fact I've spent longer writing this article than the plug-in. Not that it is complete of course, things left to do are to add reloading support to the Groovy files that Wicket users. Advice from the Wicket community on how to do that would be appreciated. And of course I haven't tested it against any more complex examples yet.

Now of course it wouldn't be a plug-in if it couldn't be installed into other apps. To do this we package it up with "grails package-plugin" which will create a zip. I've placed in plug-in at http://dist.codehaus.org/grails-plugins/ so in any Grails application you can now do:

grails install-plugin Wicket 0.1

or directly from the URL:

grails install-plugin http://dist.codehaus.org/grails-plugins/grails-Wicket-0.1.zip

And it will install this version of the plug-in.

Note: The plug-in doesn't work with the Grails URL mapping mechanism in 0.5 so you need to delete any grails-app/conf/*UrlMappings.groovy files otherwise you'll get an exception.

Plug-in sources can be found here: http://svn.grails-plugins.codehaus.org/browse/grails-plugins/grails-wicket/trunk

33 comments:

Erik van Oosten said...

Interesting!

Do I understand that the main advantage of using this, is access to GORM from Wicket? That would be cool because I really like Wicket, but admittedly with envy to dynamic stuff like ActiveRecord. Anyway, if this is true, aren't there more direct ways of using GORM in Wicket? I am unfamiliar with using Groovy so I'am probably missing something obvious.

Erik van Oosten said...

I found answers in http://www.nabble.com/Gricket%3A-The-Love-Child-of-Grails-and-Wicket-tf3772804.html

Thanks Grame.

akochnev said...

Wow, this is beautiful! I assume that doing something similar to integrate Tapestry as a view plugin for Grails would be very similar... That's my personal "Holy Grail" - be able to use GORM from a tapestry based front end.. Wow !!!

Graeme Rocher said...

Yes it would be similar. There is also someone doing a JSF integration that will be similar to the above I imagine

akochnev said...

Graeme, not sure if you would care about this, but I started on the Tapestry-Grails plugin, the first (of a series) of blog posts on the subject are at Grails + Tapestry = Grapestry ? Part 1 (of n).

Antony Stubbs said...

Hi Graeme,
Had a look at the wicket plugin, which I'm very excited about the possibilities of, but had a problem.

Is there a bug tracker which is appropriate for the plugin? Or should I simply post the the grails-plugin module on Jira?

The issue (which I also posted on the mailing list here

wicket-grails plugin doesn't look for *.html in grails-app/views

The 0.3 wicket-grails doesn't seem to look for the wicket html files in the views directory. I get the exception wicket.markup.MarkupNotFoundException: Markup not found.

If I put the HelloWorld.html in either the project root, or web-app/WEB-INF/classes/ it finds it there ok.

I'll try and download the plugin from SVN and see if that's any different.

Rajmahendra said...

Waav! Really nice article. keep writing like this :) Really helps me a lot.!

Raj

SimonLei said...

hi, Antony Stubbs,

I encountered the same problem as you. And I find a dirty but quick way to
solve the problem. I modified the
$GRAILS_HOME/scripts/Package.groovy,
add lines:

fileset(dir:"${basedir}/grails-app/views") {
include(name:"**/**")
exclude(name:"**/*.groovy")
}

at line 146, just below the lines copy files from src/java.

Antony Stubbs said...

@SimonLei:
Ah yes I can see why html files from the view directory would be excluded! I wonder what GR did when he tried it. However, I can imagine people would want to include plain ol' html files into their sites.

I'm not familiar with Grails much, but I'm confident there would be a way for the plugin to hook into the build process and get it's groovy files copied from the views directory.

I shall open an issue for this - so we can track it. One step closer to Wicket integration :)
Issue: http://jira.codehaus.org/browse/GRAILS-2182

I wonder if Wicket DataBinder could still be used along with this, or even Wicket Web Beans. GORM definitely makes searching / listing for beans easy though.

SimonLei said...

Wicket use many inner anonymous class, but groovy doesn't support it. I have to write many unnecessary classes that could be writen as inner anonymous in java class. So I need a smarter way to write wicket pages in groovy way.

I found WicketBuilder at http://wicketstuff.org/confluence/display/STUFFWIKI/wicket-groovy .But it doesn't support wicket 1.3 yet. :( Any suggestion?

docsharp01 said...

Great blog with lots of useful information and excellent commentary! Thanks for sharing. DSL internet service providers

aiyipianni said...

Aston Villa rode their luck at Hull City where an 88-minute own goal from Kamil Zayatte saw them leapfrog three points clear of Arsenal and into fourth place in the Premier League wow gold with a 1-0 win.

Villa had to survive Hull penalty wotlk gold appeals for a handball against Ashley Young in time added on, television replays showing that referee Steve Bennett wow gold correctly rejected the claims after consulting a linesman.

Bennett had been involved in controversy after just five minutes when American goalkeeper Brad Friedel looked to have handed Hull the initiative and threaten Villa's return to the Champions League qualifying wow gold zone.

Friedel spilled the ball under pressure from Nick Barmby and stand-in right-back Nigel Reo-Coker turned it into his own net as he attempted to wow gold clear.

But Bennett cut short celebrations at the KC Stadium -- and let Friedel off the hook -- when he ruled out the score for an wow wotlkapparent infringement by Barmby.

Zayatte's intervention from a Young cross bound for wow gold Gabriel Agbonlahor then saw Villa leapfrog Arsenal and draw level with Manchester United on 38 points -- seven adrift of leaders Liverpool and four wow gold behind Chelsea.

Stung by an on-pitch dressing down wow gold by manager Phil Brown at Manchester City last week, Hull showed five changes and a vastly improved performance.

Promoted Hull were looking for only their second win in 11 games while wow gold Villa arrived unbeaten in seven and it looked to be heading for a goalless draw when the home side suffered a cruel late blow.

aiyipianni said...

South Africa inflicted the first home series defeat on Australia in almost 16 years as they wrapped up a nine-wicket win over the world's number one ranked world of warcraft gold Test nation in Melbourne on Tuesday.

Captain Graeme Smith wow power leveling hit a fluent 75 as his side successfully passed a world of warcraft gold modest victory target of 183 on the final day at the MCG to take an wow powerleveling unassailable 2-0 lead.

It was the South African's first-ever Test series triumph in Australia and dofus kamas victory in the third and final match in Sydney will see them leapfrog the home side at the top of the global rankings.

Hashim Amla (30 not out) scored the winning runs shortly after lunch as South Africa became the first team to overcome Australia at home since the West Indies in 1992-93.

South Africa were never under any pressure in their run chase and did not lose a wicket until just before lunch when the inspirational Smith Lord of the Rings Online Gold was trapped leg before wicket by Nathan LOTRO Gold Hauritz.

Smith had flyff penya dominated a 121-run opening stand flyff money with Neil McKenzie, hitting ffxi gil 10 boundaries.

McKenzie struggled to buy ffxi gil a half century and survived strong eq2 plat lbw shouts from Brett Lee, eq2 gold who was bowling despite an injured foot that will Lord of the Rings Online gold keep him out of the Sydney Test.

South Africa's LOTRO gold victory was set up by a brilliant maiden Test century fly for fun penya from JP Duminy, who shared a stunning flyff penya 180-run ninth wicket partnership with pace bowler Dale Final Fantasy XI gil Steyn.

It gave the tourists ffxi gil a priceless 65-run lead on first innings before man of eq2 plat the match Steyn worked his magic with the ball as Australia were eq2 gold bowled out on the fourth day for 247 in their second innings.

The pugnacious Smith was virtually runescape money lost for words in his victory speech.

"It has been such a special moment runescape gold for all of us, it has been an incredible team effort," he said.

"I have been smiling non-stop wow po since we hit the winning runs.

"To be 2-0 up after this game was something wow or we only dreamt of."

South Africa won the first Test in Perth from an unlikely position, chasing 414 for victory for the loss of only four wickets.

uiyui said...

welcome to the wow power leveling cheap wow power leveling service site, buy cheap wow gold,wotlk gold,world of warcraft power leveling buy wow gold

chengja said...

Alex Rodriguez,the playerwow goldwho would restore integrity to baseball's homewow goldrunre cord,admittedwow goldMonday towow goldusing performance-enhancingwow goldwow golddrugs himself.

chengja said...

The All-Star thirddofus kamasdofus kamasbaseman said in an interviewkamas dofusacheter dofuswith ESPN that he used steroids with the Texas Rangers for three years,from 2001-03,in an attemptbuy kamasacheter kamasto justify his status as the game's highest-paid player after signing a 10-year.

chengja said...

Back then it wasworld of warcraft golda different culture,Rodriguezcheap wow goldsaid.It waswow orvery loose.I was young.I waswow power levelingstupid.

chengja said...

He said he quitbuy wow goldcheap wow goldafter 2003,hiswow power levelingdofus kamasfirst of three AL MVP seasons,because I've provedbuy ffxi gilLord of the Rings Online goldto myself and to everyone that I don't need any of that.

chengja said...

When I arrivedwow goldin Texas in 2001,Iwow goldfelt an enormous amount of pressure.I feltwow goldwow goldlike I had all the weightwow goldof the world on top of me and I needed to perform.

xuemei said...

I like play online game, I also buy wow gold and wow gold, the World of Warcraft Gold is very cheap, and use the warcraft gold can buy many things, I like cheap wow gold, thanks, it is very good.

derfefww said...

It is the shadow of legend Gold which make me very happy these days, my brother says sol gold is his favorite games gold he likes, he usually buy some buy shadow of legend Gold to start his game and most of the time he will win the cheap shadow of legend Gold back and give me some shadow of legend money to play the game.

chenlu said...

runescape money
runescape gold
runescape money
runescape gold

sm said...

網頁設計,情趣用品店,情趣用品專賣網

A片,色情A片,免費A片,成人影片,色情影片,a片免費看,情色貼圖,情色文學,情色小說,色情小說
AV,AV女優

辣妹視訊,美女視訊,視訊交友網,視訊聊天室,視訊交友,視訊美女,免費視訊,免費視訊聊天,視訊交友90739,免費視訊聊天室,成人聊天室,視訊聊天,視訊交友aooyy
哈啦聊天室,辣妺視訊,A片,色情A片,視訊,080視訊聊天室,視訊美女34c,視訊情人高雄網,視訊交友高雄網,0204貼圖區,sex520免費影片,情色貼圖,視訊ukiss

buy wow gold said...

When Wow Gold wolf finally found the wow gold cheap hole in the chimney he crawled cheap wow gold down and KERSPLASH right into that kettle of water and that was cheapest wow gold the end of his troubles with the big bad wolf.
game4power.
The next day the Buy Wow Goldlittle pig invited hisbuy gold wow mother over . She said "You see it is just as mygamegoldI told you. The way to get along in the world is to do world of warcraft gold things as well as you can." Fortunately for that little pig, he buy cheap wow gold learned that lesson. And he just k4gold lived happily ever after!.

Anonymous said...

I was very like to play the Rohan online game, in my mind I can gave up all things but I can not gave up the rohan crone, it gave me the courage and the confidence, in the game I was very like to earn the rohan gold, but I also need to buy rohan crone, but the same time I know a website sell the cheap rohan crone, so if you want to buy, I suggest you come here.

肖小非 said...

When the Wow Gold wolf finally found the Buy Wow Goldhole in the chimney he crawled wow gold cheap down and KERSPLASH right into that kettle of water and that was cheapest wow gold the end of his aoc gold troubles with the big bad wolf.

The next day the cheap wow gold buy gold wow little pig invited his mother over . She said "You see it is just as I told you. The way to gdpchinaget along in the world is to do world of warcraft gold things as well as you can." Fortunately for that little pig, he mmopawn learned that lesson. And he just lived happily ever after!

Anonymous said...

^^ nice blog!! ^@^

徵信, 徵信網, 徵信社, 徵信社, 感情挽回, 婚姻挽回, 挽回婚姻, 挽回感情, 徵信, 徵信社, 徵信, 徵信, 捉姦, 徵信公司, 通姦, 通姦罪, 抓姦, 抓猴, 捉猴, 捉姦, 監聽, 調查跟蹤, 反跟蹤, 外遇問題, 徵信, 捉姦, 女人徵信, 女子徵信, 外遇問題, 女子徵信, 外遇, 徵信公司, 徵信網, 外遇蒐證, 抓姦, 抓猴, 捉猴, 調查跟蹤, 反跟蹤, 感情挽回, 挽回感情, 婚姻挽回, 挽回婚姻, 外遇沖開, 抓姦, 女子徵信, 外遇蒐證, 外遇, 通姦, 通姦罪, 贍養費, 徵信, 徵信社, 抓姦, 徵信, 徵信公司, 徵信社, 徵信公司, 徵信社, 徵信公司, 女人徵信,

徵信, 徵信網, 徵信社, 徵信網, 外遇, 徵信, 徵信社, 抓姦, 徵信, 女人徵信, 徵信社, 女人徵信社, 外遇, 抓姦, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信社, 女人徵信社, 徵信社, 徵信, 徵信社, 徵信, 女子徵信社, 女子徵信社, 女子徵信社, 女子徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社,

徵信, 徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 外遇, 抓姦, 離婚, 外遇,離婚,

徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信,徵信社, 徵信社, 徵信, 外遇, 抓姦, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社, 徵信社, 徵信社,徵信,徵信, 徵信, 外遇, 抓姦

song said...

wow gold
wow gold
eve isk
wow power leveling
wow power leveling
wow power leveling
wow power leveling
warhammer gold
warhammer power leveling
bestchina traveland
uggsor
ghd straighteners

Cardi Ugg Boots
Classic Short Ugg Boots

Classic Tall Ugg Boots
Metallic Ugg Boots

Mini Ugg Boots
Sundance Ugg Boots

tiffany Jewelry

belrion said...

buy wow gold from top 10 best website.

wow gold discount store,

cheap wow gold  bargain shop;

ffxi gil for you to experience the power.

world of warcraft gold last minute fire sale.

buy ffxi gil with 5% coupon for your character.

everquest platinum market and play to win your game.

warhammer gold at inexpensive price.

cheap ffxi gil to have, why farm the gil yourself.

cheap wow power leveling for your new toons.

cheapest wow gold available with us.

wow gold for sale to celebrate

Anonymous said...

Do you know the Atlantica online Gold, in the game you need the Atlantica Gold. it can help you increase your level. My friends always asked me how to buy Atlantica online Gold, i do not know he spend how much money to buy the Atlantica online money, when i see him in order to play the game and search which the place can buy the cheap Atlantica online Gold . i also happy with him.

I do not know why I was very like to play the EVE, so I need eve isk and the eve gold , do you know the eve online isk.

Anonymous said...

You smart and buy Sword of the New World Vis, you play the game is right, Sword of the New World Gold. you have a wonderful time, buy vis, I have it cheap snw vis, I buy Sword of the New World money.

I find job Tales Of Pirates gold, this is a nice work andTales Of Pirates money. I do not want to rely on my parents cheap Tales Of Pirates gold, They look after me so long time, they pay me too much and very tired buy Tales Of Pirates Gold.

cvxv said...

看房子,買房子,建商自售,自售,台北新成屋,台北豪宅,新成屋,豪宅,美髮儀器,美髮,儀器,髮型,EMBA,MBA,學位,EMBA,專業認證,認證課程,博士學位,DBA,PHD,在職進修,碩士學位,推廣教育,DBA,進修課程,碩士學位,網路廣告,關鍵字廣告,關鍵字,課程介紹,學分班,文憑,牛樟芝,段木,牛樟菇,日式料理, 台北居酒屋,日本料理,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,台北結婚,場地,住宿,訂房,HOTEL,飯店,造型系列,學位,SEO,婚宴,捷運,學區,美髮,儀器,髮型,看房子,買房子,建商自售,自售,房子,捷運,學區,台北新成屋,台北豪宅,新成屋,豪宅,學位,碩士學位,進修,在職進修, 課程,教育,學位,證照,mba,文憑,學分班,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,結婚,場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,居酒屋,燒烤,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,小套房,小套房,進修,在職進修,留學,證照,MBA,EMBA,留學,MBA,EMBA,留學,進修,在職進修,牛樟芝,段木,牛樟菇,關鍵字排名,網路行銷,PMP,在職專班,研究所在職專班,碩士在職專班,PMP,證照,在職專班,研究所在職專班,碩士在職專班,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,EMBA,MBA,PMP
,在職進修,專案管理,出國留學,EMBA,MBA,PMP
,在職進修,專案管理,出國留學,EMBA,MBA,PMP,在職進修,專案管理,出國留學,婚宴,婚宴,婚宴,婚宴,漢高資訊,漢高資訊,比利時,比利時聯合商學院,宜蘭民宿,台東民宿,澎湖民宿,墾丁民宿,花蓮民宿,SEO,找工作,汽車旅館,阿里山,日月潭,阿里山民宿,東森購物,momo購物台,pc home購物,購物

漢高資訊,漢高資訊,在職進修,漢高資訊,在職進修,民宿,民宿,整形,造型,室內設計,室內設計,漢高資訊,在職進修,漢高資訊,在職進修,民宿,美容,室內設計,在職進修,羅志祥,周杰倫,五月天,民宿,民宿,整形,整形,室內設計,室內設計,比利時聯合商學院,在職進修,比利時聯合商學院,在職進修,漢高資訊,找工作,找工作,找工作,找工作,找工作,蔡依林,林志玲

coco0610 said...

wholesale jewelrycostume jewelryhandmade jewelryfashion jewelrypearl jewelrycrystal jewelrysemiprecious jewelryturquoise jewelrycoral jewelryshell jewelryswarovski crystal