user5795168
user5795168

Reputation:

using <set> tag in grails

I am new to grails. I came across a set tag which we can use in gsp pages itself to set the values which is similar to setting model from the controller.

<g:set var="home" value="something" />

so that when we write ${home} it outputs "something".

Is there any way to set the value in sessions in gsp pages itself and not from controller using set tag?

Upvotes: 6

Views: 1729

Answers (2)

Mcoder
Mcoder

Reputation: 309

Yes you can do it in gsp pages as well. You just have to include an extra attribute scope to indicate which scopes(session, flash, page and request) you are setting the value to.

<g:set var="home" value="something" scope="session" />

If you do not include the scope option then it defaults to page.

To display the the value you just have to write ${session.home} or ${request.home} or simply ${home} for request scope. Hope this helps.

For more : https://grails.github.io/grails-doc/3.0.x/ref/Tags/set.html

Upvotes: 6

Vinay Prajapati
Vinay Prajapati

Reputation: 7556

Well! above answer suffices the need. Just wanted to add one more thing that gsp pages comprises of jsp internally and hence all the 9 implict objects are available on gsp pages as well.

request     HttpServletRequest object
response    HttpServletResponse object
out         PrintWriter object used to send output to the client.
session     HttpSession object
application ServletContext object associated with application context.
config      ServletConfig object associated with the page.
pageContext server-specific features JspWriters.
page        synonym for this
Exception   handling exceptions and error page redirects.An instance of javax.servlet.jsp.JspException

You could at any point of time access these in your gsp pages.

More you can read from this.

Hope it helps!

Upvotes: 0

Related Questions