Last updated by admin 4 years ago
????????? {excerpt:hidden=true}Views and layouts {excerpt}
??????????? {excerpt:hidden=true}Controllers & Views {excerpt}
{excerpt:hidden=true} Grails supports the creation of views using either JavaServer Pages (JSP) or Groovy Server Pages (GSP). This section describes how to provide a model to views. For a more complete reference on see the GSP and JSP references.Syntactically they are very similar the main difference being that code within scriptlets is in Groovy not Java! Grails controllersuses a convention mechanism to delegate to an appropriate view. For example an action called list will delegate to a "list" view: {excerpt} Grails??Java Server Pages (JSP) ????Groovy Server Pages (GSP) ????????????????????????????????????????????????????????????????GSP ? JSP??????????????????????????????????GSP???????????????Java????Groovy?????????! Grails ??????? ???????????????????????????????????????list????????????"list"?????????????:class BookController {
def list = {
["books" : Book.list() ]
}
}<html> <head> <title>Book list</title> </head> <body> <h1>Book list</h1> <table> <tr> <th>Title</th> <th>Author</th> </tr> <g:each in="${books}"> <tr> <td>${it.title}</td> <td>${it.author}</td> </tr> </g:each> </table> </body> </html>
???????? {excerpt:hidden=true}Defining Layouts {excerpt}
{excerpt:hidden=true} Layouts can be created through Grails's support for SiteMesh. There are 2 ways to create layouts the first is to associate a view with a layout at the "layout" meta tag to your page:{excerpt} ???????SiteMesh?????Grails???????????????????????????????2????????????1?????????"layout"????????????????????????????:<html>
<head>
<meta name="layout" content="main"></meta>
</head>
<body>This is my content!</body>
</html><html>
<head>
<title><g:layoutTitle default="An example decorator" /></title>
<g:layoutHead />
</head>
<body onload="${pageProperty(name:'body.onload')}">
<div class="menu"><!--my common menu goes here--></menu>
<div class="body">
<g:layoutBody />
</div>
</div>
</body>
</html>?????????? {excerpt:hidden=true}Layout by Convention {excerpt}
{excerpt:hidden=true} The second way to associated layouts is to use "layout by convention". For example if you have a controller such as the below: {excerpt} ?2???????????????????"??????????"???????????????????????????????????????:class BookController {
def list = { … }
}


