我是 Spring MVC &的新手经历了Craig Walls Spring4 in Action。
考虑一下代码段,
@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
}
图片显示 spittles.jsp 位于 / 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) 为什么我需要在控制器方法中返回字符串 “spittles” ?< / p>
2) 它(返回字符串)是否与
保持关系@RequestMapping( value =“/ spittles” ,method = RequestMethod.GET)
因为值( / spittles )与控制器方法中返回的字符串相同?
3) 当我输入时,为什么我看不到 .jsp扩展程序 网址
答案 0 :(得分:1)
问题:
/WEB-INF/views/splittles.jsp
。如果您返回&#34; hello_world&#34;,则需要查看/WEB-INF/views/hello_world.jsp
。/my/super/vality/url
如果您愿意 - 这只是您接受(GET)请求的路径。例如,您可以使用相同路径的控制器方法,一个回答GET,另一个回答POST请求,两者都会产生不同的视图:
@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";
}
您甚至可以返回splittles/jspName
之类的相对路径,这意味着您可以在文件夹中组织JSP - 这里/WEB-INF/views/splittles/something.jsp