Springboot Configuring JSP Support

Source: Internet
Author: User

Springboot does not support JSP templates by default, so it needs to be configured.

Here is an example that can be run:

First configure the properties file:

Spring.http.encoding.force=True
Spring.http.encoding.charset=utf-8
spring.http.encoding.enabled=true
Server.tomcat.uri-encoding=utf-8

Server.port=8080
/web-inf/jsp/
Spring.mvc.view.suffix:. jsp
Application.message:Hello World

Then configure the Startup class:

 Packagesample.jsp;

Importorg.springframework.boot.SpringApplication;
Importorg.springframework.boot.autoconfigure.SpringBootApplication;
ImportOrg.springframework.boot.builder.SpringApplicationBuilder;
ImportOrg.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
Public classApplicationextendsSpringbootservletinitializer {

@Override
protectedSpringapplicationbuilder Configure (Springapplicationbuilder application) {
returnApplication.sources (Application.class);
}

Public Static voidMain (string[] args)throwsException {
Springapplication.run (Application.class, args);
}

}

Finally write the control layer:

 Packagesample.jsp;

Importjava.util.Date;
ImportJava.util.Map;
ImportOrg.springframework.beans.factory.annotation.Value;
ImportOrg.springframework.stereotype.Controller;
Importorg.springframework.web.bind.annotation.GetMapping;
Importorg.springframework.web.bind.annotation.RequestMapping;

@Controller
Public classWelcomecontroller {

@Value ("${application.message}")
PrivateString message;

@GetMapping ("/welcome")
PublicString Welcome (map<string, object>model) {
Model.put ("Time",NewDate (). GetTime ());
Model.put ("Message", This. Message);
return"Welcome";
}

@GetMapping ("/test")
PublicString Test () {
return"Test";
}

}

All configuration of the Pom.xml file:

<?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>spring-boot-sample-web-jsp</groupId>
<artifactId>ming</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring Boot Web JSP sample</name>
<description>spring Boot Web JSP sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>pivotal Software, inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/. /.. </main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
<springboot.version>1.5.4.RELEASE</springboot.version>
</properties>
<dependencies>
<!--Compile--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--provided--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${springboot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.6</version>
<scope>provided</scope>
</dependency>
<!--Test-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>

Example Download: http://pan.baidu.com/s/1pKNDJD5

Springboot Configuring JSP Support

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.