grails

GSP

Parameters#

Variables and scopes Details
application ServletContext instance
applicationContext Spring ApplicationContext instance
flash The flash object
grailsApplication GrailsApplication instance
out response writer for writing to the output stream
params params object for retrieving request parameters
request HttpServletRequest instance
response HttpServletResponse instance
session HttpSession instance
webRequest GrailsWebRequest instance

Basics

GSP supports the usage of <% %> scriptlet blocks to embed Groovy code (this is discouraged):

<html>
   <body>
     <% out << "Hello GSP!" %>
   </body>
</html>

You can also use the <%= %> syntax to output values, like in JSP:

<html>
   <body>
     <%="Hello GSP!" %>
   </body>
</html>

GSP also supports JSP-style server-side comments too:

<html>
   <body>
     <%-- This is my comment --%>
     <%="Hello GSP!" %>
   </body>
</html>

Expressions

In GSP the <%= %> syntax is rarely used due to the support for GSP expressions.

A GSP expression is similar to a JSP EL expression or a Groovy GString and takes the form ${expr}:

<html>
  <body>
    Hello ${params.name}
  </body>
</html>

However, unlike JSP EL you can have any Groovy expression within the ${..} block.

Any Groovy expression can be interpolated in all string literals, apart from single and triple single quoted strings. Interpolation is the act of replacing a placeholder in the string with its value upon evaluation of the string. The placeholder expressions are surrounded by ${} or prefixed with $ for dotted expressions. The expression value inside the placeholder is evaluated to its string representation when the GString is passed to a method taking a String as argument by calling toString() on that expression.

GSP Tags

There are variety of gsp tags available which can be used to create forms, textfield, radio buttons, check boxes, if-else, for each etc.

<g:if>

<g:if test="${session.role == 'admin'}">
   <%-- show administrative functions --%>
</g:if>
<g:else>
   <%-- show basic functions --%>
</g:else>

<g:each>

<g:each in="${[1,2,3]}" var="num">
  <p>Number ${num}</p>
</g:each>

form

<g:form name="myForm" url="[controller:'book',action:'list']">...</g:form>

textField

<g:textField name="myField" value="${myValue}" />

radio

<g:radio name="myGroup" value="1"/>

Follow this link for more info - https://docs.grails.org/latest/guide/theWebLayer.html#tags


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow