简体   繁体   English

找不到spring 404控制器

[英]spring 404 controller not found

I have a problem with spring, because I receive 404 error not found. 我有弹簧问题,因为我收不到404错误。

My web.xml 我的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 also mvc-dispatcher.xml file and my controller 我还有mvc-dispatcher.xml文件和我的控制器

<?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 my controller 和我的控制器

@Controller
public class TestController {

@RequestMapping(value="/test", method = RequestMethod.GET)
public @ResponseBody
ResponseEntity<Boolean> create() {
    return new ResponseEntity<>(true, HttpStatus.OK);
}

}

When I do query like localhost:8080/PROJECT_NAME/test I got 404 not found error. 当我像localhost:8080 / PROJECT_NAME / test一样查询时,我找不到404错误。 localhost:8080/PROJECT_NAME returns 200 status code What is wrong ? localhost:8080 / PROJECT_NAME返回20​​0状态代码有什么问题?

You don't need to mention context root in url-pattern. 您不需要在url-pattern中提及上下文根。 url-pattern should be / , which means any/every URL/request sholuld be served by dispatcher servlet. url-pattern应为/ ,这意味着任何/每个URL /请求都应由调度程序servlet提供。

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

as you have mentioned url-pattern as /PROJECT_NAME/* , which means any URL in the form http://localhost:8080/PROJECT_NAME(Context_Root)/PROJECT_NAME/Anything will be forwarded to dispatcher servlet and that's why localhost:8080/PROJECT_NAME/PROJECT_NAME/test is working fine. 正如您所提到的url-pattern为/PROJECT_NAME/* ,这意味着http://localhost:8080/PROJECT_NAME(Context_Root)/PROJECT_NAME/Anything形式的任何URL都将被转发到调度程序servlet,这就是localhost:8080/PROJECT_NAME/PROJECT_NAME/test的原因localhost:8080/PROJECT_NAME/PROJECT_NAME/test工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM