我正在尝试为某些业务验证捕获旧/新价值。为此,ValueChangeListener 似乎是一个不错的选择。它在 h:selectOneMenu 上工作得很好,但是当与带有支持组件的自制复合组件一起使用时它不会被调用。知道我做错了什么吗?
要添加的一件事是,当从 state.xhtml 中删除 componentType 属性时,valueChangeListener 会按预期工作......
组件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface displayName="state" componentType="com.company.dept.system.ui.address.State" shortDescription="State Information Display/Input Component">
<composite:attribute name="value" type="java.lang.String" required="true" shortDescription="The value of the component" />
<composite:editableValueHolder name="state" />
</composite:interface>
<composite:implementation>
<div id="#{cc.clientId}">
<h:selectOneMenu id="state" value="#{cc.attrs.value}">
<f:selectItem itemLabel="(select)" noSelectionOption="true"/>
<f:selectItems var="item" itemLabel="#{item.displayValue}" value="#{cc.states}" />
</h:selectOneMenu>
</div>
</composite:implementation>
</html>
支持组件
@FacesComponent("com.company.dept.system.ui.address.State")
public class State extends UIInput implements NamingContainer {
private List<com.company.dept.policy.enums.State> states;
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
/**
* Prepare the list of states to display
*/
public List<com.company.dept.policy.enums.State> getStates(){
if (states != null) {
return states;
}
states = new ArrayList<com.company.dept.policy.enums.State>();
for (com.company.dept.policy.enums.State st : com.company.dept.policy.enums.State.values()) {
if(!st.equals(com.company.dept.policy.enums.State.NWNORWAY) && !st.equals(com.company.dept.policy.enums.State.UNKNOWN) && !st.equals(com.company.dept.policy.enums.State.TTTRUST_TERRITORY_AND_GUAM)) {
states.add(st);
}
}
Collections.sort(states,new StateNameComparator());
return states;
}
}
值变化监听器
public class ClientValueChangeListener implements ValueChangeListener {
@Override
public void processValueChange(ValueChangeEvent event)
throws AbortProcessingException {
System.out.println("*****************************");
System.out.println("VALUE CHANGE LISTENER. OLD=" + event.getOldValue() + " - NEW=" + event.getNewValue());
System.out.println("*****************************");
}
}
消费页面:
<h:form>
<address:state value="#{testPage.state}">
<f:valueChangeListener type="com.company.dept.system.ui.clientinformation.ClientValueChangeListener" for="state"/>
</address:state>
<h:commandButton id="submitButton" value="Test" action="#{testPage.act}"/>
</h:form>