繁体   English   中英

春季靴百里香展示的productName

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM