[英]Spring boot thymeleaf display of productName
我想顯示我的productName,但是有以下錯誤:
ERROR 10464 --- [nio-8080-exec-6] org.thymeleaf.TemplateEngine
: [THYMELEAF][http-nio-8080-exec-6] Exception processing template
"/productView/productPage": An error happened during template parsing
(template: "class path resource
[templates//productView/productPage.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened
during template parsing (template: "class path resource
[templates//productView/productPage.html]")
@Controller
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("productAdmin")
public String next(Model model){
model.addAttribute("eProduct",new Product());
return "/adminView/productAdmin";
}
@GetMapping("/productPage")
public String productPage(){
return "/productView/productPage";
}
@PostMapping("/saveProduct")
public String save(@ModelAttribute("eProduct") Product product, BindingResult result,
@RequestParam("pathImage") MultipartFile multipartFile ){
String path = System.getProperty("user.home") + File.separator + "projectImages\\";
try {
multipartFile.transferTo(new File(path + multipartFile.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
product.setPathImage("\\images\\" + multipartFile.getOriginalFilename());
productService.save(product);
return "/mainView/index";
}
@GetMapping("/products")
public String products(Model model){
model.addAttribute("products",productService.findAll());
return "/productView/products";
}
@GetMapping("/product-{id}")
public String productPage(@PathVariable("id") int id, Model model){
Product product = productService.findOne(id);
model.addAttribute("product",product);
return "/productView/productPage";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
Product Page
<p><span th:text="${product.productName}"/></p>
</body>
</html>
但是我沒有這個問題的原因。 在春天我寫
$ {} product.productName
而且我的代碼運行良好。但是在這種情況下,我不明白自己做錯了什么。 您能幫我解決這個問題嗎? 因為我不知道接下來要做什么,所以我嘗試自己動手,但沒有成功。謝謝。
檢查模板的語法,可能您缺少結束標記
我發現了您的錯誤,在您上傳到注釋中的日志中,我發現該錯誤的原因如下:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "product.id" (template: "productView/productPage" - line 10, col 4)
這樣一來,您的產品模型沒有該字段的吸氣劑的提示,您沒有為該字段使用正確的名稱,或者發送的是空值。 因此,進一步調查后,我發現了另一條消息。
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
因此,這意味着您的產品ID為null。 為了解決這個問題,您需要更改以下選項之一的代碼。
<span th:text="${product.id != null} ? ${product.id} : 'null'></span>
<span th:text="${product?.id}"></span>
最后一個選項稱為“安全導航”。 我沒用過 我只使用了第一個,但它也應該起作用。 有關安全導航的更多信息,請參見此處。 [安全導航]
還有一件事,我看不到調用${product.id}
的片段,但是執行我剛剛發送給您的操作應該可以。
我在錯誤日志中看到雙斜杠(//):templates // productView / productPage.html
嘗試將代碼更改為此:
@GetMapping("/productPage")
public String productPage(){
return "productView/productPage";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.