sprigBoot实现jsp页面跳转+配置mvc
前言:接上篇文章,boot整合mybatis后,我们想返回jsp页面。但boot不支持jsp,需要自行配置。
工具: idea
pom.xml引入
<!--引入jsp支持-->
<!--boot默认不支持jsp,添加以下依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
在src-main下创建webapp文件夹,在webapp下创建WEB-INF文件夹
注意项目架构除来了,但是我们的webapp只是一个普通的文件夹,我们要改变其性质:
完成webapp配置.
配置mvc视图
在application-dev.yml 下配置mvc
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://localhost:3306/gpyh_web_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
mybatis:
mapper-locations: classpath*:**/*Mapper.xml
type-aliases-package: com.example.entity
特别强调注意yml文件层级的编写,一定要有层级,否则读取不到配置
在webapp的WEB-INF下创建hello.jsp
<%--
Created by IntelliJ IDEA.
User: zzr
Date: 2019/3/18
Time: 10:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
上帝保佑${abc}
</body>
</html>
在我们的controller中编写访问路径
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author zhaozeren
* @version 1.0
* @date 2019/3/18
*/
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getUser")
@ResponseBody
public User getUser(){
return userService.getUser();
}
@RequestMapping("hello")
public String helloJsp(ModelMap modelMap){
modelMap.put("abc","-阿门");
return "hello";
}
}
访问:http://localhost:8888/user/hello
即完成boot 实现 jsp 页面的跳转