为什么我总是可以在Primefaces中的getAsObject()中获得标签?

时间:2017-02-25 13:09:14

标签: jsf primefaces

我不知道为什么我会验证一切正确的事情。有人知道吗? 我试着调试:
- getAsString()返回ID(右边)
- 当我提交时 - getAsObject()总是抛出异常,因为标签值总是传递而不是ID值 我的代码如下:

我的xhtml文件:

<div class="ui-grid-row" style="margin-top: 5px; margin-bottom: 5px;">
        <div class="ui-grid-col-2 pdt4">
            <p:outputLabel for="txtApprovalScheduler"
                           value="#{lang['workforce.category.parttimeManagement.approved.scheduler']}"/>
        </div>
        <div class="ui-grid-col-2">
            <p:selectOneMenu value="#{parttimeController.selectedAgentDTO}"
                             id="txtApprovalScheduler"
                             filterMatchMode="contains" editable="true"
                             style="width: 86%;"
                             required="true"
                             requiredMessage="#{lang['workforce.category.parttimeManagement.approved.scheduler.missing']}">
                <f:converter converterId="agentConverter"/>
                <f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
                <f:selectItems value="#{parttimeController.agentDTOs}" var="agentItem1"
                               itemLabel="#{agentItem1.userName}"
                               itemValue="#{agentItem1}"/>
            </p:selectOneMenu>
        </div>
        <div class="ui-grid-col-2 pdt4">
            <p:outputLabel for="txtApprovalRegister"
                           value="#{lang['workforce.category.parttimeManagement.approved.register']}"/>
        </div>
        <div class="ui-grid-col-2">
            <p:selectOneMenu value="#{parttimeController.selectedAgentDTOForRegister}"
                             id="txtApprovalRegister" editable="true"
                             filterMatchMode="contains" style="width: 86%; font-size: 12px !important;"
                             required="true"
                             requiredMessage="#{lang['workforce.category.parttimeManagement.approved.register.missing']}">
                <f:converter converterId="agentConverter"/>
                <f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
                <f:selectItems value="#{parttimeController.agentDTOs}" var="item1"
                               itemLabel="#{item1.userName}"
                               itemValue="#{item1}"/>
            </p:selectOneMenu>
        </div>
    </div>

我的转换器:

@FacesConverter(forClass = AgentDTO.class,value = "agentConverter")
public class AgentConverter implements Converter {

    public static List<AgentDTO> listAgentDTOs;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String agentId) {
        if (agentId.trim().equals("")) {
            return null;
        } else {
            try {
                Long number = Long.parseLong(agentId);
                for (AgentDTO a : listAgentDTOs) {
                    if (a.getAgentId() == number) {
                        return a;
                    }
                }
            } catch (NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid data"));
            }
        }
        return null;
    }
    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
        if (value == null || value.equals("") || "-1".equals(value)) {
            return "";
        } else {
            String result = String.valueOf(((AgentDTO) value).getAgentId());
            return result;
        }
    }
}

我的豆子:

@Component
@Scope("view")
@ManagedBean(name = "parttimeController")
public class ParttimeController extends BaseController implements Serializable {
private AgentDTO selectedAgentDTO;
private AgentDTO selectedAgentDTOForRegister;
private List<AgentDTO> agentDTOs;
.... getter/setter and initiallize AgentCenvert.listAgentDTOs

我的班级:

public class AgentDTO{
    private Long agentId;
private String userName;
....getter/setter
}

1 个答案:

答案 0 :(得分:0)

我找到了原因 - 我只删除 selectOneMenu 中的可编辑属性。因此getAsObject()将传递值而不是标签