弹簧启动百里香叶显示的产品名称



我想显示我的产品名称,但错误为:

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)

因此,您的产品模型没有该字段的 getter,您没有为该字段使用正确的名称,或者您正在发送空值。因此,进一步调查,我发现了另一条消息。

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null

因此,这意味着您的产品ID为空。要解决此问题,您需要更改以下选项之一的代码。

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

最新更新