How do I use JSP in Springboot? But strongly not recommended, decisive change themeleaf it

Source: Internet
Author: User

Do Web projects, must have used JSP this big. Spring MVC also makes it easy to associate a JSP with a view, which is very convenient to use. When you move from a traditional spring MVC project to a Spring boot project, you find that the JSP and view associations are a bit cumbersome because the official deprecated JSP is used in spring boot. In my opinion, the continued use of this cumbersome procedure to support JSP is just for simple compatibility.

Let's start by looking at how to use JSP in Springboot.

1. Add support for JSP dependencies in POM.XM

        <dependency>           <groupId>org.apache.tomcat.embed</groupId>           <artifactId> tomcat-embed-jasper</artifactid>           <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet.jsp.jstl</groupId>            <artifactid>jstl-api </artifactId>            <version>1.2</version>        </dependency>

2. Configure the JSP and traditional spring MVC and view associations in the Src/main/resources/application.properties file

# MVCspring.view.prefix=/web-inf/views/spring.view.suffix=.jsp

3. Create the Src/main/webapp/web-inf/views directory, the JSP file is placed here

<! DOCTYPE html>    Hello ${name}</body>

4. Writing a Controller

Package com.chry.study;

Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.ModelAndView;

@Controller @enableautoconfiguration  Public class Samplecontroller {    @RequestMapping ("/hello")    public  Modelandview Getlistautentiview () {        new  modelmap ();        Model.addattribute ("name", "Spring Boot");        return New Modelandview ("Hello", model);    } }

5. Writing the Application class

 PackageCom.chry.study;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.builder.SpringApplicationBuilder;ImportOrg.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication Public classWebApplicationextendsSpringbootservletinitializer {@OverrideprotectedSpringapplicationbuilder Configure (Springapplicationbuilder application) {returnApplication.sources (WebApplication.class); }     Public Static voidMain (string[] args)throwsException {springapplication.run (webapplication.class, args); }}

6. After running in Java application Mode, you can access the Http://locahost:8080/hello

================== Split Line ==================================

The above code pom.xml in Javax.servlet.jsp.jstl is used to support the JSP tag library, there is no problem in the Web2.5 container, single when your container is Web3.0 or later, there will be problems. This is a very problem with the father.

The first problem: Javax.servlet.jsp.jstl will automatically load dependent Servlet-api-2.5.jar, and will overwrite the 3.1 version of JAVAX.SERVLET-API that support Web3.0 when it is actually running. Even if you show up in Pom.xml, adding the 3.1 version of Javax.servlet-api is useless. Causes the Springboot app to throw runtime exception run errors.

The second problem: because the 3.1 version of Javax.servlet-api is overwritten, jquery over 2.0 is not working, and the result is that many of your JavaScript scripts based on the new version of jwuery2.0 are faulty or not executed.

This is an irreconcilable contradiction, need not javax.servlet.jsp.jstl, need not Web3.0.

But in most cases, the JSTL tag library is not required, and Web3.0 is required. The alternative is to use themeleaf instead of JSP.

How do I use JSP in Springboot? But strongly not recommended, decisive change themeleaf it

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.