Spring Error MVC 404

I'm losing my mind and don’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 go to /index.html

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

however, when I go to http: // localhost: 8080 / SpringMVC / login.html I have a 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 do not want to move login.jsp at the same level as index.jsp, but why do I have this problem?

+8
java spring spring-mvc jsp servlets
source share
7 answers

HTTP 404 means that the resource was not found.

  • This means that a controller or resource with direct access (for example, a CSS file) was not found.
  • It does NOT mean that the JSP specified in the controller was not found (this will be 500 Internal Server Error)

HTTP Status 404 - / SpringMVC / login.jsp

It looks like you are sending an 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

Due to the name (login), your Spring security configuration is incorrect.

+2
source share

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

I used the following (which did not work):

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

I should have used the following value (which works):

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

or

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

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

+4
source share
  • create a folder under WEB-INF "jsps" for all your views, and under "jsps" - a folder "security" for your business. NOTIFICATION. Moving JSP files to WEB-INF, you can prevent direct access to these files. This is necessary for application security. Think about the situation in which your JSPs provide personal information about your client and access must be granted / verified by your controller. If your JSPs exist from WEB-INF, they are available with a request directly to them.

  • configure your view recognizer as follows:

 <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, add your JSP files to "WEB-INF / jsps / security" and return "security / login" from your account.

Now, the view resolver views the views in the "WEB-INF / jsps" section. Since your method returns "security / login", the view recognizer expects a jsps subcommand called "security" under it and a JSP file called "login.jsp" (suffix = jsp)

+2
source share

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

Specify the initConfigLocation 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> 
0
source share

I am using Spring MVC with eclipse and MacOS X system and encountered 404 error when the controller returns the correct jsp name. Later I found out that the problematic jsp file does not have x (execute) permission directly on the file system. Then I `chmod + x some.jsp 'and the problem is resolved.

0
source share

This answer may be obsolete for the above question, but it will help others who are just starting with spring. I also struggled with the 404 WEB-INF / welcome.jsp error.

Here the problem is with importing ModelAndView

Replace

import org.springframework.web.servlet.ModelAndView;

FROM

 import org.springframework.web.portlet.ModelAndView; 
0
source share

Adding this here, just in case someone is facing the same problem. My application worked fine earlier, then I updated the Spring version and in the process also updated the spring-security version. When I tried to access my html login page, I ran into this problem. I made changes to all the necessary configurations due to a change in the spring-security version from 3 to 4. I referenced this link and it was very useful.

I ran into this problem due to the configuration below in web-servlet.xml

 <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 changed both location values ​​to "/app/" . It appears that the newer version of Spring matches either the entire line or checks to see if the requested location starts with the location value above.

I found the code below 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 in my case evaluated false, and therefore the 404 HTTP response

Another tip, if you encounter such an error, try turning on the highest level of spring skating registration. You can find out the cause of the error from there.

0
source share

All Articles