Pet adoption website: SpringBoot supports JSP development and configuration

Pet adoption website (2): SpringBoot supports JSP development and configuration

Spring boot originally does not recommend JSP as a template. If you want to use JSP for development, you need to introduce the package separately

1.build.gradle configuration

Configuration packing method: war
Introduction package: Tomcat embedded Jasper and jstl

  1. Go to maven's central warehouse website to find the corresponding package
    Baidu searches maven warehouse, finds maven's central warehouse, and clicks the first

    Click the required dependency package in maven warehouse

    Find the package of the version you need and click the version number

    Select gradle, copy dependency, and paste it into the build.gradle file

compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.27'
compile group: 'javax.servlet', name: 'jstl', version: '1.2'

  1. First, find the build.gradle file, and configure the packing method and dependency in the build.gradle file


After pasting the dependency, you may be prompted about gradle, as shown in the following figure:

Click Import Changes to import your currently configured dependencies
Click enable auto import to import the dependency automatically, and then the file changes will be imported automatically

If there is no prompt, click Gradle on the right side of IDEA, and click refresh to import manually

After the import is successful, the dependent packages will be displayed under Dependencies

2. Build webapp directory

1. Click File - Project Structure

Click Model, then click "+", and select web from the drop-down list

Delete old web.xml file

After deletion, click "+" again to create a new web.xml file

Modify the location of devlopment to src\main\webapp\WEB-INF\web.xml

Click ok, click apply

3. Add page and Controller layer

Under the WEB-INF folder, create a new folder named views to store JSP pages, and add page index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h1>Pet adoption website</h1>

</body>
</html>

Create a new Controller layer and a new UserController class under the package

package com.design.graduation.adoptpet.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @RequestMapping(value={"/","/index"})
    public String indexPage(){
        return "index";
    }

}

4. Add properties to the application.properties file

Locate the application.properties file in the resource folder

# Custom properties, which can be read in the Controller
# Page default prefix directory
spring.mvc.view.prefix=/WEB-INF/views/
# Response page default suffix
spring.mvc.view.suffix=.jsp

# Set port number
server.port=8888
#Set session expiration time
server.servlet.session.timeout=90m
#Whether to keep session data between restarts
server.servlet.session.persistent=false
#Maximum age of session cookie s(one day)
server.servlet.session.cookie.max-age=1d

# Set up mysql database
spring.datasource.url=jdbc:mysql://localhost:3306/adoptpet?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasourcee.hikari.test1.driver-class-name=com.mysql.jdbc.Driver

5. Project operation and test


After the startup is successful, open the page through IP and port, and display it normally

Published 3 original articles, praised 0, visited 17
Private letter follow

Tags: Gradle Spring JSP Session

Posted on Fri, 31 Jan 2020 02:32:08 -0500 by hucklebezzer