JSF(primefaces)SelectOneMenu没有在bean

时间:2015-06-03 12:14:00

标签: jsf jsf-2 primefaces

primefaces selectOneMenu在'''''自定义对象,当这里简单的对象为String,Double,Integer时它可以正常工作:

     <ui:define name="body-content">
    <p:panel id="filtePanel">
        <c:forEach items="#{journalCall.filterPanel.filterList}" var="filter">
            <h:form class="form-inline" style="display:flex;">
                <p:panel styleClass="form-group" style="white-space: nowrap">
                    <p:selectOneMenu styleClass="form-control" value="#{filter.definition}">
                        <f:selectItem noSelectionOption="true" itemLabel="Выбор фильтра"/>
                        <f:selectItems value="#{journalCall.filterPanel.availableFields}" var="field"
                                       itemLabel="#{field.caption}"
                                       itemValue="#{field}"/>
                        <p:ajax event="change" update="@this :filtePanel"/>
                    </p:selectOneMenu>
                </p:panel>
            </h:form>
        </c:forEach>
    </p:panel>
    <h:form styleClass="container">
        <p:commandButton value="Добавить фильтр" actionListener="#{journalCall.filterPanel.addPanel}"
                         update=":filtePanel"
                         styleClass="btn-primary"/>
        <ui:insert name="search"/>
    </h:form>
</ui:define>

JournalCall是@SessionScoped @ManagedBean 我试着使用转换器,默认jsf selectOneMenu没有结果......

bean代码:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@SessionScoped
@ManagedBean
public class JournalCall implements Serializable {

private static final long serialVersionUID = 1L;

@Inject
@FilterPanelQualified(form = FilterForm.JOURNAL_CALL)
private FilterPanel filterPanel;

public FilterPanel getFilterPanel() {
    return filterPanel;
   }
}

FilterFieldDefinition类(FilterFieldType是枚举):

public class FilterFieldDefinition {

private  String name;
private  FilterFieldType fieldType;
private  String caption;

public FilterFieldDefinition(String name, FilterFieldType fieldType,
        String caption) {
    this.setName(name);
    this.setFieldType(fieldType);
    this.setCaption(caption);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public FilterFieldType getFieldType() {
    return fieldType;
}

public void setFieldType(FilterFieldType fieldType) {
    this.fieldType = fieldType;
}

public String getCaption() {
    return caption;
}

public void setCaption(String caption) {
    this.caption = caption;
  }
 }

@Override
public List<FilterFieldDefinition> getAvailableFields() {
    return (metadata);
}

f:selectItems值为

 public List<FilterFieldDefinition> metadata;

@PostConstruct
public void init() {
 ....
 }
    @Override
public List<FilterFieldDefinition> getAvailableFields() {
    return (metadata);
}

getFilterList()实例:

public class FilterCondition {

private String textValue;
private Date dateFrom;
private Date dateTo;
private String secondTextValue;
private String panelID;
private FilterFieldDefinition definition;
private FilterOperation operation;

public String getTextValue() {
    return textValue;
}

public void setTextValue(String textValue) {
    this.textValue = textValue;
}

public Date getDateFrom() {
    return dateFrom;
}

public void setDateFrom(Date dateFrom) {
    this.dateFrom = dateFrom;
}

public Date getDateTo() {
    return dateTo;
}

public void setDateTo(Date dateTo) {
    this.dateTo = dateTo;
}

public String getSecondTextValue() {
    return secondTextValue;
}

public void setSecondTextValue(String secondTextValue) {
    this.secondTextValue = secondTextValue;
}

public String getPanelID() {
    return panelID;
}

public void setPanelID(String panelID) {
    this.panelID = panelID;
}

public FilterOperation getOperation() {
    return operation;
}

public void setOperation(FilterOperation operation) {
    this.operation = operation;
}

public FilterFieldDefinition getDefinition() {
    return definition;
}

public void setDefinition(FilterFieldDefinition definition) {
    this.definition = definition;
}
}

转换器:

  @FacesConverter(forClass = FilterFieldDefinition.class,value =    "simpleConverter")
 public class SimpleConverter implements Converter {

@Inject
@FilterPanelQualified(form = FilterForm.JOURNAL_CALL)
private FilterPanel filterPanel;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.isEmpty()) {
        return null;
    }

    for (FilterFieldDefinition filterFieldDefinition : filterPanel.getAvailableFields()) {
        if (filterFieldDefinition.getCaption().equals(value)) {
            return filterFieldDefinition;
        }
    }
    return null;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value == null) {
        return "";
    }
    if (value instanceof FilterFieldDefinition) {
        return String.valueOf(((FilterFieldDefinition) value).getCaption());
    } else {
        return null;
    }
}
}

设置转换器:

      <h:selectOneMenu styleClass="form-control" value="#{filter.definition}" immediate="true" converter="simpleConverter">

0 个答案:

没有答案