中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

SpringBoot2.x支持jsp使用jar方式部署嗎

這篇文章主要介紹“SpringBoot 2.x支持jsp使用jar方式部署嗎”,在日常操作中,相信很多人在SpringBoot 2.x支持jsp使用jar方式部署嗎問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SpringBoot 2.x支持jsp使用jar方式部署嗎”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

成都創新互聯公司專注于新北企業網站建設,成都響應式網站建設,商城網站制作。新北網站建設公司,為新北等地區提供建站服務。全流程定制制作,專業設計,全程項目跟蹤,成都創新互聯公司專業和態度為您提供的服務

一 pom.xml文件修改

1 增加tomcat jsp 依賴

        <!-- 添加servlet依賴模塊 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--jsp依賴jar-->
		<dependency>
		   <groupId>javax.servlet.jsp</groupId>
		   <artifactId>javax.servlet.jsp-api</artifactId>
		   <version>2.3.1</version>
		</dependency>

        <!-- 添加jstl標簽庫依賴模塊 -->
        <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>
        <!-- 使用jsp引擎,springboot內置tomcat沒有此依賴 -->
       
        <dependency>
		    <groupId>org.apache.tomcat.embed</groupId>
		    <artifactId>tomcat-embed-jasper</artifactId>
		    <version>9.0.27</version>
		    <scope>provided</scope>
		</dependency>

2 修改資源打包配置

       <resources>
            <resource>
	            <!--打包成jar靜態資源必須指明路徑,打包成war可以不用-->
	            <directory>src/main/webapp</directory>
	            <targetPath>META-INF/resources</targetPath>
	            <includes>
	                <include>**/**</include>
	            </includes>
	        </resource>
	        <resource>
	            <directory>src/main/resources</directory>
	            <includes>
	                <include>**/**</include>
	            </includes>
	        </resource>
	        
        </resources>

二 項目目錄

增加目錄 src/main/webapp,目錄中可加入jsp相關文件

application.properties中加入

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

#支持jsp debugger

server.servlet.jsp.init-parameters.development=true
 

SpringBoot 2.x支持jsp使用jar方式部署嗎

三 修改代碼

增加轉換配置

StaticResourceConfigurer.java

package com.vipkid.configuration;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.WebResourceRoot.ResourceSetType;
import org.springframework.util.ResourceUtils;

/**
 * 資源路徑轉換配置
 * Add main class fat jar/exploded directory into tomcat ResourceSet.
 * 
 * @author zouqinghua
 * @date 2019年10月30日  下午9:44:34
 *
 */
public class StaticResourceConfigurer implements LifecycleListener {

	private final Context context;

	StaticResourceConfigurer(Context context) {
		this.context = context;
	}

	@Override
	public void lifecycleEvent(LifecycleEvent event) {
		if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
			URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();

			if (ResourceUtils.isFileURL(location)) {
				// when run as exploded directory
				String rootFile = location.getFile();
				if (rootFile.endsWith("/BOOT-INF/classes/")) {
					rootFile = rootFile.substring(0, rootFile.length() - "/BOOT-INF/classes/".length() + 1);
				}
				if (!new File(rootFile, "META-INF" + File.separator + "resources").isDirectory()) {
					return;
				}

				try {
					location = new File(rootFile).toURI().toURL();
				} catch (MalformedURLException e) {
					throw new IllegalStateException("Can not add tomcat resources", e);
				}
			}

			String locationStr = location.toString();
			if (locationStr.endsWith("/BOOT-INF/classes!/")) {
				// when run as fat jar
				locationStr = locationStr.substring(0, locationStr.length() - "/BOOT-INF/classes!/".length() + 1);
				try {
					location = new URL(locationStr);
				} catch (MalformedURLException e) {
					throw new IllegalStateException("Can not add tomcat resources", e);
				}
			}
			this.context.getResources().createWebResourceSet(ResourceSetType.RESOURCE_JAR, "/", location,
					"/META-INF/resources");

		}
	}
}

增加監聽類 TomcatConfiguration.jsp

package com.vipkid.configuration;

import org.apache.catalina.Context;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * 配置 addLifecycleListener
 * @author zouqinghua
 * @date 2019年10月30日  下午9:45:28
 *
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnProperty(name = "tomcat.staticResourceCustomizer.enabled", matchIfMissing = true)
public class TomcatConfiguration {
	/*
	 * 
	 * SpringBoot 1.x方式配置
	  @return
	@Bean
	public EmbeddedServletContainerCustomizer staticResourceCustomizer() {
		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {
				if (container instanceof TomcatEmbeddedServletContainerFactory) {
					((TomcatEmbeddedServletContainerFactory) container)
							.addContextCustomizers(new TomcatContextCustomizer() {
								@Override
								public void customize(Context context) {
									context.addLifecycleListener(new StaticResourceConfigurer(context));
								}
							});
				}
			}

		};
	}
	*/
	
	/**
	 * SpringBoot 2.x方式配置
	 * @return
	 */
	@Bean
	public WebServerFactoryCustomizer webServerFactoryCustomizerBean(){
		
		return new WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory>() {

			@Override
			public void customize(ConfigurableTomcatWebServerFactory factory) {
				factory.addContextCustomizers(new TomcatContextCustomizer() {
								@Override
								public void customize(Context context) {
									context.addLifecycleListener(new StaticResourceConfigurer(context));
								}
							});
			}
			
		};
		
		
		
		
	}
}

四 代碼

IndexController和

src/main/webapp/WEB-INF/views/hello.jsp

package com.smc.sys.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

	@RequestMapping("/hello")
    public String index(){
		System.out.println("index ==== >>");
        return "hello";
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Pragma", "no-cache");
	response.setHeader("Expires", "0");
%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<c:set var="ctx" value="${pageContext['request'].contextPath}" />
<html>
<body>
	<h2>Hello World - ${name}</h2>
</body>
</html>

五 打包 啟動

mvn clean package -Dmaven.test.skip=true 

java -jar target/xxx.jar

六 訪問

http://localhost:8080/hello

到此,關于“SpringBoot 2.x支持jsp使用jar方式部署嗎”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注創新互聯網站,小編會繼續努力為大家帶來更多實用的文章!

新聞標題:SpringBoot2.x支持jsp使用jar方式部署嗎
文章轉載:http://m.2m8n56k.cn/article24/pecpce.html

成都網站建設公司_創新互聯,為您提供商城網站企業建站外貿建站品牌網站制作網站內鏈網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 久久成人免费观看全部免费 | 国产精品亚洲综合 | 国产精品国产 | 加勒比综合 | 亚洲国产欧美日韩 | 国产欧美日韩在线不卡第一页 | 日韩三级一区 | 一级作爱视频免费观看 | 在线精品免费观看综合 | 免费一级毛片在级播放 | 在线观看偷拍视频一区 | 亚洲国产一成人久久精品 | 91精品久久久久久久久网影视 | 国产精品久草 | 国产3区| 欧美性生交大片免费看 | 亚洲在线播放 | 欧美色成人 | 性生i活一级一片 | a级毛片毛片免费很很综合 a级毛片免费 | 久久厕所精品国产精品亚洲 | 日韩专区在线 | 男女扒开双腿猛进入爽爽视频 | 三级网站视频在线观看 | 黄www片 | 黄色免费在线观看视频 | 国产成在线观看免费视频成本人 | 久久国产精品女 | 女在床上被男的插爽叫视频 | 亚洲性色视频 | 91热久久免费频精品动漫99 | 成人毛片一区二区三区 | 一区二区三区国产美女在线播放 | 91大神大战丝袜美女在线观看 | 欧美一区二区在线观看免费网站 | 亚洲成人一区 | 成人区精品一区二区不卡亚洲 | 久久国产视频网站 | 一级无毛片 | 国产一区亚洲二区三区毛片 | 日韩精品无码一区二区三区 |