2012-10-03 31 views
2

我试着在eclipse上运行一个基本的Java EE Spring项目(jboss 7.1.1服务器,Spring 3.1.2发布),但是当它总是打印配置文件找不到,但我实际上把配置文件放在正确的位置。我不配置欢迎文件,而是配置mvc:view-controller。春天MVC:我设置了默认页面,但春天总是应该我的配置文件没有找到

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> 

这是网络应用-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

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

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

回答

2

你的配置不远未确定。

有一两件事我注意到的是,hello.html文件在根文件夹WebContent。我想这是你想要呈现的观点,当你因为这条线在配置的访问http://localhost:8080/springupload/

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

如果是这样,那么春天正试图以这种解决,因为prefixsuffix/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> 

然而,你必须与他们没有二阶视图解析器,春天只取第一个解析为/WEB-INF/hello.jsp,因此404 Not found

把它包起来您的解决方案是将hello.html/WEB-INF/和改变webmvc-config.xml的ViewResolver配置,像这样

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

最后,你不应该访问的URL http://localhost:8080/WEB-INF/*直接内容,所以你在这里尝试的所有内容都将导致404 Not found

+0

你知道如何制作第一个org.springframework.web.servlet.view.InternalResourceViewResolver可以匹配文件* .html或* .jsp吗? – user504909

+0

我不认为你可以在'InternalResourceViewResolver'中指定多个后缀。如果您想支持多个后缀,请为每个后缀定义一个_viewResolver_,或者编写自己的_viewResolver_,它接受一组后缀。 – betomontejo

+0

我做了除最后一项之外的所有内容,但对于http:// localhost:8080/springupload /仍为404。我把hello.jsp放到/ WEB-INF /里,serlvet里面只有一个Resolver for jsp文件。我认为InternalResourceViewResolver将直接访问/ WEB-INF/*下的内容。您能告诉我如何访问http:// localhost:8080/springupload/WEB-INF/* – user504909