为什么Spring Boot强迫我使用Thymeleaf模板

时间:2017-05-16 10:25:33

标签: java spring spring-mvc spring-boot

我已经尝试过在我的Spring启动应用中登录页面,我收到了此错误

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.)

在stackoverflow上,人们会提出需要添加百万美元依赖性的建议

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

但之后我收到了这个错误

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

此错误表示Spring无法在/resurses/templates文件夹中找到模板login.html。 如果我在另一个文件夹中有自己的login.jsp并且我不想使用任何模板,我该怎么办?

这是我的登录映射

    @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";
    }

这是我的安全配置

 @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();
 }

2 个答案:

答案 0 :(得分:2)

简单,只需在控制器中为您的登录页面创建自己的映射,例如:

@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;
    }
}

配置应该如下:

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

application.properties文件应如下所示:

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

其中" / WEB-INF / jsp-pages /"是你的文件目录

答案 1 :(得分:1)

Spring-Boot不会强迫您使用,它鼓励您。

如果需要,您可以使用.jsp页面,为此,您需要:

1 - 在MainClass.java

中添加此内容
@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainClass.class);
    }

2 - 在application.properties

中添加此内容
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

3 - 将.jsp个网页放在已映射的文件夹中,您可以使用.jsp代替thymeleaf