SpringMVC 引用BootStrap和jQuery

无意中在Maven公服中发现bootstrap经被打包到jar中了。

http://mvnrepository.com/artifact/org.webjars/bootstrap


添加依赖
<dependency>
    <groupId>org.webjarsgroupId>
    <artifactId>bootstrapartifactId>
    <version>3.3.5version>
dependency>

在 bootstrap-3.3.5.pom 中的依赖只有一个
<dependencies>
    <dependency>
        <groupId>org.webjarsgroupId>
        <artifactId>jqueryartifactId>
        <version>1.11.1version>
    dependency>
dependencies>

基于xml的SpringMVC配置

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

基于Class的配置


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/welcome").setViewName("welcome");
        registry.addViewController("/readme").setViewName("readme");
        registry.addViewController("/login-page").setViewName("login");
        registry.addViewController("/logout/success").setViewName("logout_success");
        //
        registry.addRedirectViewController("/", "/welcome");
    }

}

引用


<link rel="stylesheet" href="${ctx}webjars/bootstrap/3.3.5/css/bootstrap.min.css">


<link rel="stylesheet" href="${ctx}webjars/bootstrap/3.3.5/css/bootstrap-theme.min.css">


<script src="${ctx}webjars/jquery/1.11.1/jquery.min.js">script>


<script src="${ctx}webjars/bootstrap/3.3.5/js/bootstrap.min.js">script>

thymeleaf


<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}" />


<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap-theme.min.css}" />


<script type="text/javascript" th:src="@{/webjars/jquery/1.11.1/jquery.min.js}">script>


<script type="text/javascript" th:src="@{/webjars/bootstrap/3.3.5/js/bootstrap.min.js}">script>

你可能感兴趣的:(Spring)