【发布时间】:2016-08-08 08:07:23
【问题描述】:
我是 JSF 的新手,我很难理解 JSF 在验证失败后在表单中呈现的值。我正在使用 WebSphere 7 及其默认的 JSF 实现 MyFaces(我认为是 2.0)。
我的 xhtml 看起来像这样:
<h:form id="form">
<h:inputText id="text" value="#{backing.text}" required="true"/>
<h:message for="text" />
<h:selectManyListbox id="options" value="#{backing.options}" required="true">
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
</h:selectManyListbox>
<h:message for="options" />
<h:commandButton value="Save" />
</h:form>
我的支持豆是这样的:
public class Backing {
private String text;
private String[] options;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
}
- 我用一些文字填写了
<h:inputText />。 - 我从
<h:selectManyListbox />中选择了两个选项 - 我按下“保存”按钮
- 使用我为
<h:inputText />输入的值和我在<h:selectManyListbox />上选择的选项呈现表单(没有显示验证消息,如预期的那样) - 现在...
- 我清空了
<h:inputText /> - 我从
<h:selectManyListbox />中取消选择这两个选项 - 我按下“保存”按钮
- 表单将
<h:inputText />和<h:selectManyListbox />呈现为空,并使用我之前选择的选项(如预期的那样显示两个验证消息)
如您所见,渲染<h:inputText /> 和<h:selectManyListbox /> 时的行为是不同的:
-
<h:inputText />渲染组件的提交值 -
<h:selectManyListbox />渲染 bean 的值
我一直在尝试渲染<h:selectManyListbox />,但没有选择任何选项,但没有破解或弄乱我的代码,但没有运气。
¿这是一些错误吗? ¿ 我错过了什么吗?
【问题讨论】:
-
对此的更多输入:在构建其他表单时,我注意到文本输入具有相同的行为,然后将其转换为日期。我通过在我的 web.xml 中将 javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 设置为 true 来解决此问题。但是这样做我只能解决日期文本输入问题,h:selectManyListbox 仍然以相同的方式运行。
-
检测到
h:selectOneMenu的相同行为
标签: validation jsf-1.2