Japanese Views and Layouts

Last updated by admin 3 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() ]
      }
}
{excerpt:hidden=true} The corresponding view for the above action would need to be placed in the "grails-app/views/book" directory with the name "list.jsp" or "list.gsp". The example below is in GSP: {excerpt} ??????????????????"grails-app/views/book"????????"list.jsp"???"list.gsp"??????????????????????????GSP???????:
<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>
{excerpt:hidden=true} Now create a layout in called "main.gsp" in the "grails-app/views/layouts" directory and you're done!: {excerpt} ?"grails-app/views/layouts"????????"main.gsp"??????????????????!:
<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} The layout uses GSP tags to apply the layout onto the target page. {excerpt} ????????????????????????????????? GSP tags ???????

?????????? {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 = {  … }
}
{excerpt:hidden=true} You can create a layout called {{grails-app/views/layouts/book.gsp}} which will be applied to all views that the {{BookController}} delegates to. {excerpt} {{BookController}} ?????????????????? {{grails-app/views/layouts/book.gsp}} ?????????????????????{excerpt:hidden=true} Alternatively you can create a layout called {{grails-app/views/layouts/book/list.gsp}} which will only be applied to the {{list}} action within the {{BookController}}. {excerpt} ????? {{BookController}} ?? {{list}} ???????????????? {{grails-app/views/layouts/book/list.gsp}} ?????????????????????{excerpt:hidden=true} If you have both the above mentioned layouts in place the layout specific to the action will take precedence when the {{list}} action is executed. {excerpt} ??2??????????????? {{list}} ?????????????????????????????????????????