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建立web專案,並使用外部TomcatSpring BootWebTomcat
- SpringBoot使用外部tomcatSpring BootTomcat
- Springboot2專案配置(熱部署+war+外部tomcat+外部配置檔案)Spring Boot熱部署Tomcat
- 知識點①:springboot使用外部 tomcat 執行配置Spring BootTomcat
- springboot使用外部tomcat啟動,整合jsp,另有整合dubbo樣例Spring BootTomcatJS
- SpringBoot外部化配置Spring Boot
- SpringBoot配置外部Tomcat專案啟動流程原始碼分析(長文)Spring BootTomcat原始碼
- springboot 專案部署到外部tomcatSpring BootTomcat
- Springboot專案部署到外部TomcatSpring BootTomcat
- 配置Eclpise+tomcat並實現JSP的編寫與部署TomcatJS
- Tomcat/JSP中文編碼配置TomcatJS
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- spring boot(7) 配置外部tomcat(1)Spring BootTomcat
- solaris10-x86下快速配置apache與tomcat整合支援jspApacheTomcatJS
- Spring Boot 支援 JSP配置Spring BootJS
- SpringBoot 使用外部Tomcat方法及啟動原理Spring BootTomcat
- Tomcat配置jsp亂碼問題TomcatJS
- jsp+tomcat+ 建立project 配置projectJSTomcatProject
- Tomcat JSP經典配置例項TomcatJS
- SpringBoot外部化配置使用Plus版Spring Boot
- SpringBoot的外部化配置最全解析!Spring Boot
- solaris10-x86下快速配置apache與tomcat整合支援jsp(轉)ApacheTomcatJS
- springboot2.0使用外部tomcat進行啟動方法Spring BootTomcat
- Linux下jsp環境:apache,tomcat配置LinuxJSApacheTomcat
- 配置Eclpise+tomcat與實現JSP部署TomcatJS
- Tomcat下JSP、Servlet和JavaBean環境的配置TomcatJSServletJavaBean
- windows系統下jsp+mysql+tomcat的配置WindowsJSMySqlTomcat
- springboot tomcat的配置選項大全Spring BootTomcat
- 關於SpringBoot的外部化配置使用記錄Spring Boot
- springboot學習筆記:6.內建tomcat啟動和外部tomcat部署總結Spring Boot筆記Tomcat
- tomcat支援https的server.xml配置TomcatHTTPServerXML
- SpringBoot配置Profile多環境支援Spring Boot
- springboot 執行 jar 包讀取外部配置檔案Spring BootJAR