我按照他們的網站上的文檔中描述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
編輯:這可能是因爲我需要創建一個過濾器,而不是把它放在一個控制器。對此有何想法?
你在哪裏定義了'assetLocator'? – zeroflagL
@zeroflagL無處,我的問題是關於如何定義一個 – QGA
'新的WebJarAssetLocator()'... – zeroflagL