如果您使用 spring boot,则不能在 application.properties 文件中设置属性spring.mvc.view.prefix
,而是从配置类spring.mvc.view.suffix
配置 bean 。ViewResolver
应用程序属性
# Configured in @Configuration GuestNav
#spring.mvc.view.prefix=/WEB-INF/views/
#spring.mvc.view.suffix=.jsp
# Live reload
spring.devtools.restart.additional-paths=.
# Better logging
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
主要方法
@SpringBootApplication
public class WebApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApp.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApp.class, args);
}
}
配置类
@Configuration
@EnableWebMvc
public class DispatcherConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/views/**").addResourceLocations("/views/");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/notinuse/");
viewResolver.setSuffix("");
return viewResolver;
}
}
控制器类
@Controller
public class GuestNav {
@GetMapping("/")
public String home() {
return "forward:/views/guest/index.html";
}
}
您必须将文件放在目录/webapp/views/guest/index.html
中,请注意,webapp 目录位于资源目录之外。
通过这种方式,您可以使用 spring-mvc 的 url 模式,但提供静态上下文。