Spring MVC with Thymeleaf + Bootstrap using webjars

I have a spring project where I have included the following webjars in 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 the following link and scripts in my html view:

    <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 this did not work, the mapping was not 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 display in the servlet.xml file:

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

But at the same time, the mapping for mine was /TestApplicationnot found:

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

What should be the correct websites included in the sprinf project?

+4
source share
2 answers

, 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, webjars configure(HttpSecurity http), :

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

, , . , spring mvc servlet.xml. :

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

, @{..}, . @{..} :

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

Source: https://habr.com/ru/post/1666246/


All Articles