天天看点

SpringBoot后台跳转到jsp页面

代码已上传,GitHub地址

转载原因是担心原文章丢失,找了好久才找到的有用的做一个记录,防止碰到问题找不到有用文章。

1. 配置pom.xml

a. html配置和jsp配置冲突,如果配置jsp支持,就要删除html支持

b. tomcat-embed-jasper必须使用版本号,不能使用provided

<!--支持html-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
        <!--</dependency>-->
        <!-- jsp标签库 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>7.0.59</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
           

2. 配置application.properties

#在application.properties中配置jsp设置
#目录对应src/main/webapp下
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
           

3. 配置webapp

a. src/main目录下创建文件夹webapp
b. File->ProjectStructure->ProjectSetting->Modules
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191009155439850.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmdzaHVhaW9ubGluZQ==,size_16,color_FFFFFF,t_70 =600x250)
`注意DeploymentDescriptors下面的路径要指向webapp文件下`
c. 创建jsp文件夹,结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191009155735492.png)
           

4. 编写jsp

jsp文件夹下创建test.jsp
           
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1 style="color: red">Hello World</h1>
</body>
</html>
           

5. 编写java代码

/**
 * 返回jsp页面
 * */
@Controller
public class UserController {
    @RequestMapping("/")
    public String hello() {
        System.out.print("bbbbbb\n");
        return "test";
    }
}
           

6. 结果

SpringBoot后台跳转到jsp页面

继续阅读