0

I am using default configuration of Spring MVC 2. So, my dispatcher will look like,

 <prop key="index.htm">indexController</prop>

I have a simple htm file on root. When I browse this file I get 404:Not Found Error. How to browse (which exist physically on disk) htm file in Spring MVC.

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • This question has already been answered in http://stackoverflow.com/questions/2129876/using-spring-mapping-to-root-in-web-xml-static-resources-arent-found – Liam Jan 16 '12 at 22:04
  • See my answer below, also if possible provide the entry in your web.xml for dispatcher servlet. Thanks – Liam Jan 17 '12 at 07:29

4 Answers4

2

In annotation based Spring 3.x, you can just write it in your controller as,

@RequestMaping(value="/index.htm")
public void doSomeJob(){
    //some code here
}

and if you make a request as "/pathToIt/index.htm", then it will be caught by doSomeJob() method..

P.S No need for request mappings in configuration files in Spring MVC 3.x

Note: And also 404 can be caused if spring can't find your physical file..

Zaur Guliyev
  • 4,254
  • 7
  • 28
  • 44
1

Try and add a bean id in your springmvc-servlet.xml file :

<bean id="indexController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

then :

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
               ...
           <prop key="/index.htm">indexController</prop>
         </props>
       </property>
    </bean>
Zakaria
  • 14,892
  • 22
  • 84
  • 125
1

You need to map URL index.htm with your controller. You cannot view your page until it will have correct spring mvc configuration.

<bean name="/index.htm" class="com.indexController">
 ....
</bean>

In controller you will pass your jsp page name as view in modelAndView.

Check your view resolver has correct setting like this

<!-- View Resolver -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
1

Ok this sounds familiar , if you are using MVC 2 then the best way to achieve this is to give a specific mapping for dispatcher servlet instead of /

    <servlet>
      <servlet-name>myDispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myDispatcherServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

This will make sure the only requests to *.jsp will go dispatcher servet and rest will be handled by the container itself.

Liam
  • 2,837
  • 23
  • 36