Spring Boot Add JSP support

Source: Internet
Author: User

Outline

(1) Create MAVEN Web project;

(2) add dependency in Pom.xml file

(3) Configuring Application.properties support JSP

(4) Writing test controller

(5) Writing JSP pages

(6) Writing the startup class App.java

Create a MAVEN Web project

Use Eclipse to create a new Maven Web project named: spring-boot-jsp

Adding dependencies in the Pom.xml file

<!--spring Boot parent node, after introducing this, there is no need to introduce a version in the following spring boot related; -
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<!--Specify the JDK version, where we use JDK 1.8, which by default is 1.6--
<java.version>1.8</java.version>

<!--Web Support: 1, Web MVC; 2, restful; 3, Jackjson support; 4. AOP ...--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--servlet dependency. -
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

JSTL (JSP standard tag LIBRARY,JSP standards Tag library) is an ever-improving open source JSP tag library that is maintained by Apache's Jakarta team.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!--Tomcat Support .-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

Application.properties Configuration

Add Src/main/resources/application.properties:

# page Default prefix directory
spring.mvc.view.prefix=/web-inf/jsp/
# response page Default suffix
spring.mvc.view.suffix=.jsp
# Custom properties that can be read in the controller
Application.hello=hello Angel from Application

Writing a test controller

@Controller
public class Hellocontroller {
Private String Hello;

@RequestMapping ("/hellojsp")
Public String hellojsp (map<string,object> Map) {
System.out.println ("hellocontroller.hellojsp (). Hello=hello");
Map.put ("Hello", hello);
return "hellojsp";
}
}

Writing JSP pages

Create the webapp/web-inf/jsp directory below Src/main to store our JSP page: hellojsp.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
hellojsp
${hello}

</body>

Writing startup classes

@SpringBootApplication
public class App {

public static void Main (string[] args) {
Springapplication.run (App.class, args);
}
}

The code is as follows:

Pom.xml

<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.kfit</groupId> <artifactid>spring-boot-jsp</artifactid > <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name> spring-boot-jsp Maven webapp</name> <url>http://maven.apache.org</url>    <!--spring Boot parent node, after introducing this, there is no need to introduce a version in the following spring boot related; -<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-bo ot-starter-parent</artifactid> <version>1.4.1.RELEASE</version> </parent> <pro perties> <!--Specify the version of the JDK, where we use JDK 1.8, which by default is 1.6--<java.version>1.8</java.version> </p Roperties> <dependencies> <!--Web support: 1, Web MVC; 2, restful; 3, Jackjson support;            4. AOP ...-<dependency> <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--servlet Depend on. -<dependency> <groupId>javax.servlet</groupId> <artifactid>java X.servlet-api</artifactid> <scope>provided</scope> </depeNdency> <!-- JSTL (JSP standard tag LIBRARY,JSP standards Tag library) is an ever-improving open source JSP tag library that is maintained by Apache's Jakarta team. -<dependency> <groupId>javax.servlet</groupId> <artifactid>jstl            </artifactId> </dependency> <!--tomcat support .--> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-tomcat            </artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactid>tomcat-embed-jasper</artif   actid> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>spring-boot-jsp</finalName> </build></project>

Controller class
 Package Com.kfit; Import Java.util.Map; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; @Controller  Public class Hellocontroller {        @RequestMapping ("/index")    Public String index (MAP <String,Object> map) {        map.put ("name", "Andy");         return "Index";}    }

Configuration class

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

Start class

 Package Com.kfit; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication  Public class App {    publicstaticvoid  main (string[] args) {        Springapplication.run (App.class, args);}    }

Spring boot add 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.