[原創]Spring boot 框架構建jsp web應用


說明

Spring boot支持將web項目打包成一個可執行的jar包,內嵌tomcat服務器,獨立部署

為支持jsp,則必須將項目打包為war包

pom.xml中設置打包方式

<packaging>war</packaging>

依賴包導入

Srping boot web項目原本會包含依賴項(starter-web模塊內部依賴包含了spring-boot-starter-tomcat模塊)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

增加如下依賴

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>provided</scope>
</dependency>

注意:若使用intellij作為集成開發環境,則scope需要設置為compile <scope>compile</scope>

參考

I can confirm that the IntelliJ error happens for me when I try to run a @SpringBootApplication class directly (without using maven/gradle). It happens in both versions on IntelliJ.

It is fixed temporarily if you follow the instructions @xilin, but these changes are overridden anytime the Gradle project is reimported (and it's annoying to have to tell new devs to do this).

建立jsp文件目錄

在main目錄下與resources同級別,建立目錄webapp/WEB-INF/jsp/,所有jsp文件將放置與此目錄中
配置ViewResolver,指定jsp目錄

	/**
	 * Created by Ant on 2015/4/11.
	 * QQ:517377100
	 */
	@Configuration
	public class JspConfiguration {
	    @Bean
	    InternalResourceViewResolver internalResourceViewResolver () {
	        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
	        viewResolver.setPrefix("/WEB-INF/jsp/");
	        viewResolver.setSuffix(".jsp");
	        return viewResolver;
	    }
	}

資源目錄

資源目錄直接位於webapp目錄下,如圖片目錄為 webapp/imgs/

假設目錄結構如下,welcome.jsp中以 <img src="imgs/spring.jpg" /> 引用圖片資源

webapp/
	imgs/
		spring.jpg
	WEB-INF/
		jsp/
			welcome.jsp

啟動服務

通過 mvn package 完成編譯打包,target目錄中將生成可執行的xx.war文件

通過 java -jar xx.war 啟動服務

鏈接

blog/Spring boot 框架構建jsp web應用.md


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM