简体   繁体   English

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

[英]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 我试图在我的Spring Boot应用中转到登录页面,但出现此错误

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 在stackoverflow上,人们提供需要增加百里香依赖性的建议

('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. 此错误表明Spring在/resurses/templates文件夹中找不到模板login.html。 What I should do if I have my own login.jsp in another folder and I don't want use any templates? 如果我在另一个文件夹中有自己的login.jsp,但又不想使用任何模板,该怎么办?

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

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: 并且application.properties文件应如下所示:

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

where "/WEB-INF/jsp-pages/" is your files directory 其中“ / WEB-INF / jsp-pages /”是您的文件目录

Spring-Boot does not force you to use, it encourages you. Spring-Boot不会强迫您使用,它会鼓励您。

You can use .jsp pages if you want, to do this you need to: 您可以根据需要使用.jsp页面,为此,您需要:

1 - Add this in your MainClass.java 1-在您的MainClass.java添加它

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

2 - Add this in your application.properties 2-在您的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 . 3-将.jsp页面放在映射的文件夹中,您可以使用.jsp代替thymeleaf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM