springboot的jsp应该放在哪_web项目jsp放在哪里 Spring Boot 静态资源处理(4)

Spring Boot 默认将 /webjars/** 映射到 classpath:/META-INF/resources/webjars/ ,结合我们上面讲到的访问资源的规则,便可以得知我们在JSP页面中引入jquery.js的方法为:

想实现这样,我们只需要在pom.xml 文件中添加jquery的webjars 依赖即可,如下:

org.webjars

jquery

2.1.4

但是我们实际开发中,可能会遇到升级版本号的情况,如果我们有100多个页面,几乎每个页面上都有按上面引入jquery.js 那么我们要把版本号更换为3.0.0,一个一个替换显然不是最好的办法。

如何来解决?按如下方法处理即可。

首先在pom.xml 中添加依赖:

org.webjars

webjars-locator

然后增加一个WebJarsController:

package org.springboot.sample.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.io.ClassPathResource;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.HandlerMapping;

import org.webjars.WebJarAssetLocator;

/**

* 处理WebJars,自动读取版本号

*

* @author 单红宇(365384722)

* @myblog https://blog.csdn.net/catoop/

* @create 2016年1月8日

*/

@Controller

public class WebJarsController {

private final WebJarAssetLocator assetLocator = new WebJarAssetLocator();

@ResponseBody

@RequestMapping("/webjarslocator/{webjar}/**")

public ResponseEntitylocateWebjarAsset(@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);

}

}

}

本文来自电脑杂谈,转载请注明本文网址:

http://www.pc-fly.com/a/jisuanjixue/article-62262-4.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值