在 Glassfish 4.1 上使用 PF 5.1、JSF 2.2.7。
我有一个简单的例子selectOneMenu
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form>
<p:selectOneMenu value="#{testBean.text}">
<p:ajax listener="#{testBean.test()}" update="outputpanel"/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
</p:selectOneMenu>
<p:outputPanel id="outputpanel">
#{testBean.text}
</p:outputPanel>
</h:form>
</h:body>
</html>
豆:
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
@Named
@ViewScoped
public class TestBean implements Serializable
{
private String text;
public String getText() {
return text;
}
public void setText(String text) {
System.out.println("settext: " + text);
this.text = text;
}
public void test() {
System.out.println("test called");
}
}
它按预期工作,除了如果下拉菜单有焦点并且我在 windows 上按 ALT 或在 mac 上按 CMD,它将调用侦听器并重置下拉菜单。当下拉列表不是其默认值(当它已经在 2 或 3 时)时,就会发生这种情况。这意味着我不能按 ALT + TAB 来检查另一个打开的程序中的内容——当我回来时它会被重置。
为什么会出现这种邪恶的行为以及如何避免呢?我宁愿不用按 ALT 来触发event="change"
和重置组件。