春季靴百里香展示的productName

时间:2018-10-18 21:05:48

标签: java spring spring-boot thymeleaf

我想显示我的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}

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

谢谢。

3 个答案:

答案 0 :(得分:1)

检查模板的语法,也许您缺少结束标记

答案 1 :(得分:0)

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

尝试将您的代码更改为此:

@Async

答案 2 :(得分:0)

我发现了您的错误,在您上传到注释中的日志中,我发现该错误的原因如下:

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}的片段,但是执行我刚刚发送给您的内容应该可以。