What does the addViewControllers method of the Spring boot WebMvcConfigurer class do?

0

I'm using thymeleaf on the front end of the application and I have the following configuration class, with the addViewControllers method in question:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    ...

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/admin/home").setViewName("adminhome");
        registry.addViewController("/user/perfil/{email}").setViewName("userperfil");
        registry.addViewController("/home").setViewName("home");
        //registry.addViewController("/403").setViewName("403");  
       } 
         ...
    }

I was thinking that it would be responsible for modifying the urls shown in the browser bar, but did not do so. So I do not know what he does ..

I tested this url in one of the application's controllers, for example:

@Controller
@RequestMapping("user")
public class UserController {

    private static final Logger LOG = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

...

    //metodo bastante simplificado..
    @RequestMapping(value = "/perfil/{email}", method = RequestMethod.GET)
    public ModelAndView perfil(@PathVariable("email") String email) {

        ModelAndView view = new ModelAndView();

            User user = userService.findByEmail(email);

                view.addObject("user", user);

                view.setViewName("usuario/perfil");
                LOG.info("Metodo perfil");
                return view;

    }

    ...
}

So in the addViewControllers method I put:

registry.addViewController("/user/perfil/{email}").setViewName("userperfil");

imagining that in the browser it would look like this:

http://localhost:8080/userperfil

instead of:

http://localhost:8080/user/perfil/[email protected]

Besides what would be possible to define these "edited" urls?

    
asked by anonymous 07.09.2018 / 01:31

1 answer

0

addViewController is responsible for mapping the path while setViewName is responsible for mapping the jsp to be loaded.

You can also use this method for redirects, for example:

registry.addRedirectViewController("/home", "/user/perfil/{email}");

In the example above when trying to access link it would redirect to link

More information:

Example spring 4 mvc by Arvind Rai

    
07.09.2018 / 02:09