java - Spring MVC : Generating links which match the controller url pattern -


I'm writing a web app with Spring 4.0.

I have written to my controllers who I believe, using the @RequestMapping annotation to define the URL pattern in general, which handles the controller.

The snippet below shows a testimonial that displays a testimonial ...

Somewhere else in my application, I want to generate a link bar which contains all the testimonials to join in my left hand.

At this time, when I go to the link bar to create an href for the anchor element, I am hardening the URL such as:

  string href = " / Testimonial view / "+ testimonialname;  

This does not look right if I want to change the URL structure later, I have to convert it to at least two places - possibly more often, where the incoming URL matches the controller , And to create an anchor element once the user will click to invite that URL.

What is the best way to deal with this problem? Should it be a normal, is it easy to use the constants to represent the URL and access them from various places? I know that my example is simple, but I understand that there will be a problem for large web applications with complex URL structures, so I have to understand what is the best practice.

I hope this is not a stupid question. I am eager to make sure that I follow the best practice from the beginning. I have seen through StackWorfarflow and Google, but nothing does not answer this specific question.

Any help received with gratitude

The short answer is that you can not do this dynamically because @RequestMapping data is compiled Puts in the data.

However, there are some options that work.

P>
  1. You can define a string constant in a different square - it makes it easier for you to change the names of the URLs
  2. You request requests at runtime within the Spring You can find mapping, so there may be some code that urls you elsewhere - I've done this to make sure that when a URL is dynamic content, then by creating coded content.

My recommendation is

  Public category URLs {public static final string testimonial_ VIEW = "/ testimonial view";  

with

  @RequestMapping (URLs.TESTIMONIAL_VIEW)  

and

 < Code> string href = URLs.TESTIMONIAL_VIEW + "/" + testimonialName;  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -