通过webjar将Bootstrap附加到Spring MVC项目

时间:2015-12-25 13:43:55

标签: java spring twitter-bootstrap spring-mvc

我正在尝试将Bootstrap框架添加到我的Spring MVC项目中。我可以看到我的页面,但没有Bootstrap。应用程序在Spring 4的Tomcat 8上运行。

在浏览器控制台中,我看到错误

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8088/webjars/bootstrap/3.2.0/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)

这是我的代码,Spring Configuration:

@Configuration
@ComponentScan("org.dummy")
@EnableWebMvc  
public class SpringConfig extends WebMvcConfigurerAdapter {  

@Bean  
public UrlBasedViewResolver setupViewResolver() {  
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
    resolver.setPrefix("/WEB-INF/jsp/");  
    resolver.setSuffix(".jsp");  
    resolver.setViewClass(JstlView.class);  
    return resolver;  
}  

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

包含的依赖项:

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.2.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>2.1.1</version>
    </dependency>

示例index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="cp" value="${pageContext.request.servletContext.contextPath}" scope="request" />

<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel='stylesheet' href='webjars/bootstrap/3.2.0/css/bootstrap.min.css'>
</head>
<body>  
    <div class="row">     
        <div class="col-md-4"> 
            <h3> 
                Column1</br>
            </h3>
            <ul>
                <li> Row 1 </li>
                <li> Row 2 </li>
                <li> Row 3 </li>        
            </ul>                                
        </div> 
        <div class="col-md-8"> 
            <h3> 
                Column 2</br>
            </h3>    
            <ol>
                <li> Row 1 </li>
                <li> Row 2 </li>
                <li> Row 3 </li>                             
            </ol> 
        </div> 
    </div>
    <script type="text/javascript" src="webjars/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="webjars/jquery/2.1.1/jquery.min.js"></script>
</body>

这是我的目录截图: Directories

1 个答案:

答案 0 :(得分:1)

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

您还需要默认的servlet处理程序

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

这与提供静态资源所需的基于xml的配置中的<mvc:resources/><mvc:default-servlet-handler/>相同。

也可以尝试

@Override
@Bean
public HandlerMapping resourceHandlerMapping() {
    AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
    handlerMapping.setOrder(-1);
    return handlerMapping;
}