1

I am new to Spring MVC & going through Craig Walls Spring4 in Action.

Consider the snippet,

@RequestMapping(value = "/spittles", method = RequestMethod.GET)
    public String spittles(Model model, @RequestParam("max") long max,
                           @RequestParam("count") int count) {

        model.addAttribute("spittleList",spittleRepository.findSpittles(max, count));

        return "spittles";                 // <-- return view name
}

The image shows the spittles.jsp resides in /WEB-INF/views/

enter image description here

WebConfig.java:

@Configuration
@EnableWebMvc                   // Enable Spring MVC
@ComponentScan(basePackages={"org.spittr"})
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver resolver =
            new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);

        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
                         DefaultServletHandlerConfigurer configurer) {

        /* configure static content handling */
        configurer.enable();
    }



}

1) Why do I need to return the string "spittles" in the controller method?

2) Does it(return string) holds a relationship to the

@RequestMapping(value = "/spittles", method = RequestMethod.GET)

as the value(/spittles) is the same as the returned string in the controller method?

3) Why don't I see a .jsp extension when i enter the URL

http://localhost:8080/web/spittles?max=238900&count=5

and the o/p is resolved as:enter image description here

3
  • It is all related to how View Resolvers work in MVC. ad 1) It is the name of the view (the jsp page) that should be displayed to the user. ad 2) No. URL mapping is not directly related to view resolving. ad 3) In MVC you only see name of the view (JSP is only presentation layer, you can use Thymeleaf instead of JSP and it will work in a very similar way).
    – Rafal G.
    Sep 7, 2015 at 8:59
  • @R4J: But it works too if I just return null from the controller method. Sep 7, 2015 at 9:09
  • 1
    yes it works even if you return null because the view resolver InternalResourceViewResolver resolves views for rendering @Controllers by adding prefix/suffix for a jsp. If that JSP exists then it renders else shows jsp not found error. Sep 7, 2015 at 13:56

1 Answer 1

1

To your questions:

  1. The String "spittles" will be passed to the view resolver, which looks for a view /WEB-INF/views/splittles.jsp. If you'd return "hello_world", you would need a view /WEB-INF/views/hello_world.jsp.
  2. No - that's the controller URL. You could define a completely different Controller URL like e.g. /my/super/vality/url if you'd like - that's just the path under which you accept the (GET) request.
  3. See answers to 1.) and 2.) Although its good practice keep Spring-Controller-URLs and JSP view names alike, so it is obvious to the developer what's happening here.

You could for example have to controller methods for the same path and one answering to GET and the other answering to POST requests and both resulting in differne views:

@RequestMapping(value = "/spittles", method = RequestMethod.GET)
public String spittles(Model model, @RequestParam("max") long max,
                       @RequestParam("count") int count) {
  // ...
  return "splittles_get";
}

@RequestMapping(value = "/spittles", method = RequestMethod.POST)
public String spittles(Model model, @RequestParam("max") long max,
                       @RequestParam("count") int count) {
  // ...
  return "splittles_post";
}

You can even return a relative path like splittles/jspName meaning that you can organize your JSPs in folders - here /WEB-INF/views/splittles/something.jsp

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.