Spring MVC did not detect my Controller
I have a Spring MVC project with the following files:
/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appconfig-root.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
There are also 2 configuration files:
/src/main/webapp/WEB-INF/appconfig-root.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="appconfig-mvc.xml"/>
<context:component-scan base-package="yc.servlets"/>
<!--<context:property-placeholder location="classpath:application.properties"/>-->
</beans>
/src/main/webapp/WEB-INF/appconfig-mvc.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
and the class that contains my Controller:
package yc.servlets;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestPage {
@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
public String hello() {
return "hello";
}
}
I
thought this was enough for Spring MVC to work, but when I type localhost:8888
in my browser, I get a 404
error.
localhost:8888/hello_there
also gives a 404
error.
Solution
Hello, you are returning “hello” in the Controller and it will find the hello .jsp
If it can’t find it, it throws a 404 HTTP error
When you configure InternalViewResolver:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
It tries to look in /WEB-INF/jsp/hello.jsp, so when you return the string “hello” in the controller:
@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
public String hello() {
return "hello";
}
It will add a suffix “.jsp” to “hello”, and it will try to find “hello.jsp” because if it can’t find it it will throw a 404
You can change the @Controller to @RestController, which implicitly adds a @ResponseBody comment, and see if it returns “hello” in the response
Also, if this doesn’t help you, you can change it:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
to this:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
If it still works now, try the next step:
Try this URL:
localhost:8080/yourApplicationName/hello_there
The default conext-path here is your application name
If you go to TOMCAT_HOME/webapps/
You will find that there is no app under the root directory
That’s why localhost:8080/… The reason why it can’t be found
You can resolve this issue in two ways:
1.Find your application deployed and copy to root folder in
TOMCAT_HOME/webapps/ROOT
2.See the name of your application in TOMCAT_HOME/webapps/
and call url : localhost:8080/yourAppName/hello_world
Thanks