2015-10-16 30 views
1

我按照他們的網站上的文檔中描述here webjars版本無關如何使Spring MVC中

首先,我添加了必需的路徑

<mvc:resources mapping="/webjars/**" location="/webjars/"/>

然後我創建了一個控制器以下

@ResponseBody 
@RequestMapping("/webjarslocator/{webjar}/**") 
public ResponseEntity locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) { 
    try { 
     String mvcPrefix = "/webjarslocator/" + webjar + "/"; // This prefix must match the mapping path! 
     String mvcPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 
     String fullPath = assetLocator.getFullPath(webjar, mvcPath.substring(mvcPrefix.length())); 
     return new ResponseEntity(new ClassPathResource(fullPath), HttpStatus.OK); 
    } catch (Exception e) { 
     return new ResponseEntity<>(HttpStatus.NOT_FOUND); 
    } 
} 

很少有依賴關係丟失,所以我在maven中添加了以下pom

<dependency> 
    <groupId>org.webjars</groupId> 
    <artifactId>webjars-locator</artifactId> 
    <version>0.28</version> 
</dependency> 

上面將導入以下

import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.servlet.HandlerMapping; 
import org.springframework.core.io.ClassPathResource; 

無這些已經從外部罐導入。

的錯誤是:assetLocator cannot be resolved

編輯:這可能是因爲我需要創建一個過濾器,而不是把它放在一個控制器。對此有何想法?

+0

你在哪裏定義了'assetLocator'? – zeroflagL

+0

@zeroflagL無處,我的問題是關於如何定義一個 – QGA

+0

'新的WebJarAssetLocator()'... – zeroflagL

回答

1

documentation相當稀少,但您可以創建一個資產定位器的實例,其中包含new WebJarAssetLocator()

+0

非常感謝.. – QGA

+0

我很樂意提高這些文檔的請求:https://github.com/webjars/webjars/blob/master/app/views/documentation.scala.html#L341-L431 –