SpringMvc4.x---快捷的ViewController

时间:2022-12-01 23:26:56
@RequestMapping("/index")
public String hello(){ return "index";
}

此处无任何的业务处理,只是简单的页面跳转,写了至少三行有效的代码,在实际的开发中会涉及大量这样的页面转向,若都这样写会特别的麻烦,我们通过在配置类MyMvcConfig里通过重写addViewControllers来简化配置:

public class MyMvcConfig extends WebMvcConfigurerAdapter {

......... @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("/index");
    registry.addViewController("/toUpload").setViewName("/upload");
    registry.addViewController("/converter").setViewName("/converter");
} }