Reputation: 14126
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
Upvotes: 1
Views: 1073
Reputation: 3035
To your questions:
/WEB-INF/views/splittles.jsp
. If you'd return "hello_world", you would need a view /WEB-INF/views/hello_world.jsp
./my/super/vality/url
if you'd like - that's just the path under which you accept the (GET) request.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
Upvotes: 1