Tengo un proyecto Spring MVC con los siguientes archivos:
/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>
Y 2 archivos de configuración:
/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>
Y la clase que contiene mi controlador:
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";
}
}
Creo que esto es suficiente para conseguir un trabajo MVC primavera, pero cuando escribo localhost:8888
en el navegador me sale un 404
error.
localhost:8888/hello_there
También da 404
error.
Hola va a devolver "hola" en el controlador y su va a encontrar hello.jsp Si no encuentra arrojará error HTTP 404
Como se ha configurado el 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>
Se está tratando de encontrar en /WEB-INF/jsp/hello.jsp, por lo que cuando regrese cadena "Hola" en el controlador:
@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
public String hello() {
return "hello";
}
Se añadirá un sufijo ".jsp" a 'hola' y tratará de encontrar 'hello.jsp', ya que no pudo encontrar que se va a tirar 404
Puede cambiar de su @Controller a @RestController que añade anotación implícitamente @ResponseBody y ver si se devuelve "hola" en respuesta
Además, si esto no ayuda puede cambiar:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
a éste:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Si sus try sigue trabajando ahora los siguientes pasos:
Tratar tales url:
localhost:8080/yourApplicationName/hello_there
Conext por defecto de la ruta es su nombre de la aplicación que aquí Si usted va a TOMCAT_HOME / webapps /
se encuentra que en la raíz de la carpeta no es su aplicación
Así que por eso no podía encontrados por localhost: 8080 / ...
Puedes solucionar este problema de dos maneras:
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
Gracias