简体   繁体   English

Tapestry-将方法应用于tml循环中的值

[英]Tapestry - apply method to a value from a tml loop

I found a similar post but what I´m asking is a bit different. 我找到了类似的帖子,但我要问的内容有所不同。

I have a table 'Room' in my database to store info about rooms. 我的数据库中有一个“房间”表,用于存储有关房间的信息。 One of the elements in the table is price (float in MySQL). 该表中的元素之一是价格(MySQL中为float)。

I want to print all my rooms in tml with their info so first, I make a List of Room Objects. 我想在tml中将所有房间及其信息打印出来,所以首先,我列出了“房间对象”列表。 (with createQuery) (使用createQuery)

Now, in tml : 现在,在tml中

<t:loop source="rooms" value="room">
   ${room.price}

${room.price} prints one decimal, for example, 120.0. $ {room.price}打印一个小数,例如120.0。

Is it possible to pass that room.price to the Java page to remove the decimal a print it correctly? 是否可以将room.price传递给Java页面以删除小数并正确打印?

Solved. 解决了。 I do not know if it´s the right way but I made: 我不知道这是不是正确的方法,但我做了:

tml tml

${getInt(room.precio)}

java 爪哇

public int getDynamicFieldValue(double arg1) {
    return (int) arg1;
}

Another better option or better code? 另一个更好的选择或更好的代码?

In the tml use ${roomPrice} 在tml中使用$ {roomPrice}

Then in the Java do something like this: 然后在Java中执行以下操作:

public String getRoomPrice() {
    return String.format("%.0f", room.getPrecio());
}

The variations are endless, including truncation with an integer, but I think this gives the basic idea: do the conversion in the Java class rather than in the tml file. 变化是无限的,包括用整数截断,但是我认为这给出了基本思想:在Java类而不是tml文件中进行转换。

Another way is to create a new Binding prefix which you can use in your template file without having to add a method in each page class where you want the formatting. 另一种方法是创建一个新的Binding前缀,您可以在模板文件中使用它,而不必在要设置格式的每个页面类中添加一个方法。

You can define as many formatters as you want, which you then can use wherever in your project you want like the following: 您可以定义任意数量的格式化程序,然后可以在项目中的任何位置使用它们,如下所示:

${format:price=room.price}

Example code: 示例代码:

https://wiki.apache.org/tapestry/Tapestry5HowToAddMessageFormatBindingPrefix https://wiki.apache.org/tapestry/Tapestry5HowToAddMessageFormatBindingPrefix

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

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