新建一个项目,添加web支持 ,然后修改pom.xml。

复制代码
<!--添加jsp页面的解析依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

<!--jsp文件的存放以及编译目标文件夹-->
        <resources>
            <!--手动指springboot的jsp编译的位置,比如META-INF/resources-->
            <resource>
                <!--源文件夹-->
                <directory>src/main/webapp</directory>
                <!--指定编译文件夹-->
                <targetPath>META-INF/resources</targetPath>
                <!--指定哪些源文件要进行编译-->
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>
复制代码

然后,在项目结构里(project structure)里的module里添加当前项目的web支持。

然后,创建一个controller类

复制代码
package com.example.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping("/say")//此处say指定了浏览器访问地址,比如:http://127.0.0.1:8080/say
    public ModelAndView say(){
        ModelAndView mv=new ModelAndView();
        mv.addObject("message","hehe ,got it!");//此处message等下会在页面中自动配置,并显示后面的内容
        mv.setViewName("say");//此处say,与webapp中的页面文件同名,这样就能印射到相应的jsp文件上
        return mv;
    }
}
复制代码

然后,创建一个jsp文件得到ModelAndView对象,并显其中内容。

复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
复制代码