Pujan
Pujan

Reputation: 3264

404 Returnred for index.htm in Spring MVC

I am getting 404 for index.htm. As you can see that index.htm is registered in the server.

Server log

[0m[0m10:00:57,643 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Mapped URL path [/index.htm] onto handler 'indexController'
[0m[0m10:00:57,644 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Root mapping to handler 'indexController'
...
[0m[0m10:07:31,827 INFO  [stdout] (http-/0.0.0.0:8080-1) Error message=404 returned for /EAFUIWeb/index.htm with message Not Found : [Fri Sep 26 10:07:31 ICT 2014]Not Found

Controller

@Controller
public class IndexController {
    @RequestMapping(value={"/index.htm","/"}, method = RequestMethod.GET)
    public ModelAndView initGET(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ...
    }

    @RequestMapping(value={"/index.htm","/"}, method=RequestMethod.POST)
    public ModelAndView loadEntity(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ..
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
...
</web-app>

index.jsp

<c:redirect url="/index.htm" />

applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sec="http://www.springframework.org/schema/security" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.2.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <sec:global-method-security
        pre-post-annotations="enabled" proxy-target-class="true" />

    <mvc:annotation-driven />
    <mvc:resources mapping="/config/**" location="/config/" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/tmpls/**" location="/tmpls/" />
    <mvc:resources mapping="/media/**"
        location="file:${user.home}/uploads/avatar/" />

    <context:component-scan base-package="com.bara.j2ee.pattern.control.spring,com.master.util" />

    <mvc:default-servlet-handler/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <context:property-placeholder location="classpath:ad.properties"
        ignore-unresolvable="true" />

Is there any configuration problem? How can I fix 404 for index.htm

Upvotes: 0

Views: 303

Answers (3)

Jimmy
Jimmy

Reputation: 1875

To solve this answer you need to change your / to /*.htm i think this is the problem.

Upvotes: 0

Pujan
Pujan

Reputation: 3264

Thanks all. I have figured it out. It was multiple viewresolver issue. I have no index.jsp file but another jsp servces /config/index.jsp and I have added in spring-view.xml. As per mkyong, this order should be preserved.

<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>/WEB-INF/springapp-view.xml</value>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

spring-view.xml

<bean id="index" class="org.springframework.web.servlet.view.JstlView">
    <property name="url" value="/config/index.jsp" />
</bean>

Reference: http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/

Upvotes: 0

Desorder
Desorder

Reputation: 1539

To solve those 404 problems, you need to understand how Spring finds and loads your JSPs.

In a short description... When you call, in this case, /index.htm, DispatchServlet will look for a method that has /index.htm mapping and call that method. The method will return something. In this case, you are supposed to return a ModelAndView. something like:

modelAndView.setViewName("myPage");
return modelAndView;

Then Spring comes to list of ViewResolvers and by the order, it will check where it can find "myPage".

Again, in your case, as you only have one, it will look for /WEB_INF/jsp/myPage.jsp (p:prefix="/WEB-INF/jsp/" p:suffix=".jsp").

Your problem seems to be that Spring can't find your page (which I don't know as you didn't post your initGET method body.

Make sure that, whatever you are setting/returning there can be found /WEB_INF/jsp/ and ends with .jsp

Upvotes: 1

Related Questions