【问题标题】:JSP, import css,js,imagesJSP, 导入 css,js,图片
【发布时间】:2016-12-19 13:27:11
【问题描述】:

我有一个 Spring MVC 应用程序,其中包含当前放置在 src/main/java/resources/ 中的一堆 *.css、*.js 和 *.png 文件目录。 我阅读了 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">&copy 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>

谢谢小伙伴们!

【问题讨论】:

    标签: java css jsp spring-mvc


    【解决方案1】:

    将文件从 src/main/java/resources 移动到 src/main/resources 并删除 spring 配置中的“/resources”部分,例如

     <spring:url value="/css/style.css" var="css"/>
    

    Maven 会将文件放在类路径根目录下的资源目录中,而不是放在类路径根目录下的 /resources 文件夹中。

    【讨论】:

    • 我检查了我的 *.war 文件,但我没有在这里找到资源文件夹。只有 web-inf 和 meta-inf...
    • 不应该是战争中的资源文件夹。这些文件应该在战争的顶级文件夹中。请将 css、js 和 images 目录移动到 webapp 文件夹。那么它应该可以工作了。
    猜你喜欢
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多