Thymeleaf 错误 以及thymeleafMVC配置,前端代码例子
来源:互联网 发布:威海孙小乔淘宝店 编辑:程序博客网 时间:2024/06/04 19:32
springMVC java配置文件 配置使用 thymeleaf 的时候一直报 这个错:
The type org.thymeleaf.ITemplateEngine cannot be resolved. It is indirectly referenced from required .class files
还以为是 引用的JRE版本太低或者太高 试了很久也没用,后来发现是由于 缺少jar包引起的
缺少:thymeleaf-3.0.3.RELEASE, 使用thymeleaf 需要thymeleaf-3.0.3.RELEASE 和thymeleaf-spring4-3.0.3.RELEASE.jar 两个jar包
可见:不只classNotFound 错误是可能缺少jar包,错误引用也可能是缺少jar包
看spring4 in action 根本没说需要什么bean,碰了很多钉子 最终得出 需要的thyleaf jar 包为:
thymeleaf-3.0.3.RELEASE 、thymeleaf-spring4-3.0.3.RELEASE.jar、attoparser-2.0.0.RELEASE.jar、unbescape-1.0.jar
mvc java 配置代码:
package config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.context.ContextLoader;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.thymeleaf.TemplateEngine;import org.thymeleaf.spring4.SpringTemplateEngine;import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;import org.thymeleaf.spring4.view.ThymeleafViewResolver;import org.thymeleaf.templatemode.TemplateMode;import org.thymeleaf.templateresolver.ITemplateResolver;import org.thymeleaf.templateresolver.ServletContextTemplateResolver;@EnableWebMvc@ComponentScan(basePackages="controller")@Configurationpublic class MvcConfig extends WebMvcConfigurerAdapter { @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setTemplateMode("HTML5"); templateResolver.setPrefix("/WEB-INF/thymeleaf/"); templateResolver.setSuffix(".html"); templateResolver.setCharacterEncoding("utf-8"); templateResolver.setOrder(1); templateResolver.setCacheable(false); return templateResolver; } @Bean public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; } @Bean // public ViewResolver viewResolver() { public ThymeleafViewResolver viewResolver(TemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine); viewResolver.setCharacterEncoding("utf-8"); return viewResolver; } }
前端例子代码:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style type="text/css">label.error{color:red;}input.error{background:yellow}</style></head> <body> //th:object 代表命令对象也就是表单要绑定的对象,跟spring form表单绑定标签中commandName 绑定对象类似 //当请求跳转到该页面需要相应的传一个带有key为user的对象,key默认为该对象类型的小写字母开头 //th:action 代表要提交的表单链接, url格式为 @{} <form th:object="${user}" method="POST" th:action="@{/register}"> //th:if 用来判断表单是否验证有误,有错误则显示出来无错则不显示 <div th:if="${#fields.hasErrors('*')}"> <ul> //th:each遍历错误并通过ul列表显示出来 <li th:each="err:${#fields.errors('*')}" th:text="${err}">输入有误</li> </ul> </div>//判断该字段是否验证有误,有错误则显示相应的css,该css可自定义 <label th:class="${#fields.hasErrors('username')}?'error'">用户名</label> <input type="text" th:field="*{username}" th:class="${#fields.hasErrors('username')}?'error'">//th:if 判断如果该字段验证错误,则展示span标签及错误信息,如果无错则不展现 <span th:if="${#fields.hasErrors('username')}" th:text="${#fields.errors('username')}"></span> <label th:class="${#fields.hasErrors('password')}?'error'">用户名</label> <input type="text" th:field="*{password}" th:class="${#fields.hasErrors('password')}?'error'"> <input type="submit" value="提交"/> </form> </body> </html>
后台代码controller:
@Controllerpublic class ThyController {@RequestMapping("/toleaf")public String tleaf(Model model){//向前台添加一个命令对象 //默认key为该类型User->小写字母user //也就是表单中th:object=${user}model.addAttribute(new User());return "tleaf";}@RequestMapping("/register")public String register(@Valid User user,Errors errors){System.out.println(user.getUsername());if(errors.hasErrors()){return "tleaf";}return "success";}}
1 0
- Thymeleaf 错误 以及thymeleafMVC配置,前端代码例子
- thymeleaf例子
- spring boot使用thymeleaf模板编写前端程序-文件路径和代码复用
- spring mvc 配置 thymeleaf
- SpringMVC 配置Thymeleaf模板
- spring 配置thymeleaf
- SpringMVC配置Thymeleaf
- Thymeleaf小小配置
- perl 常用函数以及例子代码
- Spring的AOP配置以及常见错误(内附代码)
- SpringBoot+SpringSecurity+thymeleaf 代码实现
- Spring 集成 Quartz 配置以及例子
- JMS简介以及WebLogic配置JMS例子
- OSG的配置以及两个例子
- cordova环境配置以及简单例子创建
- Spring MVC + thymeleaf 非配置
- Springboot+Thymeleaf配置与使用
- ssm 下 前端ajax 访问后台 以及出现的错误
- 缩略词汇整理
- postgresql中如何新增加一个SysCache
- Integer.decode()和Intger.valueof()
- seaJS模块化开发
- leetcode-22-Generate Parentheses
- Thymeleaf 错误 以及thymeleafMVC配置,前端代码例子
- Visual Studio 2015 WPF增强:调试查看UI布局,Xaml修改tag更智能
- android:获取手机号码和姓名实现通讯录
- 《深度探索C++对象模型》第二章 构造函数语意学
- Threes.js入门篇之2
- this指针
- iOS 环信集成问题(连文档都不说明的坑。。)
- 被虐日記----开心并快乐着的求各种虐
- MySQL——表结构操作