springboot:web项目支持jsp作为视图输出
2017-05-05 本文已影响1029人
无至
构建springboot项目
- 开发工具:Spring Tool Suite
- 创建项目:(略)
修改pom.xml支持jsp
<!-- spring boot tomcat jsp 支持开启 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- servlet支持开启 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- jstl 支持开启 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
增加jsp文件目录、文件、控制器
- 在main目录下创建webapp/WEB-INF/jsp文件夹
- 在jsp目录先创建index.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>Insert title here</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
- 添加IndexController控制器
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/")
public String index (Model model) {
model.addAttribute("message", "this is index jsp page");
return "index";
}
}
- 修改application.yml文件让springmvc支持视图的跳转目录指向为/main/webapp/WEB-INF/jsp
server:
port: 80
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
- 启动项目访问:http://localhost/