Spring MVC - ViewControllerRegistry Examples

Code Snippets Spring MVC 

@SpringBootApplication
public class Main extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers (ViewControllerRegistry registry) {
RedirectViewControllerRegistration r =
registry.addRedirectViewController("/test", "/test2");
//r.setStatusCode(HttpStatus.SEE_OTHER);
//r.setContextRelative(false)
}

public static void main (String[] args) {
SpringApplication.run(Main.class, args);
}
}




@SpringBootApplication
public class Main extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers (ViewControllerRegistry registry) {

//mapping url to a view
ViewControllerRegistration r = registry.addViewController("/test");
r.setViewName("myView");
//setting status code
r.setStatusCode(HttpStatus.GONE);

//mapping another url to a status code
registry.addStatusController("/test2", HttpStatus.SERVICE_UNAVAILABLE);
}

public static void main (String[] args) {
SpringApplication.run(Main.class, args);
}
}
Original Post




    @Override
public void addViewControllers(ViewControllerRegistry registry) {
RedirectViewControllerRegistration r = registry.addRedirectViewController("/test", "/test2");
//r.setStatusCode(HttpStatus.GONE);
r.setStatusCode(HttpStatus.PERMANENT_REDIRECT);
}




    @Override
public void addViewControllers (ViewControllerRegistry registry) {
RedirectViewControllerRegistration r = registry.addRedirectViewController("/test", "test2");
}
@SpringBootApplication
public class Main extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers (ViewControllerRegistry registry) {
RedirectViewControllerRegistration r =
registry.addRedirectViewController("/test", "/test2");
//r.setStatusCode(HttpStatus.SEE_OTHER);
//r.setContextRelative(false)
}

public static void main (String[] args) {
SpringApplication.run(Main.class, args);
}
}
Original Post




    @Override
public void addViewControllers(ViewControllerRegistry registry) {
//this will map uri to jsp view directly without a controller
registry.addViewController("/hi")
.setViewName("hello");
}
Original Post




    @Override
public void addViewControllers(ViewControllerRegistry registry) {
//mapping '/' to index view name without a controller
ViewControllerRegistration r = registry.addViewController("/");
r.setViewName("index");
}
Original Post




See Also