twitter-bootstrap Spring MVC with Thymeleaf + Bootstrap 使用 webjars

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41548633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:30:28  来源:igfitidea点击:

Spring MVC with Thymeleaf + Bootstrap using webjars

javajquerytwitter-bootstrapspring-mvcthymeleaf

提问by user1563721

I have a Spring project where I included the following webjars into pom.xml:

我有一个 Spring 项目,其中包含以下 webjar 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>

Then I included in my HTML view the following link and scripts:

然后我在 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>

But it didn't work, no mapping found:

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

[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'

...so I tried to include the following mapping into servlet.xml:

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

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

But with this, mapping for my /TestApplicationis not found:

但是这样,/TestApplication找不到我的映射:

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

How should webjars be included in a Spring project in a correct way?

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

回答by DimaSan

The problem is that you are mixing the standard HTML hreftag with Thymeleaf's syntax @{}. Change it as follows:

问题是您将标准 HTMLhref标记与 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>

And if you are using Spring Security you need also specify the authorization to your webjars in the configure(HttpSecurity http)method like:

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

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

回答by coletrain

A couple things I could think of as to why the above is not working for you. Make sure you are using spring mvc namespace inside of your servlet.xml file. Looks something like:

关于为什么上述方法对您不起作用,我可以想到几件事。确保在 servlet.xml 文件中使用 spring mvc 命名空间。看起来像:

<bean xmlns:mvc="http://www.springframework.org/schema/mvc"....

Also not sure why you are trying to access assets using @{..}when you have specified the classpath. Try removing the @{..}like so:

也不确定为什么@{..}在指定类路径时尝试使用访问资产。尝试删除@{..}像这样:

<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>