【发布时间】:2015-10-16 09:46:59
【问题描述】:
我已经按照here 的描述关注了他们网站上的文档
首先我添加了所需的路径
<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;
这些都没有从外部 jar 导入。
错误是:assetLocator cannot be resolved
编辑:可能是我需要创建一个过滤器而不是将其放入控制器中。对此有什么想法吗?
【问题讨论】:
-
你在哪里定义
assetLocator? -
@zeroflagL 无处,我的问题是关于如何定义一个
-
new WebJarAssetLocator()... -
非常感谢我尝试了
new AssetLocator(),但没有解决 -
请回答这个问题,我会接受
标签: spring maven spring-mvc webjars