加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用webjars的Thymeleaf Bootstrap的Spring MVC

发布时间:2020-12-18 00:24:33 所属栏目:安全 来源:网络整理
导读:我有一个 Spring项目,我将以下webjars包含在pom.xml中: dependency groupIdorg.webjars/groupId artifactIdbootstrap/artifactId version3.3.7-1/version/dependencydependency groupIdorg.webjars/groupId artifactIdjquery/artifactId version3.1.1/versi
我有一个 Spring项目,我将以下webjars包含在pom.xml中:
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>

然后我在HTML视图中包含以下链接和脚本:

<link rel="stylesheet" href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

<script src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>

但它没有用,没有找到映射:

[org.springframework.web.servlet.PageNotFound] (default task-15) No mapping found for HTTP request with URI [/TestPublicWeb-0.0.1-SNAPSHOT/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css] in DispatcherServlet with name 'testapp'

…所以我尝试将以下映射包含到servlet.xml中:

<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

但是有了这个,找不到我/ TestApplication的映射:

[org.springframework.web.servlet.PageNotFound] (default task-13) No mapping found for HTTP request with URI [/TestApplication/] in DispatcherServlet with name 'testapp'

webjars应该如何以正确的方式包含在Spring项目中?

解决方法

问题是您将标准HTML href标记与Thymeleaf的语法@ {}混合.更改如下:
<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

<script th:src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>

如果您使用的是Spring Security,还需要在configure(HttpSecurity http)方法中指定对webjars的授权,例如:

http.authorizeRequests().antMatchers("/webjars/**").permitAll();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读