新冠病毒??還在阻擋全世界重啟,但我們學(xué)習(xí)腳步不不能停滯澜掩,接下來給大家展示一個現(xiàn)在開發(fā)中已經(jīng)不太常用的一個小知識點妈经,希望對大家有所啟發(fā)胞得。
在平時 大家可能用 Spring Boot 2 最多就是開發(fā) RESTful API,可能很少有人在 Spring Boot 2 中用過JSP視圖稼跳,那我就來一起體驗下創(chuàng)建一個用 JSP 視圖的 Spring Boot 2 應(yīng)用有多么方便盟庞。
一起來看看我們需要些什么
項目結(jié)構(gòu)
咱們可以從 Spring Initializer 獲取項目框架。
項目依賴
<u>pom.xml</u>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eprogrammerz.examples.spring</groupId>
<artifactId>spring-boot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jsp</name>
<description>Example Spring Boot with JSP view</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置
- 啟動類配置
SpringBootServletInitializer
按傳統(tǒng)的 WAR包 部署方式來運行 SpringBootJspApplication
汤善。
<u>SpringBootJspApplication.java</u>
package com.eprogrammerz.examples.spring.springbootjsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootJspApplication.class, args);
}
}
- Resources
<u>application.properties</u>
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Controller and View Template
- 寫一個簡單映射方法的Controller
package com.eprogrammerz.examples.spring.springbootjsp.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping({"/", "/hello"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
}
- 將下面內(nèi)容保存成 JSP 文件什猖,放在
src/main/webapp/WEB-INF/jsp/
目錄
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello ${name}!</title>
</head>
<body>
<h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>
使用Maven運行
在項目根路徑下使用命令行運行程序。
mvn clean spring-boot:run
訪問 localhost:8080 測試你的程序效果红淡。