Spring MVC - HTTP 405 - 请求方法'GET'不受支持,加载静态CSS文件。

3

我在stackoverflow上阅读了几篇关于配置和映射以提供静态内容(如JS和CSS)的帖子,但我无法使其正常工作。

我有以下文件:

-> POM.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>es.landeSoft</groupId>
        <artifactId>MvcTesting</artifactId>
        <name>SpringMVCTesting</name>
        <packaging>war</packaging>
        <version>1.0.0-BUILD-SNAPSHOT</version>
        <properties>
            <java-version>1.6</java-version>
            <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
            <org.aspectj-version>1.6.10</org.aspectj-version>
            <org.slf4j-version>1.6.6</org.slf4j-version>
        </properties>
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework-version}</version>
                <exclusions>
                    <!-- Exclude Commons Logging in favor of SLF4j -->
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                     </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>

            <!-- AspectJ -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${org.aspectj-version}</version>
            </dependency>   

            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j-version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.15</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.mail</groupId>
                        <artifactId>mail</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.jms</groupId>
                        <artifactId>jms</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jdmk</groupId>
                        <artifactId>jmxtools</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jmx</groupId>
                        <artifactId>jmxri</artifactId>
                    </exclusion>
                </exclusions>
                <scope>runtime</scope>
            </dependency>

            <!-- @Inject -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>

            <!-- Servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

            <!-- Test -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>        
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <additionalProjectnatures>
                            <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                        </additionalProjectnatures>
                        <additionalBuildcommands>
                            <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                        </additionalBuildcommands>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArgument>-Xlint:all</compilerArgument>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <configuration>
                        <mainClass>org.test.int1.Main</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

--> WEB.XML

<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">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

--> 服务器上下文配置文件.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans     
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->


            <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />   

        <!-- Handles HTTP GET requests for /resources/ by efficiently serving up static resources in the ${webappRoot}/resources directory -->

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


        <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>

        <context:component-scan base-package="es.landeSoft.MvcTesting" />

    </beans>

您可以在servlet-context中看到我声明了以下行:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

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

我已经创建了一个 CSS 文件,位置为:

src/Main/webapp/resources/css/styles.css

在 JSP 中,我通过以下方式引用它:

href="<c:url value="/resources/css/styles.css" />" 

它的呈现效果如下:
   <link href="/MvcTesting/resources/css/styles.css" rel="stylesheet"  type="text/css"/>

但我遇到了以下问题:
GET http://localhost:8081/MvcTesting/resources/css/styles.css 405 (Method not allowed)

I tried adding servlet-mappings in web.xml but I want to make it work using the new tag

And It works, but I do not want to do it like this. the

When tomcat is starting I can read the following line:

INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'

.

enter image description here

EDIT:

I'm trying using:

<mvc:annotation-driven />
<mvc:default-servlet-handler/>

NO luck


你能发布你的 web.xml 文件吗? - Kevin Bowersox
我发现很难看到你的问题。我已经按照你提供的方式配置了许多项目。这种情况是否发生在所有资源文件中?我注意到一件事,就是你需要确保XML文件不包含任何配置,这些文件中的额外空格会导致问题,并且很难检测出来。 - Kevin Bowersox
我看到了以下这行代码:INFO: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'。我需要在什么地方注册ResourceHttpRequestHandler吗?感谢你的留言,Kevin。这让我感到很烦恼。 - Carlos Landeras
更新了web.xml文件。之前没有显示出来。 - Carlos Landeras
你是否有一些控制器映射到 / 或 /* 的情况? - Hurda
显示剩余4条评论
4个回答

4

我遇到了同样的问题,以下代码对我有用。 尝试将其添加到您的web.xml文件中

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
</servlet-mapping>

1

ResourceHttpRequestHandler类的实例无法生成405方法不允许的响应。

您可能有一个包装请求的安全层(怀疑),或者您有一个映射到//*并处理除GET之外的其他请求方法的@Controller处理程序方法。

由于@RequestMapping处理程序方法的优先级高于任何ResourceHttpRequestHandler映射,因此将使用映射到/*的处理程序来处理可以处理/MvcTesting/resources/css/styles.css的请求,并且会在响应中回复405,因为路径正确但请求方法不正确。


0

您可以尝试在web.xml中使用DispatcherServlet<url-pattern>*.css</url-pattern>进行映射,例如:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

我已经阅读过这篇文章,它指出Servlet可以提供静态内容,因此不需要额外的操作。这就是为什么创建了mvc:resource的原因。 - Carlos Landeras

0
我怀疑您可能没有通过控制器访问.jsp文件。尝试添加此控制器并通过此映射访问.jsp。
控制器:
package es.landeSoft.MvcTesting;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @RequestMapping("/")
    public String login(){
        return "login";     
    }
}

要查看完整的工作示例,请访问GitHub存储库


1
你能解释一下这会有什么不同吗? - Sotirios Delimanolis
@SotiriosDelimanolis,你说得对,这样做不行。不过我认为提供的例子可能还是有价值的。 - Kevin Bowersox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接