This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Custom tag library for generating DRY urls

Dec 28th, 2009, 04:08 AM

Hello,

I looked at the output of roo and notices that urls (or parts thereof) is being duplicated in the views and controllers. What I did was create a few final strings on the controller, so that the controller effectively looks as follows:

So if I manually manage the controller, I can change the request mapping in one place. It still doesn't help much, since I need to manually update the views. I tried to generate additional tags using the tagx scheme, however I am not sure how to reference the final strings in the controllers. Is this possible?

The other thought I had was to simply generate a custom tag library and use them in the views. The tags can then simply reference the constants in the controller. The tags I am think of creating tags for an entity like "Post" are:

posts_url - the url to view a list of posts
new_post_url - the url to display a new post form
post_url - takes an instance of post and generates a url to view that post
update_post_url - takes an instance of post and generates a url to modify that post

which generates effectively the same code. It means a designer can use this tag throughout the application to generate a link to the page that displays the form to create a new post.

These tags can also take additional logic into account such that if the resulting url points to the current page, then the link will not be generated; just the inner body. Additionally, security may dictate that the link shouldn't be generated if the user doesn't have access to the resource and the tag library can take care of that. This can increase productivity when manually changing the design of the application.

What is the best way to approach this? I can't seem to find any decent resources on how to develop your own tag library with STS and Spring MVC. I can however write a generic tag library.

The ultimate goal may be for roo to generate the tag library as part of scaffolding.

Thanks for sharing your ideas here. In fact the second idea w/r to the menu.jspx is somewhat close to the changes I have been working on and off in the recent days. My current solution goes beyond what you are suggesting but the general idea remains the same. We will be releasing Roo 1.0 very soon and I will start committing my changes around mid Jan. I would be very interested in your feedback when we (hopefully) cut an early milestone release for Roo 1.1 in late January.

Your first suggestion to have the request mappings in one place does not seem to add too much value from my perspective since the URIs all unique to the different methods, so your code would just become a little more verbose. Since most modern IDEs offer code collapsing it would not be too hard to keep the URI definitions in the method level annotation. This way it is also always clear which URI this method handles. So I think the current code is not that bad with regards to the URIs. I am however happy to be convinced otherwise.