0
我有一堆*的.css,* .js文件中的Spring MVC應用程序和* .png格式目前擺在的src/main/JAVA /資源/目錄中的文件。 我通過Spring Docs和一些教程閱讀了如何使用ResourceHandlerRegistry類爲我的模板加載這些文件,但它對我無效。JSP,導入CSS,JS,圖片
我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 /主/ web應用/ WEB-INF /視圖/)
<%@ 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>
謝謝你,夥計們!
按照您所說的,沒有任何變化:( [鏈接](http://hkar.ru/KhYV) –
我檢查了我的* .war文件,並且我沒有在這裏找到資源文件夾。 inf和meta-inf ... –
不應該是戰爭中的資源文件夾,這些文件應該位於戰爭的頂層文件夾中,請將css,js和images目錄移動到webapp文件夾中,然後它應該可以工作。 – Guenther