欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot Web应用返回JSP页面配置

程序员文章站 2022-07-15 11:59:59
...

1.要返回jsp页面需要以下配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

2.在application.yml里配置如下信息:

#路径名称按自己需要命名,在WEB-INF下新建文件夹page
spring:
  mvc:
    view:
      prefix: /WEB-INF/page/
      suffix: .jsp

3.在Controller里没什么变化

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")
public class IndexController {

    @RequestMapping("/page")
    public String indexPage(ModelMap model) {
        model.addAttribute("hello", "13212");
        return "hello";
    }
}

4.jsp内容:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello</title>
</head>
<body>

    <h3>Hello</h3>

    <div style="width:200px;height:100px;border:1px solid gray;">${hello }</div>
</body>
</html>

5.浏览器输入

http://localhost:8001/index/page

视图:
SpringBoot Web应用返回JSP页面配置

相关标签: web应用