What are the recommended approaches for generating routes in a java-based Spring MVC webapp? I'd like to avoid hard-coding paths throughout the application.
AbstractController. It provides a basic infrastructure and all of Spring's Controller inherit from AbstractController. It offers caching support, setting of the mime type etc. It has an abstract method 'handleRequestInternal(HttpServletRequest, HttpServletResponse)' which should be overridden by subclass.
The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.
and [1] copy the <button/> and <script/> elements to your JSP, [2] change the URL to point to your controller and, [3] create an element <div id="demo"></div> in your JSP then, on clicking the button in your page, this <div/> should be updated to display the String returned by your controller.
Assuming we are talking about paths RequestMappings etc.
In my application I have a single class with with a bunch of public static final
constants that I reference in the @RequestMapping annotations.
So the constants might look like this:
public class Pages {
public static final String FOO_URL "foo/foo.action"
public static final String FOO_VIEW "test/foo"
public static final String BAR_URL "bar/bar.action"
public static final String BAR_VIEW "test/bar"
}
then inside your controller you'd reference them like this:
@RequestMapping(value=Pages.FOO_URL, method=RequestMethod.GET)
public String handleFoo(Model model) {
return Pages.FOO_VIEW;
}
They're easy to change as they're all in one place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With