Springboot Integrated JSP

Source: Internet
Author: User

Some time ago to see the next springboot some things, feel springboot use is very convenient and simple, so also wrote a few small applications, later in Springboot

How to use the JSP above the question, consulted the multi-data, found the other people's blog description, also found spring on the GitHub of the example, after reading a slight change after the success

Integrated JSP, so decided to record the integration process.

Regardless of the type of IDE used, the basic use of MAVEN is the same, this article uses Intelj, creating MAVEN Web Engineering, POM relies on the following:

<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>cn.com</groupId> <artifactid>springboot-web</ artifactid> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name> Springboot-web Maven webapp</name> <url>http://maven.apache.org</url> <parent> <groupid& Gt;org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> < version>1.2.5.release</version> <relativePath/> </parent> <properties> <project.buil D.sourceencoding>utf-8</project.build.sourceencoding> <java.version>1.7</java.version> < Version.spring>3.2.9.release</version.spring> <version.jackson>2.4.4</version.jackson> </properties> <dependencies> <dependency> <groupid>j avax.servlet</groupid> <artifactId>jstl</artifactId> <version>1.2</version> &lt ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid&gt ;spring-boot-starter-web</artifactid> </dependency> <!--webjars--> <dependency> < Groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid > <scope>test</scope> </dependency> <dependency> &LT;GROUPID&GT;ORG.APACHE.TOMC At.embed</groupid> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scop e> </dependency> </dependencies> <build> <plugins> <plugin> <group Id>org.springframework.boot&lT;/groupid> <artifactId>spring-boot-maven-plugin</artifactId> <!--<version>${spring. Boot.version}</version>--> <configuration> <mainclass>cn.com.springbootwebapplication          </mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal> Repackage &LT;/GOAL&G            T </goals> </execution> </executions> </plugin> </plugins> </build > <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.sp Ring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </ repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/mil Estone</url>     <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http:/ /repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milest ones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginrepos Itories></project>
One of the most important things to note is the following dependency, which is less than the use of JSP.

    <dependency>      <groupId>org.apache.tomcat.embed</groupId>      <artifactId> tomcat-embed-jasper</artifactid>      <scope>provided</scope>    </dependency>
At the same time, the application.properties in resources are configured as follows:

    spring.view.prefix=/web-inf/jsp/    spring.view.suffix=.jsp    Application.message:Hello Phil    server.port=8282
The bottom two configuration is optional, the above two configuration, is the configuration JSP directory, here and the official website slightly different, the official website is equipped with Spring.mvc.view configuration, but so

Does not seem to work, has the understanding can discuss with me.

The following is the controller to write, specifically no longer explained, only post code:

Package Cn.com.controller;import Org.springframework.beans.factory.annotation.value;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Java.util.date;import java.util.map;/** * Created by Administrator on 2016/5/6. */@Controllerpublic class Indexcontroller {    @Value ("${application.message:hello World}")    private String message = "Hello World";    @RequestMapping ("/") Public    String Welcome (map<string, object> model) {        model.put ("Time", New Date ());        Model.put ("message", this.message);        return "Welcome";    }}
At the same time, the startup class is written as follows:

Package Cn.com;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.builder.springapplicationbuilder;import org.springframework.boot.context.web.springbootservletinitializer;/** * Created by Administrator on 2016/5/6. */@SpringBootApplicationpublic class Springbootwebapplication extends Springbootservletinitializer {    @Override    protected Springapplicationbuilder Configure (Springapplicationbuilder application) {        return Application.sources (Springbootwebapplication.class);    }    public static void Main (string[] args) throws Exception {        Springapplication.run (Springbootwebapplication.class, args);}    }
Here, the JSP must inherit the Springbootservletinitializer class, where the original text of the other article is used:

For applications that need to be deployed to traditional servlet containers, boot provides a way to initialize the Web configuration in a coded manner. To use this, boot provides an optional Webapplicationinitializer,

It uses the servlet container to register the app, which registers the servlet in a coded manner through the Servlet 3.0 API and uses ServletContext. By providing a subclass of Springbootservletinitializer,

The boot application can register the configuration with the embedded spring context, which is created when the container is initialized.

After that, you can start the Springboot app using JSPs.

The integration code is downloaded as: GitHub

Springboot Integrated JSP

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.