2

I am writing a web app with Spring 4.0.

I have written my controllers in what I believe to be the normal way using the @RequestMapping annotation to define the url pattern which the controller handles.

The snippet below illustrates this for a controller which displays a testimonial ...

@Controller
@RequestMapping("/testimonialView")
public class TestimonialRequestController {
    @RequestMapping(value="/{testimonialName}", method=RequestMethod.GET)
    public ModelAndView testimonialRequest(@PathVariable String testimonialName, ModelAndView modelAndView) throws FileNotFoundException {
        Testimonial testimonial;

        . . .
    }
}

Elsewhere in my application I want to generate a link bar which includes all the testimonials to include in my left hand nav.

At the moment, when I construct the href for the anchor element to go into the link bar, I am hardcoding the url, like this:

String href="/testimonialView/" + testimonialName;

This does not seem right. If later on I want to change the url structure I have to change it in at least two places - possibly more. Once where the incoming URL is matched to the controller, and once to construct the anchor element which a user will click to invoke that URL.

Is there a best practice way of dealing with this problem? It must be a common one. Is it as simple as using Constants to represent the URLs and accessing these from different places? I know my example is simple but I am assuming the problem must exist for much larger web apps with complex URL structure so I want to understand what best practice is.

I hope this isn't a stupid question. I am keen to ensure that I implement best practice right from the beginning. I have looked through Stackoverflow and Google but nothing quite answers this specific question.

Any help gratefully received.

2 Answers 2

3

The short answer is that you can't do this dynamically because @RequestMapping puts data into the code at compile time.

However, there are a couple of options that work.

  1. You can define the string constants in a separate class - this will make it easier for you to change the names of URLs
  2. You can explore the request mappings at runtime within Spring, so could have some code that found URLs you'd coded elsewhere - I've done this for identifying when a URL is dynamic content, vs coded content.

My recommendation is

public class URLs {

     public static final String TESTIMONIAL_VIEW = "/testimonialView";
}

with

@RequestMapping(URLs.TESTIMONIAL_VIEW)

and

String href= URLs.TESTIMONIAL_VIEW + "/" + testimonialName;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. Your suggested approach is what I had started to do when I wondered whether there was a better way - it seems not.
1

There isn't any better practice for this afaik. Most you can do is, as Ashley said, is to use constants. But as with any other client-server situation such as the Web, if you change the contract (the url in this case) you'll have to do so for both the client (i.e. the links) and the server (the controller mappings).

I would also mention that your controller can be more general, for example have a "Testimonials" controller and "view/{name}" as an action within that controller. Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.