Relation b/w view name in the controller method an

2019-08-11 09:59发布

问题:

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/

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:

回答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