简体   繁体   English

Java JSF:编辑dataTable标记中的行

[英]Java JSF: Editing rows in a dataTable tag

I am trying to update a row in a JSF data table based on the method in this article: http://www.mkyong.com/jsf2/how-to-update-row-in-jsf-datatable/ 我试图根据本文中的方法更新JSF数据表中的一行: http//www.mkyong.com/jsf2/how-to-update-row-in-jsf-datatable/

When I click Save, I am getting this error message: 当我单击保存时,我收到此错误消息:

Conversion Error setting value '1970-09-08' for 'null Converter'. 'null Converter'的转换错误设置值'1970-09-08'。

Screenshot: 截图: 截图

Markup: 标记:

<h:form>
                        <h:commandButton action="addEmployee" value="Add New" class="btn btn-primary" />
                        <br />

                        <h:dataTable class="table table-striped" value="#{employeeCollection.items}" var="item">
                            <h:column>
                                <f:facet name="header">Edit</f:facet>
                                <h:commandButton action="#{employeeCollection.edit(item)}" value="Edit" class="btn" rendered="#{not item.isEditing}" />
                                <h:commandButton action="#{employeeCollection.save(item)}" value="Save" class="btn btn-success" rendered="#{item.isEditing}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">First Name</f:facet>
                                <h:inputText value="#{item.firstName}" rendered="#{item.isEditing}" />
                                <h:outputText value="#{item.firstName}" rendered="#{not item.isEditing}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">Last Name</f:facet>
                                <h:inputText value="#{item.lastName}" rendered="#{item.isEditing}" />
                                <h:outputText value="#{item.lastName}" rendered="#{not item.isEditing}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">Date of Birth</f:facet>
                                <h:inputText value="#{item.dateOfBirth}" rendered="#{item.isEditing}" />
                                <h:outputText value="#{item.dateOfBirth}" rendered="#{not item.isEditing}" />
                            </h:column>
                        </h:dataTable>
                    </h:form>

I would post the managed bean code, but I don't think that's necessary because when clicking the Save button, the save(item) function never seems to be called anyway... so it must be something else. 我会发布托管bean代码,但我不认为这是必要的,因为当单击Save按钮时,似乎永远不会调用save(item)函数...所以它必须是其他东西。 I am new to Java, so probably I'm forgetting something very basic here, somewhere... 我是Java的新手,所以我可能忘记了一些非常基础的东西,某处......

The error describes that there was an error converting the String using a null converter. 该错误描述了使用null转换器转换String出错。 This happens if you use a Date object in your <h:inputText> tag component without a DateTime converter. 如果在没有DateTime转换器的<h:inputText>标记组件中使用Date对象,则会发生这种情况。 You can fix this by changing the JSF code: 您可以通过更改JSF代码来解决此问题:

<h:column>
    <f:facet name="header">Date of Birth</f:facet>
    <h:inputText value="#{item.dateOfBirth}" rendered="#{item.isEditing}">
        <!-- adding the datetime converter -->
        <f:convertDateTime pattern="yyyy-MM-dd" />
    </h:inputText>
    <h:outputText value="#{item.dateOfBirth}" rendered="#{not item.isEditing}" />
</h:column>

Still, it's not a good approach to handle the date validations for you and your user. 尽管如此,处理您和您的用户的日期验证并不是一个好方法。 It would be better to use a third party library that provides you a Calendar component like PrimeFaces Calendar or RichFaces Calendar . 最好使用第三方库,为您提供PrimeFaces CalendarRichFaces Calendar等日历组件。

Note that you can use the JSF provided converters for DateTime and Numbers, and you can create your custom data converters. 请注意,您可以将JSF提供的转换器用于DateTime和Numbers,并且可以创建自定义数据转换器。 More info: 更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM