This article is a brief summary of Springboot using JSP demo.
Objective
In the early days of the project, JSP is our regular use of the Java Server page, in fact, is a simplified servlet design, before the beginning of this article, review the JSP several large objects.
And in the current Springboot framework popular era, Springboot recommended template engine also changed to Thymeleaf, this article to introduce springboot use JSP page.
New Project
Create a project Springboot_jsp,pom file to include tomcat-embed-jasper dependencies and jstl tag library dependencies, the complete POM code is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.dalaoyang</ groupid> <artifactId>springboot_jsp</artifactId> <version>0.0.1-SNAPSHOT</version> < ;p ackaging>war</packaging> <name>springboot_jsp</name> <description>springboot_jsp </description> <parent> <groupId>org.springframework.boot</groupId> <artifacti D>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relative Path/> <!--lookup parent from repository to </parent> <properties> <project.build.s Ourceencoding>utf-8</project.build.sourceeNcoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java. version>1.8</java.version> </properties> <dependencies> <dependency> &L T;groupid>org.springframework.boot</groupid> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</ artifactid> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artif Actid> <scope>test</scope> </dependency> <dependency> <groupid>org.apache.tom Cat.embed</groupid> <artifactId>tomcat-embed-jasper</artifactId> <scope>prov ided</scope> </dependency> <dependency> <groupid>javax.servlet</groupi d> <artifactId>jstl</artifactId> </dependency> </dependencies> <buil d> <plugins> <plugin> <groupid>org.springframework.boot</groupid& Gt <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </b Uild></project>
Create a JSP Drop folder
Create the WebApp folder under the Src/main directory, and in order to accommodate the traditional structure, create a Web-inf folder under WebApp and create a JSP folder within the newly created Web-inf folder. (The structure can be designed by itself, no need to follow this demo), directory structure
Configuration file
In the configuration file, configure the newly created directory, along with the JSP file suffix, configured as follows:
server.port=8888spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
New Controller
Create a Indexcontroller that writes a jump method with the following code:
package com.dalaoyang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;/** * @author dalaoyang * @project springboot_learn * @package com.dalaoyang.controller * @email [email protected] * @date 2018/8/13 */@Controllerpublic class IndexController { @GetMapping("/") public String index(Model model){ model.addAttribute("name", "dalaoyang"); return "index"; }}
Create a JSP
Create a new index.jsp below src/main/webapp/web-inf/jsp with the following code:
<!DOCTYPE html>
Run Tests
Start the project and access the http://localhost:8888/in the browser, showing the following results:
SOURCE download: Big old Yang Code cloud
Springboot using JSPs