Я читаю следующую статью:
Предоставляется конфигурация сопоставления:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("myhome");
registry.addViewController("/hello").setViewName("helloworld");
registry.addRedirectViewController("/home", "/hello");
registry.addStatusController("/detail", HttpStatus.BAD_REQUEST);
}
Автор пишет:
- When we access URL /hello then helloworld.jsp will run.
- When we access URL /home then helloworld.jsp will run because it will be redirected to the URL /hello.
- When we access URL /detail then we will get header response with 400 HTTP status code.
Таким образом, линия
registry.addViewController("/home").setViewName("myhome");
бесполезно?
Также в другом руководстве я нашел следующую конфигурацию:
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/")
.setViewName("forward:/index");
registry.addViewController("/index");
registry.addViewController("/securedPage");
}
registry.addViewController("/")
.setViewName("forward:/index");
Эта линия ясна. Это означает, что если я попытаюсь получить доступ к корневому URL-адресу, приложение будет повторно запускать index.jsp
Но в чем смысл
registry.addViewController("/index");
registry.addViewController("/securedPage");
?




Да, перенаправление перезапишет исходное сопоставление, которое было
registry.addViewController("/home").setViewName("myhome");
Что касается контроллеров представления без явного задания имени представления, они будут (пытаться) показать index.jsp и securedPage.jsp соответственно.
Из ViewControllerRegistration.setViewName(String viewName):
Set the view name to return. Optional.
If not specified, the view controller will return
nullas the view name in which case the configuredRequestToViewNameTranslatorwill select the view name. TheDefaultRequestToViewNameTranslatorfor example translates "/foo/bar" to "foo/bar".
Таким образом, RequestToViewNameTranslator преобразует /securedPage в securedPage, а ViewResolver (в данном случае для JSP InternalResourceViewResolver) затем найдет JSP на основе этого.