Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the recommended approaches for generating routes in Spring MVC?

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.

like image 795
Zach Dennis Avatar asked Apr 19 '11 19:04

Zach Dennis


People also ask

Which method should be overridden by a class implementing controller interface in Spring MVC?

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.

What is the preferred way to designate a class is a controller?

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.

How can we call controller method from JSP in Spring MVC?

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.


1 Answers

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.

like image 166
kieron Avatar answered Oct 05 '22 20:10

kieron