9

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?

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200

7 Answers7

5

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

Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
  • 1
    Thanks you to save my time. My code is: ` springrest org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/mvcContext.xml 1 springrest /rest/* ` – frss-soft.com Nov 06 '16 at 19:53
  • 1
    I actually spent more than one hour just debugging this, and it finnaly gets solved, I love answers that come after a time for popular questions like these and actually address some other posssible problems, this was really helpfull. – Gherbi Hicham Nov 28 '17 at 12:25
4

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • I understand that, however I can't figure it out what and where the problem is? – danny.lesnik May 02 '11 at 08:01
  • @danny.lesnik: see extended answer – Ralph May 02 '11 at 08:02
  • @Ralph, I see that but it was mapped as /index.html and I can realize why I am going to /SpringMVC/login.jsp – danny.lesnik May 02 '11 at 08:56
  • @danny.lesnik: "but it was mapped as /index.html" - I do not understand? - Which url do the browser request? -- From the name, it looks like the Spring Security Interceptor is redirecting it. -- Please add the Spring Security configuration to you post. -- Or how I already mentioned, check the Spring Security Config, that it forwards to HTML and not JSP. – Ralph May 02 '11 at 09:15
  • @Ralph, I don't have any Spring security config, just 2 pages. I posted all code and configuration I have. – danny.lesnik May 02 '11 at 09:20
  • danny.lesnik: then what is the link between `index.html` and `login.jsp`? – Ralph May 02 '11 at 09:25
  • @Ralph no link, I have 2 mapping according to Controller /index.html as /index.jsp (works perfect) and /login.html as /security/login.html – danny.lesnik May 02 '11 at 09:27
  • @danny.lesnik: I wrote "you need to change your HTTP request to /SpringMVC/login.html" you answered: "but it was mapped as /index.html" -- Please explain: what you are doing, what you expect, and what happend! – Ralph May 02 '11 at 09:34
2
  1. create a folder under WEB-INF "jsps" for all your views, and under "jsps" a "security" folder for your case. NOTICE: Moving the JSP files into WEB-INF, you can prevent direct access on these files. It is needed for application security. Think about a situation, in which your JSPs gives personal informations about your customer and the access has to be granted/checked by your controller. If your JSPs are existing out of WEB-INF, they are accessible with a request directly on them.

  2. configure your view resolver like this:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
       <property name="prefix" value="/WEB-INF/jsps"/>
       <property name="suffix" value=".jsp"/>
    </bean>

next, put your login JSPs into the "WEB-INF/jsps/security" and return "security/login" from your loginPage.

Now, the view resolver searchs for the views under "WEB-INF/jsps". Because your method return "security/login", the view resolver expects a directory under jsps called "security" and a JSP file under this, which is called "login.jsp" (suffix = jsp)

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
0

I am using Spring MVC with eclipse and a MacOS X system and encountered a 404 error when controller returning the correct jsp name. I later figured out that the problematic jsp file has no x(execution) right on the file system. I then `chmod +x some.jsp' and the issue is resolved.

ZJ Lyu
  • 331
  • 3
  • 10
0

This answer may be outdated for the above question, but it will help others who are just starting with spring. I was also struggling with 404 WEB-INF/welcome.jsp page not found error.

Here issue is with ModelAndView import

Replace

import org.springframework.web.servlet.ModelAndView;

With

import org.springframework.web.portlet.ModelAndView;
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
0

Adding this ans here just in case someone else faces the same issue. My app was working fine earlier, then I upgraded Spring version and in the process also upgraded spring-security version. When I tried to access my login html page I was facing this issue. I had made all the necessary configurations changes because of the change of spring-security version from 3 to 4. I referred to this link and it was very helpful.

I was facing this problem because of the below configuration in my web-servlet.xml file

<mvc:resources mapping="/app/*.html" location="/app/*.html" cache-period="0"/>
<mvc:resources mapping="/app/**/*.html" location="/app/**/*.html" cache-period="0"/>

It worked after I modified both location values to "/app/". It seems like newer version of spring matches either the whole string or checks if the requested location starts with the location value configured above.

I found the below code in org.springframework.web.servlet.resource.PathResourceResolver:

if (locationPath.equals(resourcePath)) {
    return true;
}
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
if (!resourcePath.startsWith(locationPath)) {
    return false;
}

The second if condition was evaluating to false in my case and hence the 404 HTTP response

Another tip, if you face such error try enabling highest level of springframework logging. You might get to know the error reason from there.

0

I suspect that ModelAndView mv = new ModelAndView("/security/login"); is where the problem is. Make sure you have a directory 'security' folder in your root directory and it contains a login.jsp file. Or try moving the security folder inside WebContent

Provide the contextConfigLocation init parameter of your DispatcherServlet

 <servlet>
    <servlet-name>appServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>
gouki
  • 4,382
  • 3
  • 20
  • 20