JSF显示带有ID的实体:如何将ID转换为描述? [英] JSF displaying entities with IDs: how to translate IDs to descriptions?

查看:79
本文介绍了JSF显示带有ID的实体:如何将ID转换为描述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF页面中,我必须显示来自实体的数据. 该实体具有一些int字段,这些字段无法直接显示,但需要转换为描述性字符串. 它们之间的某些值可以是有限的,其他的则可以有很多可能的值(例如,全球通用的Country_ID),并应在Db上保留一个带有关联(ID,描述)的表.

通过从原始实体到与字典表相对应的实体(ID,描述)之间的关系导航可以很容易地解决后一种情况,但是我不想引入新的实体仅仅是为了解决ID到描述的翻译形式. /p>

除了另一个整数字段有特殊需要外,还应根据诸如100015-> A00015,301023-> C01023之类的规则用字母来更改十万个数字.

最初,我将翻译代码放在实体本身内部,但我知道此解决方案的局限性和弊端.

然后,我使用所有翻译不同字段的方法创建了一个单调( EntityTranslator ).对于字段值很多的情况,我将它们放在一个表中,该表从单调加载并在 TreeMap 中进行了转换,否则说明位于类内部的数组中.

ManagedBean 中,我为 EntityTranslator 写了一个吸气剂,在jsf中,我使用了很长的el语句,如下所示:

#{myManagedBean.entityTranslator.translateCountryID(myManagedBean.selectedEntity.countryID)}

我认为问题很普遍,我正在寻找解决问题的标准方法,但是,正如已经指出的那样,我不想创建新的愚蠢"实体,而只是将ID与描述相关联,我认为这是矫kill过正.

另一种可能性是使用转换器Object(Integer)<->字符串,但我更愿意在同一个类中包含对Entity的所有翻译需求.

解决方案

您的问题可以归结为以下简单行:

如何在视图中显示与实体ID不同的字段,以及如何将整数字段变形为更有意义的内容.

答案是,这取决于情况.

如果只想输入/输出数据,则除了可能的view参数(如?id=12345)外,根本不需要id.在这种情况下,您可以输入/输出视图中所需的任何内容:ID始终存在.

如果您最想创建一个新实体,则除了通过用户的直接输入之外,还可以通过JPA或数据库或其他方式生成ID.在这种情况下,您也不需要弄乱ID.

如果您想使用其他实体的信息,例如向用户显示一个下拉框,例如国家列表,您始终可以选择将label(将其命名为name)和value(将其命名为id)分开,甚至在数据库表中甚至包含一个唯一的非null列,其中包含国家/地区名称,用作自然标识符.如果您想使用输入文本字段从用户那里获取数据,则始终可以创建一个转换器,该转换器可以完成将用户输入字符串转换为实际实体对象的工作.

关于整数的转换,实际上您有多种选择:第一个是为这些字段附加一个转换器,这些转换器将大致执行301023 -> C01023C01023 -> 301023转换,第二个是编写自定义EL功能,第三个功能是事先准备正确的模型/即时进行转换.

In a JSF page I have to display the data from an entity. This entity has some int fields which cannot be displayed directly but need to be translated into a descriptive string. Between them some can have a limited number of values, others have lots of possible values (such as a wordlwide Country_ID) and deserve a table on the Db with the association (ID, description).

This latter case can easily be solved navigating via relationship from the original entity to the entity corresponding to the dictionary table (ID, description) but I don't want to introduce new entities just to solve translations form ID to description.

Besides another integer field has special needs: the hundred thousand number should be changed with a letter according to a rule such as 100015 -> A00015, 301023 -> C01023.

Initially I put the translation code inside the entity itself but I know the great limits and drawbacks of this solution.

Then I created a singletone (EntityTranslator) with all the methods to translate the different fields. For cases where the field values are a lot I put them inside a table which is loaded from the singletone and transformed in a TreeMap, otherwise the descriptions are in arrays inside the class.

In the ManagedBean I wrote a getter for EntityTranslator and inside the jsf I use quite long el statements like the following:

#{myManagedBean.entityTranslator.translateCountryID(myManagedBean.selectedEntity.countryID)}

I think the problem is quite general and I'm looking for a standard way to solve it but, as already stated, I don't want to create new 'stupid' entities only to associate an ID to a description, I think it is overkill.

Another possibility is the use of converters Object(Integer) <-> String but I'm more comfortable in having all the translation needs for an Entity inside the same class.

解决方案

Your question boils down to the following simple line:

How can I display a field different from id of my entity in my view and how can I morph an integer field into something more meaningful.

The answer is that it depends on a situation.

If you solely want to input/output data, you don't need id at all apart from the possible view parameter like ?id=12345. In this case you can input/output anything you want in your view: the id is always there.

If you want to create a new entity most possibly you have a way of generating ids via JPA, or database, or elsehow besides the direct input from the user. In this situation you don't need to mess with ids as well.

If you want to use information on other entities like show user a dropdown box with e.g. a list of countries, you always have the option to separate label (let it be name) and value (let it be id), or even have a unique not null column containing the country name in your database table that will serve as a natural identifier. If you'd like to get data from the user using an input text field you always can create a converter that will do the job of transforming user input strings to actual entity objects.

Regarding the transformation of your integers, you've actually got several choices: the first one is to attach a converter for these fields that will roughly do 301023 -> C01023 and C01023 -> 301023 transformations, the second one is to write a custom EL function and the third one is to prepare the right model beforehand / do the transformations on-the-fly.

这篇关于JSF显示带有ID的实体:如何将ID转换为描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆