简体   繁体   English

Spring Controller请求映射无法正常工作

[英]Spring Controller Request Mapping Does Not Work Correctly

I have a simple request mapping that opens up an html file residing in static folder. 我有一个简单的请求映射,可打开驻留在静态文件夹中的html文件。 It works correctly when the request mapping is a one part request like '/town' but does not works (returns 404) on urls like '/visual/town' . 当请求映射是像'/town'这样的单部分请求,但不能在'/visual/town'这样'/visual/town' URL上工作(返回404)时,它可以正常工作。 When I debug, the program falls to the desired place though. 当我调试时,程序会掉到所需的位置。 It just cannot recognise the file that it points to when the mapping constitutes of two or more parts. 当映射由两部分或更多部分组成时,它只是无法识别它指向的文件。 This doesn't makes sense to me at all. 这对我完全没有意义。

This one works : 这个作品:

@Controller("VisualHomeController")
public class HomeController {

    @RequestMapping(value = "/town")
    public String emergentTown() {
        return "static/visual/emergent_town.html";
    }
}

This one does not : 这不是:

@Controller("VisualHomeController")
public class HomeController {

    @RequestMapping(value = "/visual/town")
    public String emergentTown() {
        return "static/visual/emergent_town.html";
    }
}

And here is my servlet mapping, It seems that the problem arises here : 这是我的servlet映射,看来问题出在这里:

<servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

So, I figured out that the problem is with the servlet mapping. 因此,我发现问题出在servlet映射上。

I needed to add another servlet mapping for the first part of my additional controller which is /visual as : 我需要为附加控制器的第一部分添加另一个servlet映射,该映射是/visual

<servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/visual/*</url-pattern>
</servlet-mapping>

Then it worked. 然后它起作用了。 So whenever you want to have requests like /.../new_page you need to define the ... as servlet mappings. 因此,每当您想要像/.../new_page请求时,都需要将...定义为servlet映射。 Note that changing the main servlet mapping to /* is not enough for this purpose. 注意,仅将主servlet映射更改为/*不足以实现此目的。

Note : It still doesn't make sense that why the program falls into the return ... when debugging. 注意:调试时为什么程序会陷入return ...仍然没有意义。 I mean, if one more servlet mapping is needed, how front controller can route the request to that method? 我的意思是,如果还需要一个servlet映射,那么前端控制器如何将请求路由到该方法? And while doing so why it can not resolve the view? 并且在这样做时为什么它无法解析视图?

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

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