【问题标题】:Spring MVC resources with an embedded jetty server带有嵌入式码头服务器的 Spring MVC 资源
【发布时间】:2013-04-20 18:19:31
【问题描述】:

我的 spring mvc webapp 无法正常工作。 我正在使用带有嵌入式码头服务器的 Spring MVC。

问题是我的 mvc:resources 标签不起作用,我不知道为什么。

这里是标签:

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

我的目录结构:

    • 主要
      • java
      • 资源
        • 元信息
          • application-context.xml
          • web-context.xml
      • 网络应用
        • CSS
          • main.css
        • js
          • main.js

现在当我转到http://localhost:8080/css/main.css 时,我在调试输出中看到了这个:

Looking up handler method for path /css/main.css
Did not find handler method for [/css/main.css]
URI Template variables for request [/css/main.css] are {}
Mapping [/css/main.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@223c78ba] and 1 interceptor
Last-Modified value for [/css/main.css] is: -1
Trying relative path [main.css] against base location: ServletContext resource [/css/]
No matching resource found - returning 404

为什么这不起作用?是我的目录结构,还是我错过了一些配置?

感谢您的帮助。

编辑更多信息

我使用 Maven 构建了一个带有 shade 插件的胖罐子。我现在在我的 pom.xml 中添加了这个

<resources>
    <resource>
        <directory>src/main/webapp</directory>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

现在我的最终 jar 确实包含 css 目录,但仍然没有运气。

这是我启动嵌入式码头服务器的代码

int port = config.getInt("server.port");

final Server server = new Server();
final ServerConnector serverConnector = new ServerConnector(server);
serverConnector.setPort(port);
server.setConnectors(new Connector[]{serverConnector});

final DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("classpath:META-INF/web-context.xml");

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(new ServletHolder("defaultServlet", servlet), "/*");

HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{context, new DefaultHandler()});
server.setHandler(handlers);

server.start();
server.join();

【问题讨论】:

  • 你是如何构建你的项目的?与马文?如果是这样,包含您的 pom.xml 构建信息可能会有所帮助,或者至少说明您是将其构建为 jar 还是 war。这是相关的,因为如果您使用 Maven 将其构建为 jar,则默认情况下,您的 src/main/webapp 目录最终不会包含在 jar 中。在您配置嵌入式 Jetty 服务器的地方包含您的代码也可能会有所帮助,因为在嵌入式 Jetty Web 应用程序中提供资源涉及到额外的配置。
  • 我正在使用 Maven 作为 jar 进行构建。事实上,我正在使用阴影插件来构建一个胖罐。我将编辑我的第一篇文章以提供更多信息。
  • 我在下面提供了一个答案,这可能是您需要的缺少的配置。

标签: java spring spring-mvc


【解决方案1】:

您可能需要为您的 servlet 上下文进行一些额外的配置,就像这样,以便在您通过正在构建的 jar 运行它时正确设置类路径:

final Resource base = Resource.newClassPathResource(".");

if (base != null) {
    context.setBaseResource(base);
} else {    
    // running in a jar
    final URI uri = Service.class.getProtectionDomain().getCodeSource().getLocation().toURI();
    context.setBaseResource(Resource.newResource("jar:" + uri + "!/"));
}

【讨论】:

  • 哇,这对我来说是一种享受。我认为这就是Resource.newSystemResource 的意义所在。猜不...
  • 嘿,抱歉耽搁了,我一直很忙。这行得通,非常感谢。
【解决方案2】:

也许您的网址中缺少上下文路径?

http://localhost:8080/<<context>>/css/main.css

【讨论】:

    【解决方案3】:

    确保您的应用程序上下文中提到了 mvc 架构 xsd。

    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
    

    带有 mvc 的基本应用程序 xml 文件,上下文模式应该是这样的:

    <beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    //your beans go here
    
    </beans:beana>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2015-05-14
      • 2022-11-23
      • 2014-01-21
      相关资源
      最近更新 更多