gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:18:11 27 4
gpt4 key购买 nike

我有一个以编程方式设置的 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());
}
}

所有相关的东西都在爆炸 war 中:javafullstack\资源

否则项目设置是:javafullstack\src\main\webapp\resources

示例 .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>

非常感谢任何帮助!

最佳答案

好的,这是因为我未能在 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());
}
}

关于java - 在 Java 8 Spring 4.2.4 中加载静态资源时获取 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com