简体   繁体   English

JSF 2.0和参数化EL表达式的问题

[英]Problems with JSF 2.0 and parametrized EL Expressions

I'm encountering some troubles while trying to access some properties with parameters. 我在尝试使用参数访问某些属性时遇到了一些麻烦。 Well, I don't know if they should be called properties since the accessors have parameters but lets call them properties for now. 好吧,我不知道它们是否应该被称为属性,因为访问器有参数,但现在让我们调用它们的属性。

The situation is this: 情况是这样的:

I have a Student class that contains different localizations as objects in a map. 我有一个Student类,它包含不同的本地化作为地图中的对象。 I can get those localization objects by doing this: 我可以通过这样做获得这些本地化对象:

student.getLocalizedData(String localizationCode);

Now, on the page, I'm trying to do this: 现在,在页面上,我正在尝试这样做:

<h:inputText value="#{collegeBean.student.getLocalizedData('es').profileDescription}"/>

A student's description can have many localizations. 学生的描述可以有很多本地化。

The page loads correctly, but when I try to save the student it says that the bean collegeBean does not have the property getLocalizedData, tough that one is a property of the student contained on the bean. 页面加载正确,但是当我尝试保存学生时,它表示bean collegeBean没有属性getLocalizedData,但是那个bean是包含在bean上的学生的属性。

I know that line is insane, so I tried to wrap it: 我知道这条线是疯了,所以我试着把它包起来:

<h:inputText value="#{collegeBean.getStudentLocalizedData('es').profileDescription}"/>

Alas, again I get the same message. 唉,我再次得到同样的信息。

The only solution I've found is to make a method that completely wraps the studen't sproperty... but that leaves me with 2 methods (get + set) for every localization, and that's only for one property. 我发现的唯一解决方案就是制作一个完全包裹了studen't sproperty的方法......但是这为我的每个本地化留下了两种方法(get + set),而且这只适用于一个属性。 This means that I'll end having (2 x localizations x attributes) amount of methods!. 这意味着我将结束(2个本地化x属性)数量的方法!

Is there any approach I can take to solve this in the most dynamic way? 有什么方法可以用最动态的方式来解决这个问题吗? The ammount of localizations might change in the future and I don't want to change any code. 本地化的大量可能在将来发生变化,我不想更改任何代码。

EDIT: I'm using JBoss 6.1.0.Final and the JSF implementation that comes with it JSF 2.0.3-b05 编辑:我正在使用JBoss 6.1.0.Final和它附带的JSF实现JSF 2.0.3-b05

This should theoretically work, so I suspect a bug or an oversight in the EL implementation used. 这应该在理论上有效,所以我怀疑使用的EL实现中存在错误或疏忽。 You might want to try a different EL implementation (a different server make) or to upgrade the server. 您可能想尝试不同的EL实现(不同的服务器make)或升级服务器。 If in vain, report it as a bug to the server vendor. 如果徒劳无功,请将其作为错误报告给服务器供应商。

However, since you're actually delegating the getLocalizedData() to a Map , better is to not do that but instead return the whole Map directly. 但是,由于您实际上将getLocalizedData()委托给了Map ,因此最好不要这样做,而是直接返回整个Map

public Map<String, LocalizedData> getLocalizedData() {
    return localizedData;
}

so that you can use it as follows 以便您可以按如下方式使用它

<h:inputText value="#{collegeBean.student.localizedData.es.profileDescription}"/>

or, if desired 或者,如果需要的话

<h:inputText value="#{collegeBean.student.localizedData['es'].profileDescription}"/>

In my experience, most EL implementations that accept parameters are buggy in one way or other. 根据我的经验,大多数接受参数的EL实现都是以某种方式出错。 The one I am most happy with is Tomcat 7's EL. 我最满意的是Tomcat 7的EL。

Note that you can use a custom EL implementation with Mojarra and MyFaces. 请注意,您可以使用Mojarra和MyFaces的自定义EL实现。 I usually bundle Tomcat's one with my applications, so I can trust in a stable feature set being available. 我通常将Tomcat与我的应用程序捆绑在一起,因此我可以信赖一个稳定的功能集。 You must be careful though, as you can get into classloader problems if you don't configure everything correctly. 但是你必须小心,因为如果没有正确配置所有内容,你可能会遇到类加载器问题。

Mojarra: 钻嘴鱼科:

<context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>YOUR EL IMPLEMENTATION EXPRESSION FACTORY</param-value>
</context-param>

MyFaces: MyFaces的:

<context-param>
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>YOUR EL IMPLEMENTATION EXPRESSION FACTORY</param-value>
</context-param>

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

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