1、配置pom.xml
1 <!--配置jsp jstl的支持-->
2 <dependency>
3 <groupId>javax.servlet</groupId>
4 <artifactId>jstl</artifactId>
5 </dependency>
6
7 <!--对jsp的支持-->
8 <dependency>
9 <groupId>org.apache.tomcat.embed</groupId>
10 <artifactId>tomcat-embed-jasper</artifactId>
11 </dependency>
2、配置application.properties(在src/main下创建webapp文件)
# 配置jsp文件的位置,默认位置为:src/main/webapp
spring.mvc.view.prefix:/WEB-INF/jsp/ #指向jsp文件位置:src/main/webapp/WEB-INF/jsp
# 配置jsp文件的后缀
spring.mvc.view.suffix:.jsp
3、编写jsp(文件放到src/main/webapp/WEB-INF/jsp目录下)
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <html>
4 <head><title>登录页面</title></head>
5 <body>
6 <form action="login.do" method="post">
7 <table>
8 <tr><td>学 号:</td>
9 <td><input type="text" name="num"/></td>
10 </tr>
11 <tr><td>密 码:</td>
12 <td><input type="password" name="password"/></td>
13 </tr>
14 <tr><td><input type="submit" value="登录"/></td>
15 </tr>
16 </table>
17 </form>
18 </body>
19 </html>
4、编写控制类
1 package com.example.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5
6
7 //这个注解不能使用RestController,不然会返回模板类型的页面
8 @Controller
9 public class LoginController {
10 @RequestMapping("/login")
11 public String index() {
12
13 return "login";
14 }
15 }
5、编写启动类,重写SpringBootServletInitializer方法
1 package com.example.controller;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.boot.builder.SpringApplicationBuilder;
6 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7 @SpringBootApplication
8 //返回jsp页面必须继承SpringBootServletInitializer类重写里面的方法
9 public class SpringbootMybatisApplication extends SpringBootServletInitializer {
10
11 public static void main(String[] args) {
12 SpringApplication.run(SpringbootMybatisApplication.class, args);
13 }
14
15 @Override
16 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
17 return application.sources(SpringbootMybatisApplication.class);
18 }
19 }