【问题标题】:CSS resources are not able to access in spring 4 mvcCSS资源在spring 4 mvc中无法访问
【发布时间】:2015-06-26 14:21:19
【问题描述】:

我尝试了很多示例和参考以使其正常工作仍然无法访问

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" />

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

【问题讨论】:

  • 配置看起来没问题,认为只要&lt;link rel="stylesheet" type="text/css" href="/resources/css/component.css" /&gt; 位于src\main\resources 目录下,将&lt;link rel="stylesheet" type="text/css" href="/resources/css/component.css" /&gt; 更改为&lt;link rel="stylesheet" type="text/css" href="css/component.css" /&gt;
  • 我也这样做了...运气不好!!
  • 我还注意到AppConfig 中您的代码和我的代码之间存在差异。将其更改为扩展WebMvcConfigurerAdapter
  • 非常感谢你成功了!!!你拯救了我的一天!
  • 好的,我把 cmets 作为答案

标签: java css spring jsp spring-mvc


【解决方案1】:

Configurations 看起来没问题,如果component.css 位于 src\main\resources 目录,则认为您只需将&lt;link rel="stylesheet" type="text/css" href="/resources/css/component.css"/&gt; 更改为&lt;link rel="stylesheet" type="text/css" href="css/component.css" /&gt;

另外,您可以将AppConfig 更改为扩展WebMvcConfigurerAdapter

【讨论】:

  • 链接很好,只是我不得不扩展 WebMvcConfigurerAdapter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
  • 2015-07-08
  • 2013-08-15
相关资源
最近更新 更多