Thymeleaf 错误 以及thymeleafMVC配置,前端代码例子

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")
@Configuration
public 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:

@Controller
public 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";
	}

}






  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值