2017-12-18 122 views
0

我使用「spring tool suite」創建了spring引導啓動項目。當我運行項目時,index.jsp頁面沒有加載。但index.html可以很好地加載。spring引導項目運行問題

我的文件夾結構如下

enter image description here

我家控制器

package com.programmingfree.springservice; 

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

@Controller 
public class HomeController { 

    @RequestMapping("/") 
    public String home() { 
     return "index"; 
    } 

} 

我如何運行index.jsp

+2

您是否檢出了有關使用JSP和可執行JAR的限制的文檔? https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations。還有一個示例可用:https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp – dunni

回答

0

您使用的春天啓動的默認配置,看看在ThymeleafProperties.java.html是後綴的默認設置:

@ConfigurationProperties(
    prefix = "spring.thymeleaf" 
) 
public class ThymeleafProperties { 
    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); 
    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); 
    public static final String DEFAULT_PREFIX = "classpath:/templates/"; 
    public static final String DEFAULT_SUFFIX = ".html"; 
    private boolean checkTemplate = true; 
    private boolean checkTemplateLocation = true; 
    private String prefix = "classpath:/templates/"; 
    private String suffix = ".html"; 
    private String mode = "HTML5"; 
    //...... 
} 

所以,你必須自定義配置在application.properties:

spring.thymeleaf.prefix=classpath:/templates/ 
spring.thymeleaf.suffix=.jsp 
+0

一個JSP isn這是一種Thymeleaf模板,因此無法工作。 –

+0

親愛的戴夫,我可以在這個項目中使用Angular的html前端。 – user1177842

+0

似乎我誤解了thymeleaf,謝謝。 @ M. Deinum –

0

你有application.properties下一行?

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

資源鏈接在模板運行時被重寫,得益於ResourceUrlEncodingFilter,自動設定Thymeleaf和FreeMarker的。使用JSP時,應該手動聲明此過濾器。 spring doc