Jsf-delete-datatable

提供:Dev Guides
移動先:案内検索

JSF-DataTableのデータを削除する

このセクションでは、dataTableに削除機能を追加する方法を紹介します。

応用例

上記の機能をテストするためのテストJSFアプリケーションを作成しましょう。

Step Description
1 Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - Display DataTable subchapter of JSF - DataTables chapter.
2 Modify home.xhtml as explained below. Keep the rest of the files unchanged.
3 Compile and run the application to make sure the business logic is working as per the requirements.
4 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
5 Launch your web application using appropriate URL as explained below in the last step.

home.xhtml

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core">

   <h:head>
      <title>JSF tutorial</title>
      <h:outputStylesheet library = "css" name = "styles.css" />
   </h:head>

   <h:body>
      <h2>DataTable Example</h2>

      <h:form>
         <h:dataTable value = "#{userData.employees}" var = "employee"
            styleClass = "employeeTable"
            headerClass = "employeeTableHeader"
            rowClasses = "employeeTableOddRow,employeeTableEvenRow">

            <h:column>
               <f:facet name = "header">Name</f:facet>
               <h:inputText value = "#{employee.name}"
                 size = "10" rendered = "#{employee.canEdit}"/>
               <h:outputText value = "#{employee.name}"
                  rendered = "#{not employee.canEdit}"/>
            </h:column>

            <h:column>
               <f:facet name = "header">Department</f:facet>
               <h:inputText value = "#{employee.department}"
                  size = "20" rendered = "#{employee.canEdit}"/>
               <h:outputText value = "#{employee.department}"
                  rendered = "#{not employee.canEdit}"/>
            </h:column>

            <h:column>
               <f:facet name = "header">Age</f:facet>
               <h:inputText value = "#{employee.age}" size = "5"
                  rendered = "#{employee.canEdit}"/>
               <h:outputText value = "#{employee.age}"
                  rendered = "#{not employee.canEdit}"/>
            </h:column>

            <h:column>
               <f:facet name = "header">Salary</f:facet>
               <h:inputText value = "#{employee.salary}"
                  size = "5" rendered = "#{employee.canEdit}"/>
               <h:outputText value = "#{employee.salary}"
                  rendered = "#{not employee.canEdit}"/>
            </h:column>

            <h:column>
               <f:facet name = "header">Delete</f:facet>
               <h:commandButton value = "Delete"
                  action = "#{userData.deleteEmployee}" />
                  <f:setPropertyActionListener
                     target = "#{userData.employee}" value = "#{employee}"/>
               </h:commandButton>
            </h:column>

         </h:dataTable>
      </h:form>
   </h:body>
</html>

すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。

データテーブルのJSFデータ削除

任意の行の[削除]ボタンをクリックします。 出力は次のようになります。

JSF datatable1のデータを削除