2015-11-25 142 views
14

我試圖用一些HTML5和JavaScript運行Spring啓動應用程序,並且出現問題。Spring Boot添加HTML和JavaScript

我有項目結構是這樣的:

enter image description here

我的Spring MVC控制器是調用文件offer.html和工程確定。

我offer.html文件是這樣的:

<!DOCTYPE html> 
<html lang="en" ng-app="coByTu"> 
<head> 
    <title>Page</title> 
    <script type="text/javascript" src="../js/lib/angular.js" /> 
    <script type="text/javascript" src="../js/src/offer.js" /> 
</head> 
<body> 

</body> 
</html> 

,當我輸入我的應用程序URL http://localhost:8080/offerView

從服務器的響應是:

enter image description here

我有不知道爲什麼我的應用程序沒有看到這個腳本文件,任何人都知道我做錯了什麼?

+0

請參閱https://github.com/spring-guides/tut-spring-security-and-angular-js/tree/master/modular – EpicPandaForce

回答

27

基本上所有需要靜態提供的內容(如javascript文件)應放置在靜態文件夾下。 https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

我已經一起引發一個快速的工作示例展示它是如何做: https://github.com/ericbv/staticContentWithSpringBoot

文件結構: enter image description here

HTML文件:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Page</title> 
    <script type="text/javascript" th:src="@{/js/lib/angular.js}" /> 
    <script type="text/javascript" th:src="@{/js/src/offer.js}" /> 
</head> 
<body> 

使用日:SRC將確保鏈接瞭解背景

編輯: 加入日:SRC使引用情境感知

+0

我試圖在這裏不使用thymeleaf :)'

1

我認爲你需要將js目錄(和內容)移動到靜態目錄中(而不是在模板中)。

+0

我曾嘗試過,但我沒有解決我的問題。 – zaqpiotr

0

設置將通過網絡上下文中使用的文檔根目錄使用ConfigurableEmbeddedServletContainer.setDocumentRoot(File documentRoot)提供靜態文件。

工作例如:

package com.example.config; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; 
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; 
import org.springframework.boot.web.servlet.ServletContextInitializer; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.core.env.Environment; 
import org.springframework.util.StringUtils; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import java.io.File; 
import java.nio.file.Paths; 

@Configuration 
public class WebConfigurer implements ServletContextInitializer, EmbeddedServletContainerCustomizer { 
    private final Logger log = LoggerFactory.getLogger(WebConfigurer.class); 

    private final Environment env; 
    private static final String STATIC_ASSETS_FOLDER_PARAM = "static-assets-folder"; 
    private final String staticAssetsFolderPath; 

    public WebConfigurer(Environment env, @Value("${" + STATIC_ASSETS_FOLDER_PARAM + ":}") String staticAssetsFolderPath) { 
     this.env = env; 
     this.staticAssetsFolderPath = staticAssetsFolderPath; 
    } 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     if (env.getActiveProfiles().length > 0) { 
      log.info("Web application configuration, profiles: {}", (Object[]) env.getActiveProfiles()); 
     } 
     log.info(STATIC_ASSETS_FOLDER_PARAM + ": '{}'", staticAssetsFolderPath); 
    } 

    private void customizeDocumentRoot(ConfigurableEmbeddedServletContainer container) { 
     if (!StringUtils.isEmpty(staticAssetsFolderPath)) { 
      File docRoot; 
      if (staticAssetsFolderPath.startsWith(File.separator)) { 
       docRoot = new File(staticAssetsFolderPath); 
      } else { 
       final String workPath = Paths.get(".").toUri().normalize().getPath(); 
       docRoot = new File(workPath + staticAssetsFolderPath); 
      } 
      if (docRoot.exists() && docRoot.isDirectory()) { 
       log.info("Custom location is used for static assets, document root folder: {}", 
         docRoot.getAbsolutePath()); 
       container.setDocumentRoot(docRoot); 
      } else { 
       log.warn("Custom document root folder {} doesn't exist, custom location for static assets was not used.", 
         docRoot.getAbsolutePath()); 
      } 
     } 
    } 

    @Override 
    public void customize(ConfigurableEmbeddedServletContainer container) { 
     customizeDocumentRoot(container); 
    } 

} 

現在你可以用command line或配置文件自定義您的應用程序中(src /主/資源/應用myprofile.yml:static-assets-folder: myfolder):

> java -jar demo-0.0.1-SNAPSHOT.jar --static-assets-folder="myfolder" 
> java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=myprofile 
0

春季啓動初學者,我有類似的問題。我的jQuery代碼在我的html文檔(一個thymeleaf模板)底部的< script>標記內工作正常,但是當我將完全相同的代碼放入static/js文件夾中的外部.js文檔時,它不再響應。超級簡單的修復 - 只需要把所有的。js doc代碼在這裏面: $(document).ready(function(){... code ...}); 然後它工作正常。希望這可以幫助某人。

+0

我不認爲這是一個適當的修復。您是否使用Chrome開發人員的工具調試過這個問題? –