Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot和Thymeleaf中的单表继承_Java_Jpa_Spring Boot_Thymeleaf - Fatal编程技术网

Java Spring Boot和Thymeleaf中的单表继承

Java Spring Boot和Thymeleaf中的单表继承,java,jpa,spring-boot,thymeleaf,Java,Jpa,Spring Boot,Thymeleaf,我在JPA中使用单表继承 -汽车 --电动汽车 --汽油车 在我的thymeleaf模板中,我通过访问每个超类(car)的列表,以便创建一个包含两个子类对象的表 <tr th:each="car : ${cars}" th:class="${not car.isECar()} ? 'electricCar' : 'notECar'"> 我现在有一个问题,我想访问一个子类的属性,而另一个子类中不存在这个属性 <td th:text="'kWh ' + ${car.kwh}

我在JPA中使用单表继承

-汽车
--电动汽车
--汽油车

在我的thymeleaf模板中,我通过访问每个超类(car)的列表,以便创建一个包含两个子类对象的表

<tr th:each="car : ${cars}" 
th:class="${not car.isECar()} ? 'electricCar' : 'notECar'">

我现在有一个问题,我想访问一个子类的属性,而另一个子类中不存在这个属性

<td th:text="'kWh ' + ${car.kwh}" style="vertical-align: center;">1025 kWh</td>
1025 kWh
这将引发以下异常:

org.springframework.expression.spel.SpelEvaluationException:EL1008E:(位置6):在类型为的对象上找不到属性或字段“…”


只有当属性存在时,才有办法呈现html标记吗?

好的,我现在找到了一个解决方法:

首先,我在超类car中编写了一个方法,它返回null
在我的子类E-Car中,我覆盖了返回正确值的方法

现在,每辆车都有一个名为getKWH()的方法,我可以通过以下方式访问它:

<td th:text="${car.getKWH()} + ' kWh'" th:if="${car.getKWH()!=null}">2200 kWh</td>
2200kWh

好,我现在找到了一个解决方法:

首先,我在超类car中编写了一个方法,它返回null
在我的子类E-Car中,我覆盖了返回正确值的方法

现在,每辆车都有一个名为getKWH()的方法,我可以通过以下方式访问它:

<td th:text="${car.getKWH()} + ' kWh'" th:if="${car.getKWH()!=null}">2200 kWh</td>
2200kWh

try th:if=“${car.kwh!=null}”benkuly不工作,因为在其他子类中找不到kwh:(try th:if=“${car.kwh!=null}”@benkuly不工作,因为在其他子类中找不到kwh:(我有相同的问题…但搜索更好的解决方案我有相同的问题…但搜索更好的解决方案)