java - CSS资源在spring 4 mvc中无法访问

标签 java css spring jsp spring-mvc

我尝试了很多例子和引用来让它工作仍然无法访问

link

但仍然无法访问资源

这是我的代码

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.raghu")
public class AppConfig extends WebMvcConfigurationSupport {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }


}

Appconfig 是我添加资源位置的步骤,我也在 dispatcher-servlet.xml 中添加并尝试过

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }

}

调度器-servelt.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

  <!-- Enables the Spring MVC @Controller programming model -->
  <annotation-driven />

  <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
  <resources mapping="/resources/**" location="/resources/" />

  <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
  <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <context:component-scan base-package="com.raghu" />

</beans:beans>

JSP

<link rel="stylesheet" type="text/css" href="/resources/css/component.css" />

这里我也尝试给出上下文路径

最佳答案

配置看起来没问题,以为只有你要做的就是改变<link rel="stylesheet" type="text/css" href="/resources/css/component.css"/><link rel="stylesheet" type="text/css" href="css/component.css" />如果component.css位于 src\main\resources 目录。

此外,您可以更改 AppConfig扩展WebMvcConfigurerAdapter

关于java - CSS资源在spring 4 mvc中无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075638/

相关文章:

java - RecyclerView 没有附加适配器

html - 为什么固定元素不拉伸(stretch)到网格区域的整个宽度?

css - 如何通过 CSS 中的悬停选择器更改 JFXButton 中的 Font Awesome 字形颜色?

html - Materialise CSS Modal 将正文内容向右移动

java - 在给定数组中的单元格的情况下设置二维数组中的行、列和对角线元素的函数

Java 图形用户界面/图形

java - 如何从列表/集合和自定义比较器在 Java 中以 O(N) 创建优先级队列

java - ApplicationListener 处理 ContextClosedEvent 的 NullPointer 警告

java - Spring Jpa Hibernate 列未转换为查询

spring - 如何在 Spring JSP 中设置 Last-Modified 响应 header