将selectOneMenu更改为此。为我解决了这个问题。现在我的问题是该页面似乎被多次调用,最终弄乱了选择。
但是将值转换为#{st}而不是serieType可以解决无法将字符串分配给类的问题。现在它被正确地作为一个类处理。这对我来说没有任何意义,因为我有另一个选择菜单我使用另一个bean的播放器。并将值设置为#{player}但无论如何这是有效的。
现在我只需要弄清楚为什么在再次调用页面时我的选择被改变了。
<p:selectOneMenu value="#{st}"
converter="serieTypeConverter" id="serieTypeList">
<f:selectItem itemLabel="---" noSelectionOption="true" />
<f:selectItems value="#{serviceSerieType.serieTypes}"
var="st"
itemValue="#{st}"
itemLabel="#{st.serie_type}"
itemLabelEscaped="true"/>
</p:selectOneMenu>
其他信息
以下建议的解决方案无效。它实际上最终试图将serieType类分配给serieType.selectedValue事件,尽管它的原型是这个
private String selectedValue;
<p:selectOneMenu value="#{serieType.selectedValue}"
converter="serieTypeConverter" id="serieTypeList">
<f:selectItems value="#{serviceSerieType.serieTypes}"
var="st"
itemValue="#{st}"
itemLabel="#{st.serie_type}"
itemLabelEscaped="true"/>
</p:selectOneMenu>
我在下面的某个地方出现错误。我可以弄清楚它出了什么问题。我没有看到任何明显的原因,因为我使用相同的转换器为另一个类改变了类名。有一个服务通过长ID och字符串ID返回SerieType类。
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/BowlingFacelets] threw exception [Cannot convert Match of type class java.lang.String to class com.jk.hcp.SerieType] with root cause
javax.el.ELException: Cannot convert Match of type class java.lang.String to class com.jk.hcp.SerieType
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420)
这是xhtml中唯一使用的两个地方
<h:selectOneListbox size="1" value="#{serieType}"
converter="serieTypeConverter" id="serieTypeList">
<f:selectItems value="#{serviceSerieType.serieTypes}"
var="st"
itemValue="#{st}"
itemLabel="#{st.serie_type}"
itemLabelEscaped="true"/>
</h:selectOneListbox>
<c:forEach var="list" items="#{serviceSeries.getClubAverages(club.name, serieType, calendarBean)}">
在ServiceSerieType
中public List<SerieType> getSerieTypes() {
public SerieType getSerieTypeByID(long id) {
public SerieType getSerieTypeByStringID(String id) {
@FacesConverter(value = "serieTypeConverter")
public class SerieTypeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null) {
return null;
}
long idValue;
try {
idValue = Long.parseLong(value);
}
catch (NumberFormatException ex)
{
return null;
}
ServiceSerieType serviceSerieType =
context.getApplication().evaluateExpressionGet(context, "#{serviceSerieType}", ServiceSerieType.class);
SerieType serieType = serviceSerieType.getSerieTypeByID(idValue);
return serieType;
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((SerieType)value).getStringID());
}
}
}
@ManagedBean
public class SerieType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String serie_type;
private String serie_type_info;
public String getSerie_type_info() {
return serie_type_info;
}
public void setSerie_type_info(String serie_type_info) {
this.serie_type_info = serie_type_info;
}
private static final long serialVersionUID = 1L;
public SerieType() {
super();
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getSerie_type() {
return this.serie_type;
}
public void setSerie_type(String serie_type) {
this.serie_type = serie_type;
}
public String getStringID() {
if (id == null)
return "0";
return id.toString();
}
public void setStringID(String value) {
if (value == null)
return;
try {
this.id = Long.parseLong(value);
}
catch (NumberFormatException ex) {
}
}
@Override
public boolean equals(Object other) {
return (id != null && other != null && getClass() == other.getClass())
? id.equals(((SerieType) other).getId())
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
/*
@Override
public String toString() {
return this.serie_type + " " + this.serie_type_info;
}
*/
}