春季胸腺不正确处理场?

发布于 2025-02-09 05:10:27 字数 3376 浏览 1 评论 0原文

我有一个扩展基本超级类的作者课。在模板中,我试图从作者变量中获取布尔属性,以便我可以重复使用表格进行创建和更新。

有人可以解释我在做错什么以及在哪里可以阅读更多内容的原因吗?我已经尝试了文档中最多/所有的EL方式来访问该字段,因此我猜想这是在课堂上,因为将布尔属性添加到作者类本身起作用。

的质量

@MappedSuperclass
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private boolean isNew;

    public boolean isNew() {
        return this.id == null;
    }

模板

<div class="container">
    <form th:object="${author}" method="post">
        <div class="form-row">
            <div class="form-group col-md-6">
                <label>First Name</label>
                <input type="text" class="form-control" th:field="*{firstName}" placeholder="...">
            </div>
            <div class="form-group col-md-6">
                <label>Last Name</label>
                <input type="text" class="form-control" th:field="*{lastName}" placeholder="...">
            </div>
        </div>
        **<button th:text="${author['isNew']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>**
    </form>
</div>

我尝试过

<button th:text="${author['isNew']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['isNew']} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['new']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['new']} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author.isNew()} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author.isNew} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:with="text=${author['new']} ? 'Create New Author' : 'Update Author'" th:text="${text}" type="submit" class="btn btn-primary" >Process</button>

Update

表达式我已经用更多的EL表达式进行了修补,并在下面获取了这些结果。这对我来说可能意味着胸腺未正确处理bool id == null的三元运算符。

<button th:with="text=${author.id} > 0" th:text="${text}" type="submit" class="btn btn-primary" >Process</button>  //true

<button th:with="text=${author.id} == null" th:text="${text}" type="submit" class="btn btn-primary" >Process</button> //false

有效的表达不涉及阅读字段使其多余:

<button th:with="text=${author.id} == null" th:text="${text} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>

如果您知道解决方案,请PM/评论! 一个示例项目的工作是: https://github.com/spring-projects/spring-petclinic/blob/main/src/src/main/java/java/org/springframework/samples/petclinic/model/model/baseentity.java

I have an Author class which extends BaseEntity superclass. In the template I'm trying to get a boolean property from the author variable so that I can reuse the form for Create and Update.

Can someone explain the reason what I'm doing wrong and where I can read more into it? I've tried most/all EL ways in the docs to access that field so I'm guessing it's at the classes since adding the boolean property to the Author class itself works.

BaseEntity

@MappedSuperclass
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private boolean isNew;

    public boolean isNew() {
        return this.id == null;
    }

template

<div class="container">
    <form th:object="${author}" method="post">
        <div class="form-row">
            <div class="form-group col-md-6">
                <label>First Name</label>
                <input type="text" class="form-control" th:field="*{firstName}" placeholder="...">
            </div>
            <div class="form-group col-md-6">
                <label>Last Name</label>
                <input type="text" class="form-control" th:field="*{lastName}" placeholder="...">
            </div>
        </div>
        **<button th:text="${author['isNew']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>**
    </form>
</div>

Expressions I've tried

<button th:text="${author['isNew']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['isNew']} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['new']} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author['new']} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author.isNew()} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:text="${author.isNew} == true ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>
<button th:with="text=${author['new']} ? 'Create New Author' : 'Update Author'" th:text="${text}" type="submit" class="btn btn-primary" >Process</button>

Update

I've tinkered around with more EL expressions and got to these results below. This could mean to me that thymeleaf isn't processing the ternary operator correctly for bool id == null.

<button th:with="text=${author.id} > 0" th:text="${text}" type="submit" class="btn btn-primary" >Process</button>  //true

<button th:with="text=${author.id} == null" th:text="${text}" type="submit" class="btn btn-primary" >Process</button> //false

The expression that worked doesn't involve reading the field making it redundant:

<button th:with="text=${author.id} == null" th:text="${text} ? 'Create New Author' : 'Update Author'" type="submit" class="btn btn-primary" >Process</button>

If you know a solution to this please PM/Comment!
An example project where its working is:
https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-02-16 05:10:27

您需要将所有东西都放在$ {}中,以让百里香叶理解表达

<button th:text="${author.isNew() ? 'Create New Author' : 'Update Author'}" type="submit" class="btn btn-primary" >Process</button>

You need to put everything inside the ${} to let thyme leaf understand the expression

<button th:text="${author.isNew() ? 'Create New Author' : 'Update Author'}" type="submit" class="btn btn-primary" >Process</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文