Spring MVC:HTTP状态404:请求的资源不可用

Posted

技术标签:

【中文标题】Spring MVC:HTTP状态404:请求的资源不可用【英文标题】:Spring MVC: HTTP status 404: The requested resource is not available 【发布时间】:2014-08-25 07:44:36 【问题描述】:

我是 Spring MVC 的新手。我正在尝试将 Thymeleaf 与 Spring 4 集成,并且我用一个控制器/方法编写了一个 helloworld 程序。但是,当我启动Tomcat时,访问http://localhost:8080/hello时出现此错误

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

在我的 pom.xml 文件中,我有以下内容:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>homeMadeSpring</groupId>
  <artifactId>homeMadeSpringFirst</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>homeMadeSpringFirst</name>
    <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.0.5.RELEASE</spring.version>
      <spring.security.version>3.2.4.RELEASE</spring.security.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>$spring.version</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>$spring.version</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>$spring.version</version>
      </dependency>
      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf</artifactId>
          <version>2.1.2.RELEASE</version>
          <scope>compile</scope>
      </dependency>
      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf-spring4</artifactId>
          <version>2.1.2.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>joda-time</groupId>
          <artifactId>joda-time</artifactId>
          <version>2.1</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-core</artifactId>
          <version>$spring.security.version</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-config</artifactId>
          <version>$spring.security.version</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-web</artifactId>
          <version>$spring.security.version</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-taglibs</artifactId>
          <version>$spring.security.version</version>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
      </dependency>
  </dependencies>
    <build>
        <finalName>homeMadeSpringFirst</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.2.v20120308</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/homemade</contextPath>
                    </webAppConfig>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <stopKey>stop-jetty</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
            </plugin>
        </plugins>
    </build>s
</project>

在我的 src/main/resources 目录中,我有两个文件:applicationContext-security.xml 和 thymeleaf-pages.xml。

applicationContext-security.xml的内容是:

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


    <http pattern="/login" security="none"/>
    <http pattern="/assets/**" security="none"/>

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/homeMadeSpring" access="isAuthenticated()" />

        <form-login
                login-processing-url="/authentication"
                login-page="/login"
                authentication-failure-url="/login?error=true"
                default-target-url="/hello"
                always-use-default-target="false"
                />
        <remember-me user-service-ref="userDetailsService"/>
        <logout logout-url="/logout"
                logout-success-url="/login"/>
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService"/>
    </authentication-manager>


    <user-service id="userDetailsService">
        <user name="admin" password="admin" authorities="ROLE_USER"/>
    </user-service>

</beans:beans>

**在我的 thyme-pages.xml 中,我有:**

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


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


    <bean id="httpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
    </bean>

    <bean id="defaultMessageConverters" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="httpMessageConverter"/>
            </list>
        </constructor-arg>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
        <property name="messageConverters" ref="defaultMessageConverters"/>
        <property name="cacheSeconds" value="0"/> <!--  NO CACHE  -->
    </bean>


    <bean id="homeController" class="controllers.HomeController"/>


    <bean id="contentNegotiatingResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="pdf" value="application/pdf"/>
                <entry key="xsl" value="application/vnd.ms-excel"/>
                <entry key="xml" value="application/xml"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
    </bean>

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="cacheable" value="false"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>


    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="dateFormatter" class="util.DateFormatter"/>



</beans>

在 src/main/webapp/WEB-INF 目录下,我的 web.xml 有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>HelloWorld</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:thymeleaf-pages.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/pages/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <servlet-name>HelloWorld</servlet-name>
    </filter-mapping>
</web-app>

最后,我的控制器是:

@Controller
public class HomeController

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloWorld(Model model)
    
        return "pages/hello";
    

我的项目结构截图:

【问题讨论】:

你可以尝试将 /* 作为 HelloWorld servlet 的 url 模式 你的上下文路径是什么? @coder 你认为那会做什么以及为什么? 我遇到了类似的问题,但在清理和重建后让它工作了。 我猜你应该访问 url localhost:8080/HelloWorld/hello 而不是 localhost:8080/hello 【参考方案1】:

如果您熟悉基于 java 的配置,那么您需要将此类添加到您的代码中

    @Configuration
    @PropertySource("classpath:thymeleaf.properties")
    public class ThymeleafConfiguration 

       @Value("$templatePrefix")
       private String templatePrefix;

       @Value("$templateMode")
       private String templateMode;

    @Bean
    public TemplateResolver templateResolver() 
       TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
       templateResolver.setPrefix(templatePrefix);
       templateResolver.setTemplateMode(templateMode);
       templateResolver.setOrder(1);
       return templateResolver;
    

   @Bean
   public TemplateEngine templateEngine() 
       TemplateEngine templateEngine = new SpringTemplateEngine();
       templateEngine.setTemplateResolver(templateResolver());
       return templateEngine;
     
    

您的属性文件需要包含这些属性

templatePrefix =/mail/
templateMode =HTML5

之后,只需将配置类包含到 other 中,您的 thymeleaf 就会启动并正常工作。或者用 XML 进行同样的转换

【讨论】:

【参考方案2】:

这里有两件事:

1) 您已经在 pom.xml 中使用上下文名称配置了 jetty 插件:自制,因此如果您使用 jetty,则必须使用此上下文。

2) 你已经配置了 spring 安全,但是你没有任何带有 @RequestMapping(value = "/login") 的控制器,要么删除 spring 安全,要么在控制器中定义一个方法来处理登录页面.

【讨论】:

以上是关于Spring MVC:HTTP状态404:请求的资源不可用的主要内容,如果未能解决你的问题,请参考以下文章

Spring 4 MVC,警告:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为 [...] 和 HTTP 404 错误

Spring Boot POST 请求返回 http 状态 405“方法不允许”而不是 HTTP 状态 404

Spring MVC Spring 安全性和错误处理

无法使用spring boot错误HTTP状态404连接mysql数据库请求的资源不可用

Spring MVC HTTP 状态 405 - 不支持请求方法“POST”

HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC)