【问题标题】:Why Spring Boot forces me use Thymeleaf templates为什么 Spring Boot 强迫我使用 Thymeleaf 模板
【发布时间】:2017-10-15 09:10:14
【问题描述】:

我尝试在我的 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.)

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

【问题讨论】:

  • 您是如何定义对 .html 文件的路径引用的??
  • @Afridi 我的项目中没有任何 .html 文件我只有 .jsp 文件

标签: java spring spring-mvc spring-boot


【解决方案1】:

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

@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/”是你的文件目录

【讨论】:

  • 我在帖子中添加了登录映射和配置。我看不出您和我的配置/映射之间有任何根本区别。我尝试像您一样更改控制器方法(使用 ModelAndView 而不是 String),但我仍然遇到相同的错误(((
  • 您是如何定义对 .html 文件的路径引用的??
  • 我的项目中没有任何 .html 文件 我只有 .jsp 文件
  • 但是之后我还有一个问题,Thymeleaf引擎不解析jsp格式org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup
  • @DmitryOleinik 默认情况下 Thymeleaf 仅支持 html 文件。但是看看这篇文章,可能对你有帮助:stackoverflow.com/questions/28480223/…
【解决方案2】:

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

【讨论】:

  • 这是我的第一个配置。不幸的是,它不起作用。在这种情况下,我有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.)
猜你喜欢
  • 2020-11-16
  • 2016-01-26
  • 2012-05-02
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 2019-04-11
  • 2016-12-28
  • 2020-03-14
相关资源
最近更新 更多