i have problem spring, because receive 404 error not found.
my web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/project_name/*</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/mvc-dispatcher-servlet.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> </web-app>
i have mvc-dispatcher.xml file , controller
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config/> <mvc:annotation-driven/> <context:component-scan base-package="com.project_name.web.controllers"/> </beans>
and controller
@controller public class testcontroller { @requestmapping(value="/test", method = requestmethod.get) public @responsebody responseentity<boolean> create() { return new responseentity<>(true, httpstatus.ok); } }
when query localhost:8080/project_name/test got 404 not found error. localhost:8080/project_name returns 200 status code wrong ?
you don't need mention context root in url-pattern. url-pattern should /
, means any/every url/request sholuld served dispatcher servlet.
<servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
as have mentioned url-pattern /project_name/*
, means url in form http://localhost:8080/project_name(context_root)/project_name/anything
forwarded dispatcher servlet , that's why localhost:8080/project_name/project_name/test
working fine.
Comments
Post a Comment