SpringBoot Web應用返回JSP頁面配置

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

視圖:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章