(前回の続き)GrailsのWicketプラグイン、その4。
「さて、これからどうしよう?」ということで、ドメインクラスとの連携をやってみます。
まず、ドメインクラスの作成。
nameだけもつクラスに。
# grails create-domain-class User
アプリケーションの起動時に、Userのインスタンスを複数作成。
class User {
String name
}
HelloWorld.groovyで、Userのインスタンスを1つ取得し、nameプロパティの値をHTMLページにinjection。
# vi grails-app/conf/ApplicationBootStrap.groovy
class ApplicationBootStrap {def init = { servletContext ->
new User( name : 'foo' ).save( )
new User( name : 'bar' ).save( )
new User( name : 'baz' ).save( )
}
def destroy = {
}
}
# vi grails-app/controllers/HelloWorld.groovy
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;public class HelloWorld extends WebPage
{
public HelloWorld()
{
User user = User.get(1)
add(new Label("message", user.name + ", Hello World!"))
}
}
で、run-appして「http://localhost:8080/wickethello/app」にアクセス。見事、
と表示されました(^_^)V
foo, Hello World!