繁体   English   中英

如何摆脱 org.thymeleaf.exceptions.TemplateInputException: 同时使用 thymeleaf 表达式以引导卡的形式打印数据?

[英]how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?

我正在一个小型电子商务网站上工作,spring 引导用于后端目的,并尝试在包含 4 张卡片的行中打印产品名称和成本。为此,我使用JpaRepositoryfindAll()方法获取所有数据并复制另一个列表中的前三个数据并使用model.addAttribute()传递到网页

当我运行程序时它工作正常,但是当我通过我已经映射的 URL 时,我得到了这个异常:

org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/MyEcommerce.html]”)

引起:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“threeproducts.productName”(模板:“MyEcommerce” - 第 89 行,第 40 列)

引起:org.thymeleaf.exceptions.TemplateProcessingException:异常评估 SpringEL 表达式:“threeproducts.productName”(模板:“MyEcommerce” - 第 89 行,第 40 列)

2020-08-10 20:57:52.433 ERROR 1152 --- [nio-8080-exec-4] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [带有路径 [] 的上下文中的 dispatcherServlet] 引发异常 [请求处理失败; 嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源 [templates/MyEcommerce.html]”)],根本原因

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“java.lang.String”类型的 object 上找不到属性或字段“productName” - 可能不是公共的或无效的?

What I know is we get this exception if model class not contain the property which we are trying to access in thymeleaf expression but in my model class property is there.

我还在下面发布了必要的 class 和 thymeleaf 模板:-

注意:我发布的 thymeleaf 模板部分仅包含网格和卡片的代码。

如果需要发布完整的代码,那我肯定会的。 而且stackoverflow还在帖子中给了我字数限制

Controller 和服务 class:

Model class:

@Entity
@Table(name = "products")
public class Products {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int productId;
    private String productName;
    private String productCost;
    private String quantityAvailable;
}

Controller class:

@Controller
@RequestMapping("/www.shopping.com")           /*Just For Fun*/
public class CustomerController {

    @Autowired
    private CustomerServices customerServices;

    /*controller to show index page of website to customers*/
    @GetMapping("/IndexPage")
    public String ViewIndexPage(Model model)
    {
        List<Products> products= customerServices.ReturnThreeProducts();
        model.addAttribute("ListOfThreeProducts","products");
        return "MyEcommerce";
    }
}

服务 class:

@Service
public class CustomerServices {

    @Autowired
    private CustomerRepository customerRepository;
    public List<Products> listofthreeproducts;

    public List<Products> ReturnThreeProducts()
    {
        List<Products> products=customerRepository.findAll();
       /* products.stream().forEach((productTemp) -> listofthreeproducts.add(productTemp));*/
        listofthreeproducts=products.subList(0,4);
        return listofthreeproducts;
    }
}

在最后的 Thymeleaf 模板中:

<body>
<--Code for navbar and jambotron upto here and below is of cards ignore 
images-->
<div class="container" th:each="threeproducts : ${ListOfThreeProducts}">
<div class="row">
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap" height="155">
            <div class="card-body">
                <h6 class="card-title" th:text = ${threeproducts.productName}></h6>
                <h6 class="card-text" th:text: $threeproducts.productCost></h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <!--<div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>
    <div class="col-xl-3">
        <div class="card" style="width: 15rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title">Card title</h6>
                <h6 class="card-text">Price</h6>
                <a href="#" class="btn btn-primary">AddToCard</a>
            </div>
        </div>
    </div>-->
</div>
</div>
</body>

提前致谢...

你的话对我来说很有价值..

在您的 controller 中尝试:

model.addAttribute("ListOfThreeProducts", products);

产品周围没有报价。 目前,您将字符串文字“产品”放入 model 而不是集合中。

暂无
暂无

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

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