1. 程式人生 > >spring boot 使用jsp 的一些問題

spring boot 使用jsp 的一些問題

最近做了一個小的專案,使用了spring boot 搭建的框架,頁面使用的是jsp ,雖然官方不推薦,但是畢竟是最熟悉的.

下面就說說在使用jsp的時候出現的一些問題.

1.jstl標籤不支援

   新增如下的依賴

<!-- jsp 支援 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

		<!-- jsp 支援 -->

		<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-taglibs</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

2.spring boot 實現 tomcat 虛擬目錄對映

@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter {
	@Value("${audioPath}")
	private String audioPath;
// 實現虛擬目錄
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/audioPath/**").addResourceLocations(
				"file:" + audioPath);
		super.addResourceHandlers(registry);
	}


@Value("${audioPath}")  是從配置檔案中注入的我的檔案路徑 ,假設我的路徑是D:\data\   當我訪問 /audioPath/test.png 時,就會對映到D:\data\目錄下的test.png.

3.spring boot 打包 jar 檔案之後,jsp 不能訪問.

   使用maven 的package 命令生成了jar檔案之後,可以正常啟動但不可以訪問jsp頁面.去查閱了一下資料,好多人說不能使用jsp,但是官方給出的說法是不建議而已.終於在一個博主那裡看到了解決的辦法.  

http://blog.csdn.net/qq_34665539/article/details/74783910 主要的解決辦法就是配置打包路徑,將jsp 檔案打到resources 下,下面給出配置的資訊

<build>
<resources>
			<!-- 打包時將jsp檔案拷貝到META-INF目錄下 -->
			<resource>
				<!-- 指定resources外掛處理哪個目錄下的資原始檔 -->
				<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>
				<filtering>false</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- 這個版本很重要,如果你使用的是1.5x 的版本,可能仍然不能訪問.這個需要額外配置 -->
				<version>1.4.2.RELEASE</version>
			</plugin>
		</plugins>
</build>


相關推薦

no