Spring Boot Add JSP supports "Go"

Source: Internet
Author: User

Spring Boot Add JSP support

Approximate steps:

(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

1,freemarker
2,groovy
3,thymeleaf (Use this on spring website)
4,velocity
5,jsp (seemingly spring boot officially does not recommend, STS created project will have a templates directory under Src/main/resources, here is let us put the template file, and then did not generate such as Springmvc WebApp directory)
However, this article is to choose everyone familiar with the JSP for example, because the use of JSP and the default Support Template needs special processing, so take the example better.

( 1 ) to create Maven Web Project

To create a new MAVEN Web project with Eclipse, the project is named:

spring-boot-jsp

( 2 ) in Pom.xml file Add Dependency

<!--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.3.3.RELEASE</version>

</parent>

Dependent Packages:

<!--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. Jstl can only be run on containers that support the JSP1.2 and Servlet2.3 specifications, such as Tomcat 4.x. It is also supported as a standard in JSP 2.0.

Otherwise report abnormal information:

Javax.servlet.ServletException:Circular view Path [/hellojsp]: would dispatch back to the current handler URL [/hellojsp] Again. Check your Viewresolver setup! (Hint:this May is the result of a unspecified view, due to default view name generation.)

-

<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>

JDK compiled version:

<build>

<finalName>spring-boot-jsp</finalName>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

(3) Application.properties configuration

Above said Spring-boot does not recommend JSP, want to use JSP need to configure Application.properties.
Add src/main/resources/application.properties content:

# 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

(4) Writing test controller

Write class: Com.kfit.jsp.controller.HelloController:

Package Com.kfit.jsp.controller;

Import Java.util.Map;

Import Org.springframework.beans.factory.annotation.Value;

Import Org.springframework.stereotype.Controller;

Import org.springframework.web.bind.annotation.RequestMapping;

/**

* Test

* @author Angel (qq:412887952)

* @version v.0.1

*/

@Controller

public class Hellocontroller {

Read the configuration from application.properties, such as the default value of Hello Shanhy

@Value ("${application.hello:hello Angel}")

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";

}

}

(5) 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>

(6) Writing startup classes

To write the App.java startup class:

Package com.kfit.jsp;

Import org.springframework.boot.SpringApplication;

Import org.springframework.boot.autoconfigure.SpringBootApplication;

Import Org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication

public class App extends Springbootservletinitializer {

@Override

Protected Springapplicationbuilder Configure (Springapplicationbuilder application) {

Return application.sources (App.class);

//    }

public static void Main (string[] args) {

Springapplication.run (App.class, args);

}

}

Right-click Run as Java application access: HTTP://127.0.0.1:8080/HELLOJSP can access:

hellojsp

Hello Angel from Application

Spring Boot Add JSP supports "Go"

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.