我无法让我的 spring mvc webapp 工作。我正在使用带有嵌入式码头服务器的 Spring MVC。
问题是我的 mvc:resources 标签不起作用,我不知道为什么。
以下是标签:
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="js/"/>
我的目录结构:
- 源代码
- 主要的
- 爪哇
 - 资源
- 元信息
- 应用程序上下文.xml
 - 网络上下文.xml
 
 
 - 元信息
 - 网络应用
- css
- main.css
 
 - js
- main.js
 
 
 - css
 
 
 - 主要的
 
现在,当我转到 时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();