1. spring boot 支持 jsp
pom:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency>
2. 启动类添加 InternalResourceViewResolver bean 以便 controller 返回 jsp 可以获取到
//java项目 www.1b23.com @Bean public InternalResourceViewResolver setupViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/"); resolver.setSuffix(".jsp"); return resolver; }
3. 启动类添加 ConfigurableServletWebServerFactory bean 以便可以获取到 jsp 文件
//java项目 www.1b23.com @Bean public ConfigurableServletWebServerFactory serverFactory() { ConfigurableServletWebServerFactory aFactory = new TomcatServletWebServerFactory(); aFactory.setDocumentRoot(new File("jsp 服务器全路径")); return aFactory; }
本地开发时 serverFactory() 方法可直接返回 new TomcatServletWebServerFactory()