1. 程式人生 > >Thymeleaf 錯誤 以及thymeleafMVC配置,前端程式碼例子

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";
	}

}