2

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

这是我的错误屏幕截图

这是 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页面,而它应该一些配置文件作为我的起始页面?

4

2 回答 2

2

你的配置离OK不远了。

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

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

如果是这样,那么 Spring 正试图解决这个viewResolver/WEB-INF/hello.html上的prefixand :suffix

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

但是,您有两个没有顺序的视图解析器,而 Spring 只采用第一个解析为 的视图解析器/WEB-INF/hello.jsp,因此404 Not found

总结它,您的解决方案是像这样移动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 Not found.

于 2012-10-03T18:13:13.007 回答
0

将您的配置文件名更改为[dispatcher servlet name]-servlet.xml

http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html

于 2012-10-03T17:06:37.067 回答