{"id":227,"date":"2009-08-28T14:49:09","date_gmt":"2009-08-28T09:19:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=227"},"modified":"2016-12-19T15:28:13","modified_gmt":"2016-12-19T09:58:13","slug":"grails-reverse-url-mapping-to-generate-urls-ending-with-html","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-reverse-url-mapping-to-generate-urls-ending-with-html\/","title":{"rendered":"Grails: reverse url mapping to generate urls ending with &#8220;.html&#8221;"},"content":{"rendered":"<p>Hi Friends,<\/p>\n<p>Recently I came across a problem, where the url of\u00a0the links were to be generated dynamically based on the controller and action to be executed and\u00a0 should end with &#8220;.html&#8221;. Reverse url mappings came to my rescue, which can be implemented using &lt;g:link&gt; GSP tag.<br \/>\nThen I tried this, <\/p>\n<p>In UrlMappings.groovy file :<\/p>\n<blockquote>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"groovy\">class UrlMappings {\r\n    static mappings = {\r\n      ...\r\n      \"\/contact(controller:'myController', action:'myAction')\r\n    }<\/pre>\n<\/div>\n<\/div>\n<\/blockquote>\n<p>And in my gsp file :<\/p>\n<blockquote>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"groovy\">&lt;g:link controller=\"myController\" action=\"myAction\"&gt; myLink&lt;\/g:link&gt;<\/pre>\n<\/div>\n<\/div>\n<\/blockquote>\n<p>The above code gave me this url &#8220;http:\/\/serverURL.com\/contact&#8221;. I wanted not only &#8220;contact&#8221; at the end but also &#8220;.html&#8221; text to be appended with it. So for that I added one more entry to my UrlMappings.groovy file. Now it looks like this :<\/p>\n<blockquote>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre class=\"groovy\">class UrlMappings {\r\n    static mappings = {\r\n      ...\r\n      \"\/contact.html(controller:'myController', action:'myAction')\r\n      \"\/contact(controller:'myController', action:'myAction')\r\n    }\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/blockquote>\n<p>In the above code, both lines are required to make the url ending with &#8220;.html&#8221;. Only &#8220;\/contact.html&#8221; mapping won&#8217;t work. Since for the url &#8220;http:\/\/serverURL.com\/contact.html&#8221;, UrlMappings file is searched only for contact and &#8220;.html&#8221; gets truncated. Therefore we need to have &#8220;\/contact.html&#8221; mapping also.<br \/>\nDon&#8217;t forget, the mappings specified above has to be in the same order.<\/p>\n<p>Cheers!!<br \/>\n~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com\/blog\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog Reverse Url Mappings has been used to generate the URL ending with .html, given controller and action names.<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[4840,121,50],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/227"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}