【发布时间】:2016-06-10 17:44:23
【问题描述】:
使用在 WebSphere 8.5.5 中运行的 PrimeFaces 5.3
我有一个包含两个必填字段的表单,inputText 和 selectOneMenu。我在菜单中选择了一个有效值(例如无状态),但将文本字段留空导致错误。然后我更正了文本字段,但用户还按下了菜单上的“S”键 - 打算再次选择“无状态”,但结果是“选择国家”,这意味着未选择任何内容 - 另一个验证错误。
当用户按下 ENTER 键时,他们会收到另一条关于 selectOneMenu 的错误消息,并且该字段本身被突出显示为错误,但出现“无状态”的有效值 - 这让我感到困惑,想象用户将如何反应!
有没有办法始终保留用户输入的值?
我附上了演示问题的净化代码。我的菜单列出了所有国家(以及美国各州、墨西哥各州、加拿大各省),因此无法为默认值分配一个字母,而该字母也不适用于有效值。
XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<!-- IE standards mode override *must* appear first - use resource ordering facet to force this. -->
<f:facet name="first">
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<!--
<meta http-equiv="x-ua-compatible" content="IE=edge" />
^ Prevents IE from reverting to 'IE7 Standards Mode' when accessing via non-localhost hostname.
Must appear immediately after <title> element. Note that setting this at the server level (ie: in websphere)
is considered preferable. See: http://stackoverflow.com/questions/6156639/x-ua-compatible-is-set-to-ie-edge-but-it-still-doesnt-stop-compatibility-mode
-->
</f:facet>
</h:head>
<h:body>
<br/>
<h:form id="dataForm">
<p:outputLabel for="note" value="Note"/>:
<p:inputText id="note" size="30" maxlength="50" value="#{Test.note}" required="true" />
<br />
<p:outputLabel for="citizenship" value="Citizenship:"/>
<p:selectOneMenu id="citizenship" value="#{Test.citizenship}" required="true">
<f:selectItem itemLabel="Select Country..."/>
<f:selectItem itemValue="1" itemLabel="Canada"/>
<f:selectItem itemValue="2" itemLabel="United States"/>
<f:selectItem itemValue="3" itemLabel="Mexico"/>
<f:selectItem itemValue="-1" itemLabel="Stateless"/>
</p:selectOneMenu>
<br />
<p:commandButton value="Save" type="submit" action="#{Test.save}" ajax="false" />
</h:form>
</h:body>
</html>
Java
import javax.inject.Named;
@Named ("Test")
public class Test
{
private String note;
private Integer citizenship;
public Integer getCitizenship()
{
return citizenship;
}
public String getNote()
{
return note;
}
public void setNote(String note)
{
this.note = note;
}
public void setCitizenship(Integer citizenship)
{
this.citizenship = citizenship;
}
public String save()
{
System.out.println("Citizenship=" + this.citizenship);
return "test";
}
}
编辑
应用了以下修改,但它根本没有效果。当我按下“S”时,它仍然会选择“选择国家...”选项,如果我点击控件,我仍然可以看到并选择“选择国家...”选项。
<p:selectOneMenu id="citizenship" value="#{Test.citizenship}" required="true" hideNoSelectionOption="true">
<f:selectItem itemValue="#{null}" itemLabel="Select Country..." noSelectionOption="true"/>
在我的浏览器调试器中详细研究这一点时,我注意到了其他一些事情。当我选择不同的选项时,HTML 选项元素的选定值不会改变 - 而标签会改变。实际的 HTML 选择元素位于类“ui-helper-hidden-accessible”的 DIV 中。我想知道这是否意味着什么......
【问题讨论】:
-
我发现了一个可以正常工作的 hack。如果我在默认国家/地区前添加一个空格,它会阻止默认值被字母选择。例如。将“选择国家...”替换为“选择国家...”。但是,如果用户确实故意选择了默认值,则会返回该字段获取最后一个有效值但仍被标记为错误的奇怪行为。
-
stackoverflow.com/questions/13478663/… 不是您的问题的一部分吗?
标签: jsf primefaces