Springboot Integrated JSP stepping Pit

Source: Internet
Author: User

Springboot with its efficient development efficiency more and more in the development of small and medium-sized projects, and in the use of distributed development is also very extensive, Springboot officially recommended front-end framework is thymeleaf, and the default does not support JSP, Most Java developers are most familiar with the front-end development tool is JSP, their work encountered in this situation, so find some integrated online demo, but still tread a lot of pits, recorded for learning.

The code and configuration are as follows:

1. Pom.xml, this is my integration encountered in the biggest pit, mainly spring-boot-starter-parent version, their own first to check the official documents to the 2.0.3, but tried the next view parser does not work at all, and then tried 1.3.5 and 1.5.2, these two versions are mainly s The package location of the Pringbootservletinitializer class has changed, and after the package has been re-directed, the project will run, and the page can be accessed. Finally try again the next 2.0.1 version, Springbootservletinitializer class location, all configurations are the same, the project is normal, the individual is not aware of the latest Springboot version 2.0.3 why not, so it is recommended to follow the integration of JSP students use 2.0.1 version.

<?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.example</groupid><artifactid>springboot-jsp</artifactid><version>0.0.1-snapshot </version><packaging>war</packaging><name>springboot-jsp</name><description >demo Project for Spring boot</description><parent><groupid>org.springframework.boot</ groupid><artifactid>spring-boot-starter-parent</artifactid><version>2.0.1.release</ Version><relativepath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version></properties><dependencies>< Dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-web </artifactid></dependency><dependency><groupid>org.springframework.boot</groupid ><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency>< groupid>org.apache.tomcat.embed</groupid><artifactid>tomcat-embed-jasper</artifactid>< /dependency><!--servlet Support--><dependency><groupid>javax.servlet</groupid>< artifactid>javax.servlet-api</artifactid></dependency><!--Jstl Support--><dependency>< groupid>javax.servlet</groupid><artifactid>jstl</artifactid></dependency>< Dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-test</artifactid><scOpe>test</scope></dependency></dependencies><build><plugins><plugin> <groupid>org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</ Artifactid></plugin></plugins></build></project>

2, Springbootjspapplication

Package Com.example.demo;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.builder.springapplicationbuilder;import Org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplicationpublic class Springbootjspapplication extends springbootservletinitializer{@Overrideprotected springapplicationbuilder Configure (Springapplicationbuilder application) {return application.sources (springbootjspapplication.class);} public static void Main (string[] args) {Springapplication.run (springbootjspapplication.class, args);}}

3, Indexcontroller

Package Com.example.demo;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controllerpublic class Indexcontroller {@RequestMapping ( Value= "/index", method=requestmethod.get) public String index () {return ' index ';}}

4, Application.properties

spring.mvc.view.prefix=/web-inf/jsp/spring.mvc.view.suffix=. jsp

5. Create a new webapp/web-inf/jsp directory in the main directory and add index.jsp

<! DOCTYPE html><% @page contenttype= "text/html; Charset=utf-8 "language=" java "%>

The project directory structure is as follows, I am using sts:

Start the project in STS and Access Localhost:8080/index with the following effect:

Springboot Integrated JSP stepping Pit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.