Simple introduction to springboot (5-1): front-end interface-Jsp integration

1. Static resource access

Static resources: js, css, html, pictures, audio and video, etc.
Static resource path: refers to the path that the system can directly access, and all files under the path can be directly read by the user.
Spring Boot provides static resource directory location by default and needs to be placed under the classpath, and the directory name must comply with the following rules:

  1. /static

  2. /public

  3. /resources

  4. /META-INF/resources

    在resources目录下面建立static文件夹,在文件夹里面任意放张图片。命名为:enjoy.jpg
    

Insert picture description here

在地址栏上输入localhost:8080/enjoy.jpg,可以看到图片
2. JSP integration

Generally speaking, springboot does not recommend using jsp pages directly, but it does not rule out that some company projects still use jsp as the front-end interface.

The built-in tomcat of springboot does not integrate support for JSP, nor does it support EL expressions. Therefore, to use JSP, you should first integrate related dependencies.

1) Add in the pom file

      <!--JavaServer Pages Standard Tag Library,JSP标准标签库-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--内置tocat对Jsp支持的依赖,用于编译Jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

2) Since springmvc needs to parse jsp, it is necessary to configure the parser, and add the following in applicaiton.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

3) Create a new WEB-INF folder in resources and put an index.jsp page in it

Insert picture description here
The content is as follows:

<%@ 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>jsp页面</title>
</head>
<body>
	<h1>这是个jsp页面!!</h1>
</body>
</html>

4) Finally, create a new controller, note that the annotation here is @Controller, you must not use @RestController

package cn.enjoy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/jsp")
public class JspController {
    
    
    @RequestMapping("/hi")
    public String sayHello() {
    
    
        return "index";
    }
}
	在浏览器上输入:localhost:8080/jsp/hi,可以看到JSP页面。

The last chapter: springboot simple entry (D): Global Exception Handling (GlobalExceptionHandler class)
in the next chapter: springboot simple entry (5-2): front-end interface -thymeleaf template engine

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/109283316
Recommended