0

我有一个 Spring MVC 应用程序,每当我请求资源时都会收到 405 错误。

我的 servlet.xml 文件中有以下内容:

<context:component-scan base-package="com.xetius"/>

<mvc:annotation-driven/>

<mvc:resources mapping="/images/**" location="classpath:/images/"/>
<mvc:resources mapping="/thumbs/**" location="classpath:/thumbs/"/>
<mvc:resources mapping="/gallery/**" location="classpath:/gallery/"/>
<mvc:resources mapping="/style/**" location="classpath:/style/"/>
<mvc:resources mapping="/script/**" location="classpath:/script/"/>
<mvc:resources mapping="/downloads/**" location="classpath:/downloads/"/>

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>org.springframework.web.servlet.view.tiles2.TilesView</value>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/views/**/views.xml</value>
        </list>
    </property>
</bean>

在我的 home.jsp 文件中,我有以下内容:

<img class="about" src="/images/about.jpg" alt="about" />

当我使用 Tomcat7 在 IntelliJ 中运行它时,所有资源都以 405 Method Not Allowed 的形式返回。以下是该请求的 HAR

{
  "startedDateTime": "2012-11-27T08:38:32.453Z",
  "time": 8,
  "request": {
    "method": "GET",
    "url": "http://localhost:8080/images/about.jpg",
    "httpVersion": "HTTP/1.1",
    "headers": [
      {
        "name": "Accept-Encoding",
        "value": "gzip,deflate,sdch"
      },
      {
        "name": "Accept-Language",
        "value": "en-GB,en-US;q=0.8,en;q=0.6"
      },
      {
        "name": "Cookie",
        "value": "JSESSIONID=0A0475B4404B037CD2FDD8876A02285A"
      },
      {
        "name": "Connection",
        "value": "keep-alive"
      },
      {
        "name": "Accept-Charset",
        "value": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
      },
      {
        "name": "Host",
        "value": "localhost:8080"
      },
      {
        "name": "User-Agent",
        "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
      },
      {
        "name": "Accept",
        "value": "*/*"
      },
      {
        "name": "Referer",
        "value": "http://localhost:8080/"
      }
    ],
    "queryString": [],
    "cookies": [
      {
        "name": "JSESSIONID",
        "value": "0A0475B4404B037CD2FDD8876A02285A",
        "expires": null,
        "httpOnly": false,
        "secure": false
      }
    ],
    "headersSize": 426,
    "bodySize": 0
  },
  "response": {
    "status": 405,
    "statusText": "Method Not Allowed",
    "httpVersion": "HTTP/1.1",
    "headers": [
      {
        "name": "Date",
        "value": "Tue, 27 Nov 2012 08:38:32 GMT"
      },
      {
        "name": "Content-Length",
        "value": "1045"
      },
      {
        "name": "Server",
        "value": "Apache-Coyote/1.1"
      },
      {
        "name": "Allow",
        "value": "POST"
      },
      {
        "name": "Content-Type",
        "value": "text/html;charset=utf-8"
      }
    ],
    "cookies": [],
    "content": {
      "size": 1045,
      "mimeType": "text/html",
      "compression": 0
    },
    "redirectURL": "",
    "headersSize": 173,
    "bodySize": 1045
  },
  "cache": {},
  "timings": {
    "blocked": 0,
    "dns": -1,
    "connect": -1,
    "send": 1,
    "wait": 6,
    "receive": 0,
    "ssl": -1
  },
  "pageref": "page_1"
}

当我检查 WAR 文件时,特定文件位于 WEB-INF/classes/images 中。

我一定是错过了什么,但这让我发疯。有人有想法么?

编辑-

我在这里包含了我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>a-Form Catering</display-name>
    <description>a-Form Catering Web Site</description>

    <servlet>
        <servlet-name>a-form</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>a-form</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

我的 servlet 文件实际上是 a-form-servlet.xml

4

3 回答 3

1

可能是您在 url 中错过了应用程序的上下文

<img class="about" src="/WEBAPP_CONTEXT/images/about.jpg" alt="about" />

如果您没有任何明确的上下文配置,那么我假设它是您的 WAR 文件的名称,没有 .war 扩展名。

您还可以在 JSP 中使用 JSTL 核心 url 标记来引用资源。

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   ..........................

 <img class="about" src="<c:url value="/images/about.jpg"/>" alt="about" />

在这种情况下,您不需要指定上下文。

于 2012-11-27T08:54:38.680 回答
1

你的里面会不会有什么可疑的东西web.xml?如果不是,我有一种预感,它可能会UrlBasedViewResolver劫持你的图像。您能否尝试添加一个viewNames属性来限制它解析的内容,例如:

<bean id="viewResolver" class="....UrlBasedViewResolver">
    ...
    <property name="viewNames" value=".jspwhatever" />
</bean>

(或一致prefix和/或suffix性质)。

希望有帮助。

干杯,

于 2012-11-27T09:11:42.767 回答
0

我确定这已解决,但以防其他人遇到同样的问题。<url-pattern>我通过更改以 * 结尾的 to 解决了类似的问题。这应该将 DispatcherServlet 映射到每个路径。见下文:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>a-Form Catering</display-name>
    <description>a-Form Catering Web Site</description>

    <servlet>
        <servlet-name>a-form</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>a-form</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
于 2015-05-21T20:50:21.840 回答