preface
This project uses the thymeleaf template in the springboot project. The main error is that the front-end HTML page uses thymeleaf to obtain the value passed from the background. There is a problem (the value cannot be obtained, or it is empty and null)
Error message
1. error 1: the forwarded is used as 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) * redirection cannot be used here: otherwise, the front-end data cannot be obtained through thymeleaf
2. error 2: null value error
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field ‘records’ cannot be found on null
Solution:
1. The first error is that when the data queried in the background is sent to the front-end HTML page, there is no corresponding value, and redirection cannot be used, otherwise the front-end data cannot be obtained through thymeleaf. Remove redirect directly and return “main” directly “;
2. The second error is somewhat similar to the first one. This error can find the corresponding data in the background on the page, but the paging data cannot be obtained without corresponding request. If you enter this page directly from the login interface, it is an error because the data of another requested page paging query is used, and there is no query directly The paging data is null. Therefore, you can only send a request to query the page paging data before it can successfully reach the main page.
Corresponding part code
1. Front end code main.html
<!--Employee data table display-->
<div class="container table-responsive" style="margin-top: 50px">
<table class="table table-bordered">
<caption class="caption"><a th:href="@{/showemp}">Display employee information</a> </caption>
<thead>
<tr>
<th>ID</th>
<th>ID</th>
<th>Name</th>
<th>Password</th>
<th>operate</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)
* Redirection cannot be used here: otherwise the front-end data cannot be obtained through 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
* It is wrong to enter this page directly from the login interface, because the page paging query data requested by showemp is used here, and there is no query when entering directly, and the page paging data is null.
* So you can only successfully reach the main page by sending a showemp request to query the page paging data.
*/
// return "redirect:/main";
return "redirect:/showemp";
}
return "login";
}
Read More:
- Template argument deduction/substitution failed: couldn‘t deduce template parameter [How to Solve]
- Vue Error compiling template: Component template should contain exactly one root element. If you
- [Solved] org.thymeleaf.exceptions.TemplateInputException: Error resolving template [currentUserCartItems],
- Template cannot be rendered due to the joint query of populate in mongoose: syntax error: unexpected token r in JSON at position 0
- [Solved] Vue Error: Failed to mount component: template or render function not defined
- [Solved] Vue3 Eslint Error: The template root requires exactly one element
- Template Render Error Solution (Hexo blog theme, GIT upload)
- [Solved] An error occurred while processing your request…enable the Development environment by setting …
- Vue+TS main.ts error: unused expression, expected an assignment or function call
- Android studio reports an error when running the main() method
- [Solved] ERROR SparkContext: Error initializing SparkContext. org.apache.spark.SparkException: Could not pars
- IDEA reports an error Could not reserve enough space for xxxxKB object heap processing
- [Solved] .\main.go:3:6: missing function body .\main.go:4:1: syntax error: unexpected semicolon or newline be
- [Solved] AS Run main() Error: Could not create task ‘:app:CreateOperatorDemo.main()‘
- [Solved] ASP Website Error: An error occurred on the server when processing the URL.
- [Solved] appium Error: An unknown server-side error occurred while processing the command
- jmeter Execute to create Chart Error: An error occurred: Error while processing samples: Mismatch between ……
- The SDK of Android webrtc compiled with Ninja – C out / release command reported an error, and the Android NDK processing method could not be found
- An error was reported when Maven package was running the packaged jar package: there is no main list attribute in xxx.jar, which can be solved by configuring Maven plugin