JSF getValue()与getSubmittedValue() [英] JSF getValue() v.s. getSubmittedValue()

查看:137
本文介绍了JSF getValue()与getSubmittedValue()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在开发一些JSF应用程序,并且对Web组件API中的不一致感到不安.

I've been developing a few JSF applications lately and am disturbed with the inconsistency in the web component APIs.

我注意到,在服务器端代码中的JSF组件对象上调用.getValue()或.getSubmittedValue()时,存在极其不可预测的行为.有时,当我在下拉列表框上调用.getValue()时,我注意到在选择值之前(即刷新上一页的值之前),我得到了该值,其中.getSubmittedValue()得到我是正确的值,例如:

I've noticed that there is extremely unpredictable behavior when calling .getValue() or .getSubmittedValue() on a JSF component object in server side code. Sometimes when I call .getValue() on a drop down list box, I've noticed that I get the value as it was BEFORE I selected my value (so the value from the last page refresh), of which .getSubmittedValue() gets me the correct value, as such:

UIInput name = new UIInput(); // This is the control I have in a bean.

public void submit(ActionEvent ae)
{
    someMethod(name.getValue().toString());          // Retrieves the "old" value
    someMethod(name.getSubmittedValue().toString()); // Retrieves the correct value 
}

此外,我注意到在表单字段上调用.getSubmittedValue()有时会导致空指针异常,因为该值尚未在组件对象中实例化,在这种情况下,当我在其中调用.getValue()时,在这种情况下,我会得到正确的值,例如:

Also, I've noticed that calling .getSubmittedValue() on a form field sometimes results in a null pointer exception because that value has not been instantiated in the component object, in which case when I call .getValue() in that circumstance I get the correct value, for example:

HtmlInputText name = new HtmlInputText(); // This is the control I have in a bean.

public void submit(ActionEvent ae)
{
    someMethod(name.getValue().toString());          // Retrieves the correct value
    someMethod(name.getSubmittedValue().toString()); // Throws NullPointerException 
}

这仅仅是JSF框架的怪癖",还是我错误地使用了API 完全?任何对这两种方法的见解将不胜感激.干杯.

Is this just a "quirk" of the JSF framework, or am I just using the API COMPLETELY incorrectly?? Any insight into these two methods would be greatly appreciated. Cheers.

推荐答案

由于这是Google搜索getValue与getSubmittedValue的第一结果,我想补充一下,两者之间的差异对验证至关重要(即在编写自定义验证程序时

Since this is the #1 result in Google for searching on getValue vs. getSubmittedValue I'd just like to add that the difference between these is critical in validation (i.e. when writing a custom validator)

引用getSubmittedValue()的API文档:

To quote the API documentation for getSubmittedValue():

这仅在两次解码之间为非null 并验证阶段,或何时 组件验证尚未 成功了.一旦转换并 验证成功, (转换后的)值存储在 此的本地值"属性 组件,提交的值是 重置为空.

This is non-null only between decode and validate phases, or when validation for the component has not succeeded. Once conversion and validation has succeeded, the (converted) value is stored in the local "value" property of this component, and the submitted value is reset to null.

来源: http://myfaces.apache.org /core11/myfaces-api/apidocs/javax/faces/component/UIInput.html#getSubmittedValue()

这意味着,如果对您尝试访问的绑定进行了验证/转换,则应调用getValue(),否则必须调用getSubmittedValue()并自行进行解析.这些发生的顺序似乎由它们在用户界面中出现的顺序决定,但我认为这并不能保证.即使是这样,您也不应该依靠它,因为UI中的更改字段不会破坏您的代码.

This means that if the validation/conversion has taken place for the binding you are trying to access, you should call getValue() otherwise you'll have to call getSubmittedValue() and deal with parsing it yourself. The order in which these occur seems to be dictated by the order they appear in the UI, but I don't think that's guaranteed. Even if it is, you shouldn't count on that as changing field in your UI shouldn't break your code.

您可以仅通过查看isLocalValueSet()返回的内容来检测是否已完成验证/转换.如果返回true,则说明已完成验证/转换,因此应调用getValue().否则,您将需要调用getSubmittedValue(),这将为您提供用户输入的原始输入,并且您可能希望将其解析为更有意义的内容.

You can detect if the validation/conversion has been done by just looking at what isLocalValueSet() returns. If it returns true, then the valdation/conversion has been done, so you should call getValue(). Otherwise you'll need to call getSubmittedValue() and that'll give you the raw input the user entered and you'll likely want to parse it into something more meaningful.

例如,当调用getValue()时,日历对象将返回Date对象,但是当调用getSubmittedValue()时,则返回String对象.由您的转换器将字符串解析为日期,以便可以对其进行验证.

For example, a calendar object would return a Date object when getValue() was called, but a String object when getSubmittedValue() was called. It's up to your converter to parse the string into a Date so it can be validated.

如果JSF规范中有一种方法可以为我们做到这一点,那就太好了,但AFAIK却没有.如果某些日期需要早于其他日期,并且某些日期仅在某些情况下才需要,则将需要编写多个验证器来处理该日期.因此,它很容易成为一个问题.这类似于您无法对空白字段进行任何类型的验证这一事实,这意味着您无法有条件地将该字段设为必填项.如果对所有字段(即使是空白字段)都进行了验证,则可以编写自定义验证器以引发异常(如果需要),而不是不需要.使用JSF时有些事情很痛苦.除非/直到它们被修复,否则我们只需要处理它们即可.

It'd be great if the JSF spec had a method which would do this for us, but AFAIK it doesn't. If certain dates need to be before other dates, and some are only required in certain circumstances, one will need to write several validators to handle this. So it can easily become an issue. This is similar to the fact that you can't do any kind of validation on a blank field, which means you can't make that field conditionally required. If validation was run on all fields, even blank ones, a custom validator could be written to throw an exception if it should be required and is not. There are some things with JSF which are just a pain; unless/until they're fixed, we just have to deal with them.

要在原始帖子中谈论问题的细节:这里的区别是您在生命周期中所处的位置. submit方法似乎是按钮的动作侦听器,它将其置于生命周期的尽头.操作和操作侦听器在呈现响应之前但在验证之后的调用应用程序"阶段中触发.如果要使用JSF进行编程,则应该学习和理解生命周期.值得的.

To speak to the specifics of the issue in the original post: the difference here is where you're at in the life cycle. The submit method seems like an action listener for a button, which puts it at the end of the life cycle; actions and action listeners are triggered in the "Invoke Application" phase which comes prior to the render response, but after validation. If you're going to program in JSF, you should learn and understand the life cycle. It's worth the time.

这篇关于JSF getValue()与getSubmittedValue()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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