This is the 28th day of my participation in the August Challenge

Personally feel this document to write no meaning, but eventually sometimes need to face some novice, they still compare JSP, so fat sen or will this document tidy up

SpringBoot does not support JSP by default, because JSP has low performance compared to some template engines. Thymeleaf is officially recommended, and needs to be initialized if you want to use it in your project.

1. List of questions

  • Changes to JSP take effect only after a restart

    In production environments, Recompiling JSPS by SpringBoot can cause significant performance losses and can be difficult to trace to the root of the problem, so this feature is officially disabled by default in the latest release, as described in the initialization parameters of the JspServlet class.

    • Using DevTools (the way I used it)
    • Add the configurationserver.servlet.jsp.init-parameters.development=true
  • How to avoid various 404’s

    • Import the Tomcat + JASPER + JSTL
    • You must create a WebApp directory

2. Installation procedure description

A. Build by scaffolding

First, Spring Initializr was used to construct the project, and war type was selected for construction. The overall structure diagram is as follows:

Note that we checked the WAR option

B. Add dependencies

<! -- JSTL support,c tag -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>compile</scope>
</dependency>
<! Tomcat JSP support enabled -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<! Hot deployment: fix compilation issues -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
<! --Provided start-->
<! War package deployed to external Tomcat already includes these dependencies, so you need to add the following dependencies otherwise it will conflict with the embedded Tomcat container -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
Copy the code

C. modify application. The properties

SRC /main/webapp/ web-INF/JSP / Prefix =/ web-INF/spring.vcv.view. suffix=.jsp # static file access configuration in the resources folder static folder, which is used to store js, Server.tomcat. uri-encoding=UTF-8 server.port=8001 server.servlet.context-path=/bootCopy the code

D. Create a JSP page

  • Create the webApp /WEB-INF/ JSP folder under SRC /main.

  • Create hello.jsp in the JSP folder
<%@ page contentType="text/html; charset=UTF-8" language="java" %><html>
    <head>
        <title>Title</title>
    </head>
    <body>
        Hello,${requestScope.name}
    </body>
</html>
Copy the code

E. create the Controller

package com.hanpang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping({"/","/os01"})
    public String test01(Model model){
        model.addAttribute("name".Fat Sen.);
        return "jsp/hello"; }}Copy the code

F. Start the project

  • Boot method 1: Start Demo02BootApplication in the IDE and open the project address.

  • Startup mode 2: Deploy Tomcat on an external server. After the startup is complete, open the project address. It is important to note that when deployed with external Tomcat, the embedded container needs to be adjusted to the Provided level. (Conflict prevention)

    ** My question: ** Here I deployed to the external Tomcat, is access success, but every time to modify JSP, I need to restart JSP, I personally feel very headache, do not know whether there is a solution, Baidu, but did not find the answer I want!

3. Last word

Since Spring Boot does not recommend using JSP, thymeleaf is the only option, while others such as Freemarker are also available.