When requeste to the controller gives 404 error page not found. We used the example from http://docs.spring.io/ and other examples. The error is the same.

The following are the files: web.xml, * -servlet.xml, controller.

Web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.0" metadata-complete="false"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/golfing-servlet.xml</param-value> </context-param> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/golfing/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 

gorfling.servlet.xml

 <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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:default-servlet-handler/> <context:component-scan base-package="com.vlad.home.controllers.*" /> <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/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 

controller

 @Controller @RequestMapping("/") public class TestController { @RequestMapping(value = "/test", method = RequestMethod.GET) public ModelAndView render() { System.out.println("WAS CALLED CONTROLLER METHOD render()"); ModelAndView modelAndView = new ModelAndView("test"); return modelAndView; } @RequestMapping(value = "/*") public ModelAndView others() { System.out.println("WAS CALLED CONTROLLER METHOD others()"); ModelAndView modelAndView = new ModelAndView("test"); return modelAndView; } } 

Below is the output from the tomcata console.

 мар 15, 2016 5:57:26 PM org.apache.catalina.startup.TldConfig execute INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. мар 15, 2016 5:57:26 PM org.springframework.web.context.ContextLoader initWebApplicationContext INFO: Root WebApplicationContext: initialization started мар 15, 2016 5:57:26 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh INFO: Refreshing Root WebApplicationContext: startup date [Tue Mar 15 17:57:26 EET 2016]; root of context hierarchy мар 15, 2016 5:57:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/golfing-servlet.xml] мар 15, 2016 5:57:27 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' мар 15, 2016 5:57:27 PM org.springframework.web.context.ContextLoader initWebApplicationContext INFO: Root WebApplicationContext: initialization completed in 328 ms мар 15, 2016 5:57:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean INFO: FrameworkServlet 'golfing': initialization started мар 15, 2016 5:57:27 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh INFO: Refreshing WebApplicationContext for namespace 'golfing-servlet': startup date [Tue Mar 15 17:57:27 EET 2016]; parent: Root WebApplicationContext мар 15, 2016 5:57:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/golfing-servlet.xml] мар 15, 2016 5:57:27 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' мар 15, 2016 5:57:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean INFO: FrameworkServlet 'golfing': initialization completed in 76 ms 

I try to find a mistake all day, I will be very grateful for the help.

  • Another would be an error log. - Vartlok
  • And where to see it ?. There is no error in the browser 404 page not found. - JAVAvladuxa
  • Where you took the output from the tomkata console. - Vartlok
  • There are no errors in the console. Browser error: i.stack.imgur.com/7kgTZ.png Idea output console: i.stack.imgur.com/YF0Rt.png - JAVAvladuxa
  • It may help "@ResponseBody" in the controller. The request from the client is sent just from the browser command line? - Vitaliy

2 answers 2

Try removing RequestMapping("/") from the controller. @RequestMapping at the controller level is needed, in order to add a prefix to all other @RequestMapping at the method level of this controller. For example, if you have @RequestMapping("/foo") at the controller level, and @RequestMapping("/bar") at the method level, then the URL path to the method will be / foo / bar.

If you don’t write @RequestMapping at the controller level at all, then the URL path to the same method will be / bar.

If you fail, swap <url-pattern>/golfing/*</url-pattern> to <url-pattern>/golfing/</url-pattern>

    Also, I would replace

      <context:component-scan base-package="com.vlad.home.controllers.*" /> 

    on

      <context:component-scan base-package="com.vlad.home.controllers" />