【问题标题】:CSS mapping with spring 3.2.4 MVC - no web.xml使用 spring 3.2.4 MVC 的 CSS 映射 - 没有 web.xml
【发布时间】:2014-02-23 10:16:41
【问题描述】:

我有一个 Spring MVC 应用程序,我正在尝试使用 eclipse 和 jetty 运行。所有注释,没有 web.xml。在这里尝试了一半的答案后,我仍然无法让我的 CSS 工作。

我的 CSS 在这里

src/main/webapp/resources/css/main.css

在我的 JSP 中有

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/main.css" />">

看起来正确,解析为

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

我尝试将它添加到我的 applicationContext.xml

<mvc:resources mapping="/resources/**" location="/resources/" />

它仍然没有找到 css。

有什么想法吗?

编辑

我的初始化器

public class WebInitializer implements WebApplicationInitializer {

    private final Logger log = Logger.getLogger(WebInitializer.class);

    public void onStartup(ServletContext container) {

        log.info("Starting web app");

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(AppConfig.class);
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(MvcConfig.class);

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

【问题讨论】:

  • 浏览器的开发工具是怎么说的?
  • 您的DispatcherServlet 是如何映射的?
  • @M. Deinum,查看我的编辑,添加了我的初始化程序。
  • &lt;mvc:default-servlet-handler /&gt; 添加到您的配置中。
  • 不走运。仍然得到 404。

标签: css eclipse spring model-view-controller jetty


【解决方案1】:

看起来您不能将 XML 和编程配置结合起来。

我找到了this 链接。在我的配置类中输入它后,它起作用了......

public class WebConfig extends WebMvcConfigurerAdapter {

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2011-01-18
    相关资源
    最近更新 更多