SpringBoot配置外部Tomcat並支援JSP
1、使用idea建立springboot應用,指定打包方式為war,選擇web依賴
2、將pom.xml中spring-boot-starter-tomcat的scope屬性設定為provided並新增jstl等依賴,此時pom.xml如下:
<?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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hyc</groupId>
<artifactId>hello-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hello-web</name>
<description>Spring Boot使用外部tomcat並支援JSP</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 必須 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</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>
3、專案建立完成後會自動生成HelloWebApplication和ServletInitializer兩個類,我們不用修改這兩個類
4、工具沒有自動幫我們生成webapp資料夾和web.xml需要我們手工生成,有兩中辦法,一是手動建立資料夾和檔案,二是利用idea建立,我這裡使用第二種方式,需要指定webapp資料夾和web.xml檔案的路徑,如圖:
5、編寫jsp和controller,JSP檔案放在webapp/WEB-INF/jsp下
// HelloController.java
package com.hyc.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.Map;
@Controller
public class HelloController {
@Value("${application.message:Hello World}")
private String message = "Hello World";
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
@GetMapping("/welcome")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
<!-- welcome.jsp -->
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<body>
<c:url value="/resources/text.txt" var="url"/>
<spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />
Spring URL: ${springUrl} at ${time}
<br>
JSTL URL: ${url}
<br>
Message: ${message}
</body>
</html>
6、配置application.properties
server.port=8080
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello HYC
7、專案結構如圖:
8、配置tomcat8
9、測試,訪問http://localhost:8080/welcome,結果如下:
相關文章
- SpringBoot配置外部Tomcat並打war包Spring BootTomcat
- Springboot整合外部Tomcat配置Spring BootTomcat
- SpringBoot中配置外部TomcatSpring BootTomcat
- SpringBoot 之配置外部TomcatSpring BootTomcat
- 配置springboot專案使用外部tomcatSpring BootTomcat
- springboot使用外部tomcat啟動,整合jsp,另有整合dubbo樣例Spring BootTomcatJS
- 使用springboot建立web專案,並使用外部TomcatSpring BootWebTomcat
- SpringBoot使用外部tomcatSpring BootTomcat
- Springboot2專案配置(熱部署+war+外部tomcat+外部配置檔案)Spring Boot熱部署Tomcat
- springboot 專案部署到外部tomcatSpring BootTomcat
- SpringBoot外部化配置Spring Boot
- SpringBoot配置外部Tomcat專案啟動流程原始碼分析(長文)Spring BootTomcat原始碼
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- Tomcat配置jsp亂碼問題TomcatJS
- Spring Boot 支援 JSP配置Spring BootJS
- SpringBoot 使用外部Tomcat方法及啟動原理Spring BootTomcat
- SpringBoot外部化配置使用Plus版Spring Boot
- SpringBoot的外部化配置最全解析!Spring Boot
- springboot2.0使用外部tomcat進行啟動方法Spring BootTomcat
- Springboot內建tomcat配置HTTPS證書Spring BootTomcatHTTP
- SpringBoot部署到外部Tomcat無法註冊到Nacos服務端Spring BootTomcat服務端
- 關於SpringBoot的外部化配置使用記錄Spring Boot
- SpringBoot2.x系列教程(三十六)SpringBoot之Tomcat配置Spring BootTomcat
- tomcat+jsp訪問mysqlTomcatJSMySql
- springboot 執行 jar 包讀取外部配置檔案Spring BootJAR
- 精盡Spring Boot原始碼分析 - 支援外部 Tomcat 容器的實現Spring Boot原始碼Tomcat
- SpringBoot配置Profile多環境支援Spring Boot
- SpringBoot使用IDEA設定的外部Tomcat啟動,遇到的問題和解決Spring BootIdeaTomcat
- windows下安裝jdk+tomcat+maven並配置WindowsJDKTomcatMaven
- SpringBoot外接TomcatSpring BootTomcat
- Tomcat 配置支援不同的域名訪問各自不同程式的配置方法Tomcat
- IDEA中Maven專案修改JSP後透過配置Tomcat實現立即生效IdeaMavenJSTomcat
- 你絕對不知道的 SpringBoot 的外部化配置特性!Spring Boot
- SpringBoot中使用jsp的坑Spring BootJS
- Spring Boot專案部署到外部TomcatSpring BootTomcat
- 如何在SpringBoot/Tomcat的URL查詢字串中的支援括號?Spring BootTomcat字串
- tomcat配置Tomcat
- SpringBoot整合Jsp和Thymeleaf (附工程)Spring BootJS