在Java 8 Spring 4.2.4中加载静态资源时获取404

时间:2016-02-22 18:17:42

标签: java spring spring-mvc tomcat

我有一个以编程方式设置的Java 8 Spring 4.2.4应用程序。但是,我在Tomcat访问日志中得到以下内容 - (CSS和JS):

127.0.0.1 - - [22/Feb/2016:11:54:02 -0600] "GET /javafullstack/ HTTP/1.1"   200     34257
127.0.0.1 - - [22/Feb/2016:11:54:02 -0600] "GET /javafullstack/resources/font-awesome/css/font-awesome.min.css HTTP/1.1" 404 992
127.0.0.1 - - [22/Feb/2016:11:54:02 -0600] "GET /javafullstack/resources/js/classie.js HTTP/1.1" 404 992
127.0.0.1 - - [22/Feb/2016:11:54:02 -0600] "GET /javafullstack/resources/js/jquery.js HTTP/1.1" 404 992
127.0.0.1 - - [22/Feb/2016:11:54:02 -0600] "GET /javafullstack/resources/js/bootstrap.min.js HTTP/1.1" 404 992

这就是我所拥有的:

package com.thoughtscript.javafullstack.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.thoughtscript.javafullstack")
public class Config {

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

@Bean
public JpaTransactionManager jpaTransMan() {
    JpaTransactionManager jtManager = new JpaTransactionManager(getEntityManagerFactoryBean().getObject());
    return jtManager;
}

@Bean
public LocalEntityManagerFactoryBean getEntityManagerFactoryBean() {
    LocalEntityManagerFactoryBean lemfb = new LocalEntityManagerFactoryBean();
    lemfb.setPersistenceUnitName("localEntity");
    return lemfb;
}

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600)
            .resourceChain(true).addResolver(new PathResourceResolver());
}
}

所有相关的东西都是爆炸性的战争: javafullstack \资源

否则项目设置为: javafullstack \ SRC \主\ web应用\资源

示例.jsp片段:

           <!-- jQuery -->
            <script src="<c:url value="/resources/js/jquery.js "/>"></script>

            <!-- Bootstrap Core JavaScript -->
            <script src="<c:url value="/resources/js/bootstrap.min.js "/>"></script>

            <!-- Plugin JavaScript -->
            <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
            <script src="<c:url value="/resources/js/classie.js "/>"></script>
            <script src="<c:url value="/resources/js/cbpAnimatedHeader.js "/>"></script>

            <!-- Contact Form JavaScript -->
            <script src="<c:url value="/resources/js/jqBootstrapValidation.js "/>"></script>
            <script src="<c:url value="/resources/js/contact_me.js "/>"></script>

            <!-- Custom Theme JavaScript -->
            <script src="<c:url value="/resources/js/freelancer.js "/>"></script>

            <!-- Custom -->
            <script src="<c:url value="/resources/js/custom.js "/>"></script>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

好了,这是因为我无法在Config类中扩展WebMvcConfigurerAdapter。通过扩展,我可以覆盖addResourceHandlers()。

应该看起来像:

package com.thoughtscript.javafullstack.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.thoughtscript.javafullstack")
public class Config extends WebMvcConfigurerAdapter{

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

    @Bean
    public JpaTransactionManager jpaTransMan() {
        JpaTransactionManager jtManager = new JpaTransactionManager(getEntityManagerFactoryBean().getObject());
        return jtManager;
    }

    @Bean
    public LocalEntityManagerFactoryBean getEntityManagerFactoryBean() {
        LocalEntityManagerFactoryBean lemfb = new LocalEntityManagerFactoryBean();
        lemfb.setPersistenceUnitName("localEntity");
        return lemfb;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600)
                .resourceChain(true).addResolver(new PathResourceResolver());
    }
}