0

I'm working using Spring Boot and having the error: There was an unexpected error (type=Not Found, status=404)

My .jsp pages are on the folder src>main>webapp>WEB-INF>views

application.properties:

spring.mvc.view.prefix:/WEB-INF/views/
spring.mvc.view.suffix:.jsp
spring.messages.basename=validation

Controller:

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @GetMapping("/registration")
    public String registration(Model model) {
        model.addAttribute("userForm", new User());

        return "registration";
    }

    @PostMapping("/registration")
    public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult) {
        userValidator.validate(userForm, bindingResult);

        if (bindingResult.hasErrors()) {
            return "registration";
        }

        userService.save(userForm);

        securityService.autoLogin(userForm.getUsername(), userForm.getPasswordConfirm());

        return "redirect:/welcome";
    }

    @GetMapping("/login")
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("error", "Your username and password is invalid.");

        if (logout != null)
            model.addAttribute("message", "You have been logged out successfully.");

        return "login";
    }

    @GetMapping({"/", "/welcome"})
    public String welcome(Model model) {
        return "welcome";
    }
}

But it still can't find the pages. What should I do?

2 Answers2

1

Please try with these changes:

  1. Put a forward slash at the end: spring.mvc.view.prefix:/WEB-INF/views/
  2. Change @RestController to @Controller
A Baldino
  • 178
  • 1
  • 11
0

Here are 2 things which you are doing wrong.

1.RestController returns reponse in JSON by default but you want to return JSP page so change it to controller annotation see difference-between-spring-controller-and-restcontroller-annotation.

@Controller

2.Try appending slash before after views like below.

spring.mvc.view.prefix:/WEB-INF/views/
Alien
  • 15,141
  • 6
  • 37
  • 57