Search code examples
javaspring-bootprefixapplication.propertiessuffix

Spring Boot 2.2.6 - MVC | How to ignore suffix and prefix defined in application.properties


I'm using Spring Boot 2.2.6, I put these properties in application.properties file

spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=WEB-INF/pages/

But sometimes I need to ignore those properties. For example when I want to request "/deconnection"

@Controller
public class UserProfileController {

    @Autowired
    private UserDelete deleteService;

    @RequestMapping({ "/userProfile" })
    public ModelAndView userProfile() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("members/userProfile");
        mv.addObject("page", "userProfile");
        return mv;
    }

    @RequestMapping({ "/deleteUser" })
    public ModelAndView deleteUser(Principal principal) {
        ModelAndView mv = new ModelAndView();
        deleteService.deleteUser(principal.getName());
        if (deleteService.getError() == null) {
            mv.setViewName("/deconnexion");
        } else {
            mv.setViewName("members/userProfile");
            mv.addObject("page", "userProfile");
            mv.addObject("error", deleteService.getError());
        }
        return mv;
    }
}

I don't want those properties to apply. How to solve that ?

Thanks for your help !

:EDIT:

Use

mv.setViewName("redirect:/deconnexion");

to solve.


Solution

  • I suppose you can use redirect, to go to desired url without prefixes. Something like that: https://www.baeldung.com/spring-redirect-and-forward#redirect-with-the-prefix-redirect