Spring Boot Tutorial (10): Spring Boot integration jsp

1. Project preparation

Directly use the source code of the previous chapter, Spring Boot Tutorial (9): Spring Boot integrates Mapper4

2. Add dependencies

<!-- jsp -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

3. Modify the configuration file

Add jsp related configuration to the application.propertiesconfiguration file:

## jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# 配置Tomcat编码
server.tomcat.uri-encoding=UTF-8

Fourth, create a jsp file

  1. src/mainCreate webapp/WEB-INF/jspfolders under.
  2. create in jspfolderindex.jsp

index.jsp content:

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>首页</title>
</head>
<body>
    hello ${userName}
</body>
</html>

write picture description here

5. Create Controller

Add one IndexControllerwith the following content:

package com.songguoliang.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-08 10:47
 */
@Controller
public class IndexController {

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("index");
        view.addObject("userName", "蝈蝈");
        return view;
    }
}

6. Start the service

Start the service through spring-boot:run, just double-click in the idea:
write picture description here

Browser input: http://localhost:8080/index, the page displays:
write picture description here

Seven, attention

Springboot does not officially recommend the use of jsp, but recommends it Thymeleaf.





Source code:
github
code cloud

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616878&siteId=291194637