【问题标题】:No mapping found for HTTP request with URI [/myapp/img/logout.png]未找到带有 URI [/myapp/img/logout.png] 的 HTTP 请求的映射
【发布时间】:2014-12-10 09:54:44
【问题描述】:

我正在编写 spring mvc 应用程序。

我曾问过这个问题should I make two different servlet entry for rest and normal html in web.xml,并通过在 stackoverflow 上的知识渊博的人给出的答案得到了解决(答案:should I make two different servlet entry for rest and normal html in web.xml

现在我的 web.xml 包含以下代码

<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring_myapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

但是在对答案中提到的 web.xml 进行更改后,我收到错误No mapping found for HTTP request with URI [/myapp/img/logout.png]

我无法加载任何css, js as well as images

我搜索了这个问题并得到了答案https://stackoverflow.com/a/17946825/3898076。 如果您发现此问题重复,请分享解决方案。

我无法弄清楚我的错误。你能帮我解决这个问题吗?

注意:我将文件包含为&lt;script type="text/javascript" src="js/jquery.js"&gt;&lt;/script&gt;

谢谢。

【问题讨论】:

    标签: java spring servlets web.xml


    【解决方案1】:

    也许你正在寻找这样的东西

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/css/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/images/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/js/*</url-pattern>
    </servlet-mapping>
    

    【讨论】:

    • 感谢您的回复,但它并没有完成我的工作。当我使用@RequestMapping("/htmlUrl") 时,这就是我保留/ 的原因。而且我猜 DispatcherServlet 应该绕过 js、css 和图像,但在我的情况下并没有这样做。
    【解决方案2】:

    我找到了问题的答案。 https://stackoverflow.com/a/4556267/3898076

    作为参考,答案中提到的代码 sn-p 将包含在 Spring 3.0.4+ ONLY 的 servlet-context.xml 中。

    <!-- resources exclusions from servlet mapping -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    

    【讨论】:

      【解决方案3】:

      使用它可以解决我的问题:

      <!-- resources exclusions from servlet mapping --> 
      <mvc:resources mapping="/css/**" location="/css/" /> 
      <mvc:resources mapping="/images/**" location="/images/" /> 
      <mvc:resources mapping="/js/**" location="/js/" /> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-16
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多