【问题标题】:Spring boot thymeleaf display of productNamespring boot thymeleaf 显示productName
【发布时间】:2018-10-19 08:16:11
【问题描述】:

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

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}

并且我的代码运行良好。但在这种情况下,我不明白我做错了什么。 你能帮我解决这个问题吗?因为我不知道下一步该做什么,我试着自己做,但没有成功。

谢谢。

【问题讨论】:

  • 从您在评论中添加的完整堆栈跟踪到下面的答案,错误来自使用“${product.id}”,但您的问题代码缺少此 html 片段:“异常评估 SpringEL表达式:“product.id”(模板:“productView/productPage””

标签: java spring spring-boot thymeleaf


【解决方案1】:

检查模板的语法,可能缺少结束标记

【讨论】:

  • 如您所见,标签 已关闭。
【解决方案2】:

我发现了你的错误,在你上传的日志中,我看到错误的原因如下:

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 是空的。为了解决这个问题,您需要为以下选项之一更改代码。

<span th:text="${product.id != null} ? ${product.id} : 'null'></span>
<span th:text="${product?.id}"></span>

最后一个选项是调用“安全导航”。我没用过。我只使用了第一个,但它也应该可以工作。可以在此处找到有关安全导航的更多信息。 [safe navigation]

还有一件事,我看不到调用${product.id} 的片段,但是按照我刚刚发送的操作应该可以。

【讨论】:

  • 很高兴能帮上忙!
【解决方案3】:

我在错误日志中看到双斜杠 (//): 模板//productView/productPage.html

尝试将您的代码更改为:

@GetMapping("/productPage")
public String productPage(){
return "productView/productPage";
}

【讨论】:

  • 还是同样的错误?错误日志中的路径是否更新?
猜你喜欢
  • 2019-01-02
  • 1970-01-01
  • 2021-06-03
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 2015-06-14
相关资源
最近更新 更多