Dmitry Oleinik
Dmitry Oleinik

Reputation: 317

Why Spring Boot forces me use Thymeleaf templates

I have tried go to login page in my Spring boot app and I got this error

Circular view path [/login.jsp]: would dispatch back to the current handler URL [/login.jsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

On stackoverflow people give advice that need add thymeleaf dependency

('org.springframework.boot:spring-boot-starter-thymeleaf')

but after that I got this error

There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers

this error indicates that Spring can't find template login.html in /resurses/templates folder. What I should do if I have my own login.jsp in another folder and I don't want use any templates?

This is my login mapping

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    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";
    }

and this is my Security config

 @Override
 protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                    .antMatchers("/resources/**", "/registration").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .loginProcessingUrl("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
 }

Upvotes: 0

Views: 2125

Answers (2)

José Mendes
José Mendes

Reputation: 990

Spring-Boot does not force you to use, it encourages you.

You can use .jsp pages if you want, to do this you need to:

1 - Add this in your MainClass.java

@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainClass.class);
    }

2 - Add this in your application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

3 - Put your .jsp pages in the mapped folder and you can use .jsp instead of thymeleaf.

Upvotes: 1

Afridi
Afridi

Reputation: 6932

Simple, just create your own mapping for your login page in your controller like:

@Controller
public class AppController{

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(@RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout, Model model, HttpServletRequest request) {

        ModelAndView view = new ModelAndView();
        if (error != null) {
            view.addObject("error", "Invalid username and password!");
        }

        if (logout != null) {
            view.addObject("msg", "You've been logged out successfully.");
        }

        view.setViewName("your-login-page");  
        return view;
    }
}

And configuration should look like so:

    @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests().and()
.formLogin().loginPage("/login").loginProcessingUrl("/login").failureUrl("/login?error")
        }

And application.properties files should look like so:

spring.thymeleaf.suffix=.your-file-type
spring.thymeleaf.prefix=/WEB-INF/jsp-pages/

where "/WEB-INF/jsp-pages/" is your files directory

Upvotes: 2

Related Questions