我的Spring MVC应用程序包含一堆* .css,*。js和* .png文件,目前放在 src / main / java / resources / 中目录。 我阅读了Spring Docs和一些关于如何使用ResourceHandlerRegistry类为我的模板加载这些文件的教程,但它并不适用于我。
我的WebConfig(扩展了WebMvcConfigurerAdapter):
package by.vk.arteziowebapp.configuration.webConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.Locale;
/**
* The WebConfig.class.
* Purpose: Web configuration.
*
* @author Vadzim Kavalkou
* @version 1.0 22/07/2016
*/
@Configuration
@EnableWebMvc
@ComponentScan({"by.vk.arteziowebapp.web", "by.vk.arteziowebapp.configuration.*"})
@PropertySource("classpath:i18n")
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* Gets viewResolver with properties.
*
* @return viewResolver.
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
/*Set whether to make all Spring beans in the application context accessible
as request attributes, through lazy checking once an attribute gets accessed.*/
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
/**
* Sets the path to css,image and js files.
*
* @param registry
*/
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/")
.setCachePeriod(31556926);
}
/**
* DispatcherServlet forwards requests for static resources to the servlet container’s default servlet
* and not tries to handle them itself.
*
* @param configurer .
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* Gets messageSource with locales description.
*
* @return messageSource.
*/
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
/**
* Gets LocaleResolver object with locale's properties.
*
* @return resolver.
*/
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("en"));
resolver.setCookieName("localCookie");
resolver.setCookieMaxAge(3600);
return resolver;
}
/**
* Sets LocaleInterceptors parameters.
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("locale");
registry.addInterceptor(interceptor);
}
}

我的index.jsp :( src / main / webapp / WEB-INF / views /)
<%@ page language="java" contentType="text/html" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:url value="/index?locale=ru" var="localeRU"/>
<c:url value="/index?locale=en" var="localeEN"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><spring:message code="label.titleIndex"/></title>
<spring:url value="/resources/css/style.css" var="css"/>
<spring:url value="/resources/js/script.js" var="js"/>
<spring:url value="/resources/js/jquery-2.2.1.min.js" var="jq"/>
<spring:url value="/resources/images/vk.png" var="vk"/>
<spring:url value="/resources/images/vk-mouseover.png" var="vkMouseOver"/>
<link href="${css}" rel="stylesheet"/>
</head>
<body>
<c:if test="${param.logout != null}">
<p>
<spring:message code="label.logoutMessage"/>
</p>
</c:if>
<div id="locales"><a href="${localeRU}">RU</a> | <a href="${localeEN}">EN</a></div>
<form:form action='/result' method='get' id="loginForm">
<h1><spring:message code="label.titleIndex"/></h1>
<fieldset id="inputs">
<input id="email" type='text' placeholder="<spring:message code="label.email"/>"/>
<input id="password" type="password" placeholder="<spring:message code="label.password"/>"/>
</fieldset>
<input type="submit" id="submit" value="<spring:message code="label.loginButton"/>"/>
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
</form:form>
<footer id="footer">
<div id="footer-info">© VK, 2015-</div>
<div id="image-ref">
<a href="http://vk.com/"><img id="vk-image" src="${vk}"></a>
</div>
</footer>
<script src="${js}" rel="script"/>
<script src="${jq}" rel="script"/>
</body>
</html>
&#13;
谢谢你,伙伴们!
答案 0 :(得分:0)
将文件从src / main / java / resources移动到src / main / resources 并删除&#34; / resources&#34;部分在spring配置中,例如
<spring:url value="/css/style.css" var="css"/>
Maven会将文件放在classpass根目录下的resources目录中,而不是放在类路径根目录下的/ resources文件夹中。