Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 404 error [duplicate]

I'm going crazy and can't understand what the problem is:

I have the following structure:

SpringMVC
  +WebContent
      -web-inf
        -web.xml
        -springMVC-servlet.xml
      -index.jsp
      -security
           -login.jsp 

web.xml

<display-name>springMVC</display-name> 
<servlet> 
  <servlet-name>springMVC</servlet-name> 
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping> 
  <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
  <servlet-name>springMVC</servlet-name> 
  <url-pattern>/index.html</url-pattern>
</servlet-mapping>
<welcome-file-list> 
  <welcome-file>index.html</welcome-file> 
</welcome-file-list>

springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans"> 

    <context:annotation-config/> 
    <context:component-scan base-package="com.vanilla.springMVC"/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
       <property name="prefix"> 
          <value>/</value> 
       </property> 
       <property name="suffix"> 
       <value>.jsp</value> 
       </property> 
    </bean> 

</beans>

My Controller:

package com.vanilla.springMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class DefaultController {

    @RequestMapping(value="/index.html", method=RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    @RequestMapping(value="/login.html", method=RequestMethod.GET)
    public ModelAndView loginPage(){
        ModelAndView mv = new ModelAndView("/security/login");
        return mv;
    }
}

I have no problem to navigate to /index.html

http://localhost:8080/SpringMVC/index.html works perfect.

however when I'm navigating to http://localhost:8080/SpringMVC/login.html i have 404 error.

HTTP Status 404 - /SpringMVC/login.jsp    
type Status report
message /SpringMVC/login.jsp
description The requested resource (/SpringMVC/login.jsp) is not available.

I don't want to move login.jsp on the same level as index.jsp, but why do I have this problem?

like image 805
danny.lesnik Avatar asked May 02 '11 07:05

danny.lesnik


People also ask

How does Spring MVC handle 404 error?

The 404 error code is configured properly, but it will caused the “. htm” extension handling conflict between the “servlet container” and Spring's “DispatcherServlet“. To solve it, try change the 404. htm to other file extension, for example 404.

What is the meaning of HTTP Error 404?

The HTTP 404 Not Found response status code indicates that the server cannot find the requested resource. Links that lead to a 404 page are often called broken or dead links and can be subject to link rot.

What is Spring web MVC?

A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.


2 Answers

HTTP 404 means that the resource is not found.

  • This means the controller or a direct accessed resource (like a CSS file) is not found
  • It does NOT mean that the JSP referred in the controller is not found (this would be a 500 Internal Server Error)

HTTP Status 404 - /SpringMVC/login.jsp

It looks like that you send a HTTP request /SpringMVC/login.jsp but your controller method is bound to .html, so you need to change your HTTP request to /SpringMVC/login.html

Because of the name (login) may your Spring Security configuration is not correct.

like image 32
Ralph Avatar answered Sep 22 '22 06:09

Ralph


I'll just add this in here, because it solved my 404 issue. It turned out my problem was the url-mapping value in web.xml. Using Spring WebMVC version org.springframework:spring-webmvc:4.1.6.RELEASE

I was using the following (which didn't work):

<url-pattern>/rest</url-pattern>

I should've been using the following value (which works):

<url-pattern>/rest/*</url-pattern>

or

<url-pattern>/</url-pattern>

Reference: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

like image 184
Nick Grealy Avatar answered Sep 20 '22 06:09

Nick Grealy