简体   繁体   English

春季靴百里香展示的productName

[英]Spring boot thymeleaf display of productName

I want to display of my productName, but there is Error as: 我想显示我的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>

But I don't reason of this issue. 但是我没有这个问题的原因。 In spring I write 在春天我写

${product.productName} $ {} product.productName

and my code was working good.But in this situation I don't understand what I do wrong. 而且我的代码运行良好。但是在这种情况下,我不明白自己做错了什么。 Could you please help me with this problem. 您能帮我解决这个问题吗? Because I don't know what to do next, I tried to do myself but it did not work out. 因为我不知道接下来要做什么,所以我尝试自己动手,但没有成功。

Thank you. 谢谢。

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

I found your error, in the log that you uploaded in a comment, I saw that the error's cause was the following: 我发现了您的错误,在您上传到注释中的日志中,我发现该错误的原因如下:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "product.id" (template: "productView/productPage" - line 10, col 4)

So that menas that either your product's model doesn't have getters for that field, you are not using the correct name for that field or you are sending a null value. 这样一来,您的产品模型没有该字段的吸气剂的提示,您没有为该字段使用正确的名称,或者发送的是空值。 So investigating further, I found this other message. 因此,进一步调查后,我发现了另一条消息。

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

So it means that your product's id is null. 因此,这意味着您的产品ID为null。 In oder to fix this, you need to change your code for one of the following options. 为了解决这个问题,您需要更改以下选项之一的代码。

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

The last option is call "safe navigation". 最后一个选项称为“安全导航”。 I haven't use it. 我没用过 I have only used the first one, but it should work too. 我只使用了第一个,但它也应该起作用。 More information on safe navigation can be found here. 有关安全导航的更多信息,请参见此处。 [safe navigation] [安全导航]

One more thing, I can't see the fragment where ${product.id} is being called, but doing what I just sent you should work. 还有一件事,我看不到调用${product.id}的片段,但是执行我刚刚发送给您的操作应该可以。

I see double slashes (//) in the error log: templates//productView/productPage.html 我在错误日志中看到双斜杠(//):templates // productView / productPage.html

Try changing your code to this: 尝试将代码更改为此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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