[英]Redirect does not work in Spring Boot
I have a controller, that I simply want it to redirect the request to another page. 我有一个控制器,我只是希望它将请求重定向到另一个页面。 However instead of redirecting, it prints out redirect:/test.html
. 但是,它不会重定向,而是打印出redirect:/test.html
。
I have tried to access test.html
manually, and it works fine through localhost:8080/test.html
我尝试手动访问test.html
,并且可以通过localhost:8080/test.html
Here is the controller code: 这是控制器代码:
@RequestMapping("/test")
private String entry(){
return "redirect:/test.html";
}
Add this annotation to the main class @SpringBootApplication
it will autoconfigure your Spring Boot application. 将此注释添加到主类@SpringBootApplication
,它将自动配置您的Spring Boot应用程序。 The example 这个例子
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
The Spring documentation says Spring文档说
Many Spring Boot developers always have their main class annotated with
@Configuration
,@EnableAutoConfiguration
and@ComponentScan
. 许多Spring Boot开发人员的主类始终带有@Configuration
@EnableAutoConfiguration
,@ComponentScan
@EnableAutoConfiguration
和@ComponentScan
注释。 Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient@SpringBootApplication
alternative. 由于这些批注经常一起使用(特别是如果您遵循上述最佳实践),因此Spring Boot提供了一种方便的@SpringBootApplication
替代方案。
You can find working example and tutorial that uses redirection from the product controller here . 您可以在此处找到使用重定向的工作示例和教程。
Config a InternalResourceViewResolver 配置一个InternalResourceViewResolver
@Bean
InternalResourceViewResolver viewResolverRegistry() {
return new InternalResourceViewResolver();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.