「这是我参与2022首次更文挑战的第13天,活动详情查看:2022首次更文挑战」
1、pom.文件调整
依赖(由于本身springboot没有支持jsp,所以需要添加外部jsp依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
插件(在打包的时候,需要这2个插件)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
打包方式(另外,要运行于springboot中默认的tomcat或jetty,只能用war方式)
war
2、application.properties设置
spring.mvc.view.prefix: /jsp/
spring.mvc.view.suffix: .jsp
必须要设置这个路径
jsp存储路径
src/main/webapp目录下的jsp目录
3、controller,页面响应设置
@Controller
public class ViewController {
@RequestMapping("/")
public ModelAndView ad() {
System.out.println("ad ad");
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
@RequestMapping("/ad")
public ModelAndView ada() {
System.out.println("ad ad");
ModelAndView mv = new ModelAndView();
mv.setViewName("NewFile");
return mv;
}
}
\
4、运行
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
默认情况下,直接运行application即可;
打包运行时候,需要运行war,如:java -jar xx.war
spring boot 对jsp的限制说明
When running a Spring Boot application that uses an embedded servlet container (and is packaged as
an executable archive), there are some limitations in the JSP support.
• With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
• Undertow does not support JSPs.
• Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
\
\
\