【发布时间】:2015-09-18 09:58:18
【问题描述】:
我有一个 Spring Boot Web 应用程序,该应用程序旨在为静态和基于控制器的 (ModelAndView) 页面提供服务。问题是控制器可以提供类似/{string} 的服务,而静态页面必须使用/test 提供服务。
问题是控制器映射优先,我需要避免这种情况。如果用户点击/test,他必须被转发到test.html静态页面。
我尝试用这种方式使用ViewControllerRegistry的order属性,没有成功:
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/test").setViewName("forward:/test.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE); // but I tried with 0 and -1 as well: annotated controllers should have order equals to 0
}
}
这是我的 SpringBootApplication 类:
@SpringBootApplication
public class VipApplication {
public static void main(String[] args) {
SpringApplication.run(VipApplication.class, args);
}
}
这是控制器代码:
@Controller
public class VipController {
@RequestMapping(value = "/{string}")
public ModelAndView vip(@PathVariable("string") String string) {
ModelAndView mv = new ModelAndView("mypage");
return mv;
}
}
如何重新排序映射以确保在带注释的控制器之前考虑静态页面?
【问题讨论】:
标签: spring spring-mvc spring-boot