2

I try run a basic Java EE Spring project on eclipse(jboss 7.1.1 server, Spring 3.1.2 released), but when it always print that the configuration file do not find but I Actually put the configuration file in right place. I do not configure the welcome-file, but mvc:view-controller instead.

this is my error screen shot

this is the web.xml file:

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

This is the web-application-config.xml file

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

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

The error you can see in the picture:

HTTP Status 404 - /springupload/WEB-INF/webmvc-config.xml

type Status report

message /springupload/WEB-INF/webmvc-config.xml

description The requested resource (/springupload/WEB-INF/webmvc-config.xml) is not available. JBoss Web/7.0.13.Final

I really do not know why I configure the html and jsp page, while it should some configuration file as my start page?

user504909
  • 9,119
  • 12
  • 60
  • 109

2 Answers2

2

Your configuration is not far from being OK.

One thing I notice is that the hello.html file is in your root WebContent folder. I suppose this is the view you want rendered when you access http://localhost:8080/springupload/ because of this line in the configuration:

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

If this is so, then Spring is trying to resolve to /WEB-INF/hello.html because of the prefix and suffix on this viewResolver :

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

However, you have two view resolvers with no order in them, and Spring is taking only the first one which resolves to /WEB-INF/hello.jsp, hence the 404 Not found

To wrap it up your solution is to move hello.html to /WEB-INF/ and to change your viewResolver configuration in webmvc-config.xml like so

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

Last, you're not supposed to access directly content in the http://localhost:8080/WEB-INF/* URL, so everything you try here will result in a 404 Not found.

betomontejo
  • 1,657
  • 14
  • 26
  • do you know how to made the first org.springframework.web.servlet.view.InternalResourceViewResolver can match file *.html or *.jsp? – user504909 Oct 03 '12 at 18:52
  • I don't think you can specify more than one suffix in an `InternalResourceViewResolver`. If you want to support multiple suffixes, either define one _viewResolver_ per suffix, or write your own _viewResolver_ which accepts an array of suffixes. – betomontejo Oct 03 '12 at 19:00
  • I did everything except last one,but still 404 for http://localhost:8080/springupload/. I put hello.jsp to /WEB-INF/, only one Resolver inside the serlvet for jsp file. I think the InternalResourceViewResolver will directly access the content under /WEB-INF/*. Can you tell me how to access the content in http://localhost:8080/springupload/WEB-INF/* – user504909 Oct 03 '12 at 19:12
  • Have you changed anything else? Like the configuration file names? – betomontejo Oct 03 '12 at 19:23
  • I change the name of webmvc-config.xml into myServlet-servlet.xml and change the servlet name as myServlet fellow the suggestion below: http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html – user504909 Oct 03 '12 at 19:28
  • Check your logs, maybe there's an exception when loading the `DispatcherServlet`. When a view can not be resolved it will show a 404 the full path of the view, but this error on the `springupload/` now seems an issue with the context not being loaded properly – betomontejo Oct 03 '12 at 19:34
  • I think currently I do not add log on my spring. Can you tell my how to add log on DispatcherServlet? Sorry, I need to sleep soon, will continue tomorrow. – user504909 Oct 03 '12 at 19:43
  • I'm running out of options and this is not the best place to keep this discussions. My final advice would be to import a working sample like [the ones here](https://src.springframework.org/svn/spring-samples/) (`mvc-basic` is enough to get you started) and continue from there. – betomontejo Oct 03 '12 at 19:48
0

change your configuration file name to [dispatcher servlet name]-servlet.xml

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

Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • Thank you, it seems wok but still have problem as: Http status 404 -/springupload/ – user504909 Oct 03 '12 at 17:43
  • description The requested resource (/springupload/) is not available. – user504909 Oct 03 '12 at 17:47
  • I read the passage and corrected the error above, but seem still have problem, I just want to show hello.html. so I set hello to mvc:view-controller, but it is still do not find I configured proper.(I have file hello.html under WEB-INF, you can see the second InternalResourceViewResolver resolver WEB-INF as views). But why it still show me 404 error. – user504909 Oct 03 '12 at 18:30