2015-10-16 53 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 –