thymeleaf.TemplateEngineException processing template “main“: An error happened during template pars

前言

这个项目是springboot 项目使用thymeleaf模板的,主要错误是前端HTML页面使用thymeleaf获取后台传过来的值有问题(获取不到,或者为空null)

错误信息

1、第一个错误:转发的用成了重定向 redirect:/main

2021-11-18 21:02:01.321 ERROR 9272 — [nio-8082-exec-4] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8082-exec-4] Exception processing template “main”: An error happened during template parsing (template: “class path resource [templates/main.html]”)
*
* org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: “class path resource [templates/main.html]”)
* at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
* Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: “page.getCurrent()” (template: “main” - line 86, col 17)
* 这里不能使用重定向:否则前端数据不能通过thymeleaf获取

2、第二个错误:null值错误

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field ‘records’ cannot be found on null

解决办法

1、第一个错误是我后台查询的数据发往前端HTML页面的时候,没有对应的值,不能使用重定向,否则前端数据不能通过thymeleaf获取。直接去掉 redirect,直接 return “main";
2、第二个错误,和第一个有点相似,这个错误,页面可以找到后台相应的数据,但是没有经过相应的请求,获取不到分页数据。如果直接从登录界面进入这个页面是错误的,因为这里用到了另外一个请求的page分页查询的数据,直接进入是没有查询的,page分页数据为null。所以只能先发送那一个请求查询到page分页数据才能成功的到达main页面。

对应的部分代码

1、前端代码main.html

<!--员工数据表格显示-->
<div class="container table-responsive" style="margin-top: 50px">
    <table class="table table-bordered">
        <caption class="caption"><a th:href="@{/showemp}">显示员工信息</a> </caption>
        <thead>
            <tr>
                <th>序号</th>
                <th>ID</th>
                <th>Name</th>
                <th>Password</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="book,statu:${page.records}">
                <th>[[${statu.count}]]</th>
                <td th:text="${book.id}"></td>
                <td>[[${book.name}]]</td>
                <td>[[${book.password}]]</td>
                <td>
                    <a th:href="@{/delete}" type="button" class="btn btn-danger btn-sm">delete</a>
                    <a th:href="@{/update}" type="button" class="btn btn-info btn-sm">update</a></td>
            </tr>
        </tbody>
    </table>
</div>

2、CrudController.java

@Controller
public class CrudController {
    
    
   @Autowired
   BookServiceImpl bookService;

   @GetMapping("/showemp")
   public String selectAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                           Model model){
    
    
       Page<Book> page = new Page<>(pn,2);
       Page<Book> bookPage = bookService.page(page, null);
       model.addAttribute("page",bookPage);
       System.out.println(bookPage.getCurrent());
       System.out.println(bookPage.getRecords().size());
       /**
        * 2021-11-18 21:02:01.321 ERROR 9272 --- [nio-8082-exec-4] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8082-exec-4] Exception processing template "main": An error happened during template parsing (template: "class path resource [templates/main.html]")
        *
        * org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/main.html]")
        * 	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
        * Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "page.getCurrent()" (template: "main" - line 86, col 17)
        * 这里不能使用重定向:否则前端数据不能通过thymeleaf获取
        */
//        return "redirect:/main";
       return "main";
   }
}

3、LoginController.java

@PostMapping("/login")
    public String LoginValidation(Book book,
                                  RedirectAttributes redirectAttributes){
    
    
        String name = book.getName();
        String password = book.getPassword();
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("password",password);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("name",name );
        queryWrapper.ge("password",password);
        Book one = bookService.getOne(queryWrapper);
        if (one.toString()!=null){
    
    
            /**
             * org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'records' cannot be found on null
             * 如果直接从登录界面进入这个页面是错误的,因为这里用到了showemp这个请求的page分页查询的数据,直接进入是没有查询的,page分页数据为null
             * 所以只能先发送showemp请求查询到page分页数据才能成功的到达main页面。
             */
//            return "redirect:/main";
            return "redirect:/showemp";
        }
        return "login";
    }

Guess you like

Origin blog.csdn.net/qq_43987149/article/details/121411860