我一直收到此错误addAddress:states:验证错误:使用<p:selectOneMenu
时值无效。我尝试使用id并摆脱转换器,它做同样的事情。我试过调试。发现转换后运行了两次,最后一次检查空值并返回null / false。我究竟做错了什么?如果需要更多细节,请告诉我?
代码如下
转换器
@FacesConverter(value = "statemasterconverter", forClass = Statemaster.class)
public class StateConverter implements Converter {
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return ConversionHelper.getAsString(value);
}
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
return ConversionHelper.getAsObject(Statemaster.class, value);
}
}
转化助手
public final class ConversionHelper {
private ConversionHelper() {
}
@SuppressWarnings("unchecked")
public static <T> T getAsObject(Class<T> returnType, String value) {
BigDecimal id = BigDecimal.ZERO;
if (returnType == null) {
throw new NullPointerException(
"Trying to getAsObject with a null return type.");
}
if (value == null) {
throw new NullPointerException(
"Trying to getAsObject with a null value.");
}
try {
id = BigDecimal.valueOf(Long.parseLong(value));
} catch (NumberFormatException nfe) {
return null;
}
Session session = HibernateUtil.getSessionFactory().openSession();
try {
T r = (T) session.load(returnType, id);
if (r != null)
Hibernate.initialize(r);
return r;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
return null;
}
public static String getAsString(Object value) {
if (value instanceof Gendermaster) {
Gendermaster result = (Gendermaster) value;
return String.valueOf(result.getGenderid());
} else if (value instanceof Salutationmaster) {
Salutationmaster result = (Salutationmaster) value;
return String.valueOf(result.getSalutationid());
} else if (value instanceof Countrymaster) {
Countrymaster result = (Countrymaster) value;
return String.valueOf(result.getCountryid());
} else if (value instanceof Statemaster) {
Statemaster result = (Statemaster) value;
return String.valueOf(result.getStateid());
}
return null;
}
}
xhtml代码
<p:row>
<p:column>
Country
</p:column>
<p:column>
<p:selectOneMenu id="country"
value="#{customerBean.country.countryid}" required="true">
<f:selectItem itemLabel="Select Country" itemValue="" />
<f:selectItems value="#{customerBean.countrydropdown}"
var="countrymaster"
itemLabel="#{countrymaster.countryname} - #{countrymaster.countrycodeNn}"
itemValue="#{countrymaster.countryid}" />
<p:ajax update="states"
listener="#{customerBean.updateStates()}" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>
State
</p:column>
<p:column>
<p:selectOneMenu id="states" required="true"
value="#{customerBean.state}" converter="statemasterconverter">
<f:selectItem itemValue="" itemLabel="Select State" />
<f:selectItems value="#{customerBean.statedropdown}"
var="statemaster"
itemLabel="#{statemaster.statename} - #{statemaster.statecode}"
itemValue="#{statemaster}" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:commandButton id="saveBtn" value="Save Salutation"
update="msgs"
style="float: left;" icon="ui-icon-disk"
actionListener="#{customerBean.saveAddress()}" ajax="true" />
</p:column>
答案 0 :(得分:-1)
在Ajax中使用event="valueChange"
。并尝试通过update="@form"
更新整个表单。
如果上面的代码不起作用,请尝试update=":id:id"
更新代码中的代码搜索ID。
让我们考虑一下这段代码。
<h:form id="myForm">
<h:sometag id="ineer1">
<p:ajax update="ineer3"/>// it is **not work**
<p:ajax update=":myForm:ineer2:ineer2"/>// it is **work**
</h:sometag>
<h:sometag id="ineer2">
<h:someoutfield id="ineer3"/>
</h:sometag>
<h:/form>
如果不工作,它就在我的工作,然后让我知道。
很乐意提供帮助:)