{"id":20492,"date":"2015-06-03T15:43:57","date_gmt":"2015-06-03T10:13:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20492"},"modified":"2015-06-26T10:34:22","modified_gmt":"2015-06-26T05:04:22","slug":"traits-provided-by-grails-3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/traits-provided-by-grails-3\/","title":{"rendered":"Traits Provided by Grails 3"},"content":{"rendered":"<p>The Core API&#8217;s in Grails 3 <a href=\"http:\/\/www.tothenew.com\/grails-application-development\">are based on Groovy<\/a> Traits. Following are the few of the examples which shows the way you can use these traits in Groovy classes.<\/p>\n<p><strong>ResponseRenderer<\/strong><\/p>\n<p>With the help of ResponseRenderer traits we can provide render method to a Groovy class in the following way :<\/p>\n<p>[java]<br \/>\nimport grails.artefact.controller.support.ResponseRenderer<br \/>\nclass ResponseRendererComponent implements ResponseRenderer {<br \/>\n    ResponseRendererComponent() {<br \/>\n        render &quot;rendering from the constructor&quot;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now when we try to instantiate ResponseRendererComponent, the render() inside the the constructor (which is provided by ReponseRenderer trait) will render the response.<\/p>\n<p><strong>WebAttributes<\/strong><\/p>\n<p>[java]<br \/>\nimport grails.artefact.controller.support.ResponseRenderer<br \/>\nimport grails.web.api.WebAttributes<br \/>\nclass WebAttributesComponent implements WebAttributes, ResponseRenderer {<br \/>\n    WebAttributesComponent() {<br \/>\n        render &quot;base url: ${webRequest.baseUrl}&quot;<br \/>\n        render &quot;&lt;br&gt;&quot;<br \/>\n        render &quot;params: ${params}&quot;<br \/>\n        render &quot;&lt;br&gt;&quot;<br \/>\n        render grailsApplication.config.getProperty(&quot;myVar&quot;)<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>In the above example we are accessing params, webRequest and properties which are defined in application.yml<\/p>\n<p><strong>ServletAttributes<\/strong><\/p>\n<p>With the help of SevletAttributes trait we can access the sessions inside any Groovy class.<\/p>\n<p>[java]<br \/>\nimport grails.artefact.controller.support.ResponseRenderer<br \/>\nimport grails.web.api.ServletAttributes<br \/>\nclass ServletAttributeComponent implements ServletAttributes, ResponseRenderer {<br \/>\n    ServletAttributeComponent() {<br \/>\n        render session.mySession<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>We can also redirect and forward to an action of the controller from any Groovy class using ResponseRedirector and RequestForwarder traits.<\/p>\n<p><strong>ResponseRedirector<\/strong><\/p>\n<p>[java]<br \/>\nimport grails.artefact.controller.support.ResponseRedirector<br \/>\nclass ResponseRedirectorComponent implements ResponseRedirector {<br \/>\n    ResponseRedirectorComponent() {<br \/>\n        redirect(controller: &quot;responseRedirector&quot;, action: &quot;redirected&quot;)<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><strong>RequestForwarder<\/strong><\/p>\n<p>[java]<br \/>\nimport grails.artefact.controller.support.RequestForwarder<br \/>\nclass RequestForwarderComponent implements RequestForwarder {<br \/>\n    RequestForwarderComponent() {<br \/>\n        forward(controller:&quot;requestForwarder&quot;,action:&quot;forwarded&quot;)<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Core API&#8217;s in Grails 3 are based on Groovy Traits. Following are the few of the examples which shows the way you can use these traits in Groovy classes. ResponseRenderer With the help of ResponseRenderer traits we can provide render method to a Groovy class in the following way : [java] import grails.artefact.controller.support.ResponseRenderer class [&hellip;]<\/p>\n","protected":false},"author":102,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20492"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20492"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20492\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}