springboot整合jsp踩坑

來源:互聯網
上載者:User

標籤:dex   cee   thymeleaf   start   org   aging   configure   官網   project   

  springboot以其高效的開發效率越來越多的用在中小項目的開發,並且在分布式開發中的使用也很廣泛,springboot官方推薦的前端架構卻是thymeleaf,並且預設不支援jsp,而大部分java開發人員最熟悉的前端開發工具卻是jsp,自己在工作中就遇到這樣的情況,因此在網上找一些整合的demo,但是依然踩了很多坑,記錄下來以供學習。

  代碼及配置如下:

  1、pom.xml,這個是本人整合中遇到的最大的坑,主要是spring-boot-starter-parent的版本,自己先去官網查,官方文檔給的是2.0.3,然而試了下視圖解析器根本不起作用,然後又試了1.3.5和1.5.2,這兩個版本主要是SpringBootServletInitializer類的包位置變了,重新導一下包之後項目正常運行了,也能訪問到頁面,最後又試了下2.0.1版本,SpringBootServletInitializer類位置一樣,所有配置不變,項目就正常了,個人也不知道最新的springboot版本2.0.3為什麼不可以,所以推薦後續整合jsp的同學使用2.0.1版本。

<?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><groupId>com.example</groupId><artifactId>springboot-jsp</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>springboot-jsp</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><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-starter-tomcat</artifactId></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><!-- servlet支援 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><!-- jstl 支援 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></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>

  2、SpringbootJspApplication

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplicationpublic class SpringbootJspApplication extends SpringBootServletInitializer{@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringbootJspApplication.class);}public static void main(String[] args) {SpringApplication.run(SpringbootJspApplication.class, args);}}

  3、IndexController

package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class IndexController {@RequestMapping(value="/index",method=RequestMethod.GET)public String index() {return "index";}}

  4、application.properties

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

  5、在main目錄下建立webapp/WEB-INF/jsp目錄,添加index.jsp

<!DOCTYPE html><%@page contentType="text/html; charset=UTF-8" language="java"%><html lang="en"><head><title>title</title></head><body>this is index jsp</body></html>

  項目目錄結構如下,本人使用的是STS:

  在STS中啟動項目,訪問localhost:8080/index,效果如下:

 

springboot整合jsp踩坑

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.