循环视图路径错误,Spring MVC [英] Circular View Path Error, Spring MVC

查看:30
本文介绍了循环视图路径错误,Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做教程 -> http://spring.io/guides/gs/serving-web-content/

I'm trying to do the tutorial -> http://spring.io/guides/gs/serving-web-content/

当我运行它时,它显示 Circular View Path[greeting],为什么?

When I run it, it says Circular View Path[greeting], why?

在本教程中,我不明白的一件事是以下内容的作用及其工作原理:

In the tutorial, one thing i dont understand is what the following does and why it works:

return "greeting";

代码片段:

package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {
    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}

推荐答案

您可能在学习本教程时跳过了一个步骤.

You might have skipped a step while following the tutorial.

我会解释为什么你会看到你看到的行为,你可以决定之后做什么.

I'll explain why you get the behavior you are seeing and you can decide what to do afterwards.

您可能启动了应用程序

SpringApplication.run(Application.class, args);

在教程 Application 类中的 main 方法.默认情况下,由于 @EnableAutoConfiguration(以及类路径上的其他组件),将为您注册一个 DispatcherServlet,它提供一个默认的 UrlBasedViewResolver不会为它解析的视图设置 prefixsuffix.

in the tutorial Application class' main method. By default, because of @EnableAutoConfiguration (and other components on the classpath), a DispatcherServlet will be registered for you which provides a default UrlBasedViewResolver that doesn't set a prefix or suffix to the views it resolves.

在您的 @Controller 处理程序方法中,当您这样做时

In your @Controller handler method, when you do

return "greeting";

Spring 将使用 UrlBasedViewResolver 来解析视图名称.在这种情况下,视图名称将只是 greeting.在正常情况下,一旦完成,它将使用 Servlet API 的 HttpServletRequest#getRequestDispatcher(String) 传入该视图名称.该方法返回一个 RequestDispatcher 指向该路径的处理程序.

Spring will use the UrlBasedViewResolver to resolve a view name. In this case, the view name will simply be greeting. In normal cases, once that is done, it will use the Servlet API's HttpServletRequest#getRequestDispatcher(String) passing in that view name. That method returns a RequestDispatcher which points to the handler for that path.

在我们的例子中,在获取 RequestDispatcher 之前,Spring 将比较视图名称(解析为路径)和当前请求的路径.它会发现它们是相等的.换句话说,对 /greeting 的请求将通过将视图返回给 /greeting 来处理,该视图将由相同的 @Controller 处理程序处理方法,这永远.Spring 检测到这一点并告诉你你有一个圆形的视图路径,即.你会无限循环.

In our case, before getting the RequestDispatcher, Spring will compare the view name (which resolves to a path) and the current request's path. It will find that they are both equal. In other words, a request to /greeting will be handled by returning a view to /greeting which will be handled by the same @Controller handler method, and this forever. Spring detects this and tells you that you have a circular view path, ie. you would loop infinitely.

了解 @EnableAutoConfiguration 的工作原理并更改您的配置,以便您可以定义自己的 UrlBasedViewResolverInternalResourceViewResolver 以适当设置前缀和后缀.

Find out how @EnableAutoConfiguration works and change your configuration so that you can define your own UrlBasedViewResolver or InternalResourceViewResolver which sets prefix and suffix appropriately.

您可以在 Spring MVC 官方文档.

You can read more about view name resolution in the official Spring MVC documentation.

这篇关于循环视图路径错误,Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆