Spring MVC : I set the default page but the spring always should me the configuration files do not find

标签 spring spring-mvc configuration jboss http-status-code-404

我尝试在eclipse上运行一个基本的Java EE Spring项目(jboss 7.1.1服务器,Spring 3.1.2发布),但是当它总是打印配置文件找不到但我实际上把配置文件放在正确的位置。我没有配置welcome-file,而是配置mvc:view-controller。

this is my error screen shot

这是 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
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_3_0.xsd">
<display-name>springupload</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</context-param>
<!-- Loads the Spring web application context -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value/>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

这是 web-application-config.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"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.pack" />

<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webmvc-config.xml" />
<!-- <import resource="webflow-config.xml" /> -->
<!-- <import resource="data-access-config.xml" /> -->

    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
        <property name="maxUploadSize" value="1000000"></property>
    </bean>
</beans>

这是 webmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- Enables controllers mapped with @RequestMapping annotations, formatting annotations @NumberFormat @DateTimeFormat, and JSR 303 style validation -->
<mvc:annotation-driven/>

<mvc:resources mapping="/res/**" location="/, classpath:/META-INF/web-resources/" />
<mvc:view-controller path="/" view-name="hello"/>
<mvc:default-servlet-handler />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspre">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".html"/>
    </bean>

</beans>

您在图片中看到的错误:

HTTP 状态 404 -/springupload/WEB-INF/webmvc-config.xml

输入状态报告

消息/springupload/WEB-INF/webmvc-config.xml

描述请求的资源(/springupload/WEB-INF/webmvc-config.xml)不可用。 JBoss Web/7.0.13.Final

我真不知道为什么我要配置html和jsp页面,而它却要一些配置文件作为我的起始页?

最佳答案

您的配置距离正常已经不远了。

我注意到的一件事是 hello.html 文件位于您的根 WebContent 文件夹中。我想这是您访问 http://localhost:8080/springupload/时想要呈现的 View ,因为配置中有这一行:

<mvc:view-controller path="/" view-name="hello"/>

如果是这样,那么 Spring 会尝试解析为 /WEB-INF/hello.html,因为 前缀后缀在这个viewResolver上:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".html"/>
</bean>

但是,您有两个 View 解析器,其中没有顺序,并且 Spring 仅采用第一个解析为 /WEB-INF/hello.jsp 的 View 解析器,因此 404 Not发现

要总结一下,您的解决方案是将 hello.html 移动到 /WEB-INF/ 并更改 viewResolver 配置 webmvc-config.xml像这样

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspre">
    <property name="order" value="2" />
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
    <property name="order" value="1" />
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".html"/>
</bean>

最后,您不应该直接访问 http://localhost:8080/WEB-INF/* URL 中的内容,因此您在此处尝试的所有操作都将导致 404 未找到

关于 Spring MVC : I set the default page but the spring always should me the configuration files do not find,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713168/

相关文章:

Spring Boot Taglibs

java - @PostFilter 包含?

java - 如何在 Spring MVC 2.5 项目中使用 MockMvc?

java - Spring - @Transactional 不启动事务

spring-mvc - Apache shiro,返回状态 401 而不是重定向到 url

java - 无法更改 Java 版本 GNU/linux

java - JBOSS+SPRING 请求参数 URL 编码

java - Spring MVC : how to get a handler method for a request

java - jooq:如何为静态 DSL 方法配置方言?

c++ - 当通过 boost::program_options 使用多个源时,使用最后一个而不是第一个存储值