1. 程式人生 > >(19)Spring Boot 新增JSP支援【從零開始學Spring Boot】

(19)Spring Boot 新增JSP支援【從零開始學Spring Boot】

【來也匆匆,去也匆匆,在此留下您的腳印吧,轉發點贊評論;

      您的認可是我最大的動力,感謝您的支援】

看完本文章您可能會有些疑問,可以檢視之後的一篇部落格:

-------------------------------------------------------------------------------------------------------------------------------------

這個部分比較複雜,所以單獨建立一個工程來進行講解;

      大體步驟:

(1)            建立Maven web project

(2)            

pom.xml檔案新增依賴;

(3)            配置application.properties支援jsp

(4)            編寫測試Controller

(5)          編寫JSP頁面

(6)          編寫啟動類App.java

 

1,FreeMarker
2
,Groovy
3
,Thymeleaf (Spring 官網使用這個)
4
,Velocity
5
,JSP (貌似Spring Boot官方不推薦,STS建立的專案會在src/main/resources 下有個templates 目錄,這裡就是讓我們放模版檔案的,然後並沒有生成諸如 SpringMVC 中的webapp目錄)

不過本文還是選擇大家都熟悉的JSP來舉例,因為使用JSP與預設支援的模版需要特殊處理,所以拿來舉例更好。

1)建立Maven web project

使用Eclipse新建一個Maven Web Project ,專案取名為:

spring-boot-jsp

2)在pom.xml檔案新增依賴

<!-- spring boot parent節點,引入這個之後,在下面和spring boot相關的就不需要引入版本了; -->

    <parent>

       <groupId>org.springframework.boot</groupId

>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>1.3.3.RELEASE</version>

    </parent>

依賴包:

<!-- web支援: 1web mvc; 2restful; 3jackjson支援; 4aop ........ -->

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-web</artifactId>

       </dependency>

       <!-- servlet 依賴. -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>javax.servlet-api</artifactId>

           <scope>provided</scope>

       </dependency>

       <!--

           JSTLJSP Standard Tag LibraryJSP標準標籤庫)是一個不斷完善的開放原始碼的JSP標籤庫,是由apachejakarta小組來維護的。JSTL只能執行在支援JSP1.2Servlet2.3規範的容器上,如tomcat 4.x。在JSP 2.0中也是作為標準支援的。

不然報異常資訊:

           javax.servlet.ServletException: Circular view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

        -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>jstl</artifactId>

       </dependency>

       <!-- tomcat 的支援.-->

       <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編譯版本:

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

3application.properties配置

上面說了spring-boot 不推薦JSP,想使用JSP需要配置application.properties新增src/main/resources/application.properties內容:

頁面預設字首目錄

spring.mvc.view.prefix=/WEB-INF/jsp/

響應頁面預設字尾

spring.mvc.view.suffix=.jsp

自定義屬性,可以在Controller中讀取

application.hello=Hello Angel From application

4)編寫測試Controller

編寫類: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;

/**

 * 測試

 * @author Angel(QQ:412887952)

 * @version v.0.1

 */

@Controller

public class HelloController {

       //  application.properties 中讀取配置,如取不到預設值為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)編寫JSP頁面

 src/main 下面建立 webapp/WEB-INF/jsp 目錄用來存放我們的jsp頁面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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    helloJsp

    <hr>

    ${hello}

</body>

</html>

(6)編寫啟動類

編寫App.java啟動類:

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

       }

}

helloJsp

Hello Angel From application

特別說明:針對el表示式,類似${hello}這個對於servlet的版本是有限制的,2.4版本版本以下是不支援的,是無法進行識別的,請注意。