JSF Adding A Table Showing All The Tasks
Adding A Table Showing All The Tasks
- Edit tool/src/webapp/tasklist/TaskList.jsp
- From:
<%@ taglib uri="http://sakaiproject.org/jsf/sakai" prefix="sakai" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <f:loadBundle basename="org.sakaiproject.tool.tasklist.bundle.Messages" var="msgs"/> <f:view> <sakai:view_container title="Task List Tool"> <sakai:view_content> <h:form id="tasks"> <sakai:view_title value="#{msgs.task_list_title}"/> <h:outputText value="Hello, #{TaskListBean.currentUserDisplayName}"/> <sakai:panel_edit> <h:inputText value="#{TaskListBean.taskText}" size="60"/> <h:commandButton value="#{msgs.task_list_add}" action="#{TaskListBean.processActionAdd}" /> </sakai:panel_edit> </h:form> </sakai:view_content> </sakai:view_container> </f:view>
- To:
<%@ taglib uri="http://sakaiproject.org/jsf/sakai" prefix="sakai" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <f:loadBundle basename="org.sakaiproject.tool.tasklist.bundle.Messages" var="msgs"/> <f:view> <sakai:view_container title="Task List Tool"> <sakai:view_content> <h:form id="tasks"> <sakai:view_title value="#{msgs.task_list_title}"/> <h:outputText value="Hello, #{TaskListBean.currentUserDisplayName}"/> <sakai:panel_edit> <h:inputText value="#{TaskListBean.taskText}" size="60"/> <h:commandButton value="#{msgs.task_list_add}" action="#{TaskListBean.processActionAdd}" /> </sakai:panel_edit> <h:dataTable id="tasklist" value="#{TaskListBean.allTasks}" var="entry" styleClass="listHier"> <h:column> <f:facet name="header"> <h:outputText value="#{msgs.task_list_owner}"/> </f:facet> <h:outputText value="#{entry.owner}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="#{msgs.task_list_text}"/> </f:facet> <h:outputText value="#{entry.task}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="#{msgs.task_list_date}"/> </f:facet> <h:outputText value="#{entry.creationDate}"> <f:convertDateTime type="both" dateStyle="medium" timeStyle="short" /> </h:outputText> </h:column> </h:dataTable> </h:form> </sakai:view_content> </sakai:view_container> </f:view>
- Added "<h:dataTable>" tag, which references the getAllTasks() method in TaskListBean.java
- The value fields in each column reference TaskImpl.java members
- e.g. The value="#{entry.owner}" fields gets its data from the getOwner() method in TaskImpl.java
- From:
- Edit TaskListBean.java
- From:
package org.sakaiproject.tool.tasklist.jsf; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sakaiproject.tool.tasklist.api.Task; import org.sakaiproject.tool.tasklist.api.TaskListService; import org.sakaiproject.tool.tasklist.impl.TaskImpl; public class TaskListBean { // use commons logger private final Log log = LogFactory.getLog(this.getClass()); private TaskListService taskListService; private String currentUserDisplayName; private String taskText; // Constructor public TaskListBean() { log.debug("Constructor"); } // IoC public void setTaskListService(TaskListService taskListService) { log.debug("setTaskListService"); this.taskListService = taskListService; currentUserDisplayName = taskListService.getCurrentUserDisplayName(); } public String getCurrentUserDisplayName() { return currentUserDisplayName; } public void setCurrentUserDisplayName(String currentUserDisplayName) { this.currentUserDisplayName = currentUserDisplayName; } public String getTaskText() { log.debug("getTaskText"); return taskText; } public void setTaskText(String taskText) { log.debug("setTaskText"); this.taskText = taskText; } public String processActionAdd() { log.debug("processActionAdd"); // Test for empty tasks and don't add them if(null != taskText && !taskText.equals("")) { Task task = new TaskImpl(); task.setOwner(taskListService.getCurrentUserId()); task.setTask(taskText); task.setCreationDate(new Date()); task.setSiteId(taskListService.getSiteId()); taskListService.addTask(task); // Reset task taskText = ""; } return "newTask"; } }
- To:
package org.sakaiproject.tool.tasklist.jsf; import java.util.Collection; import java.util.Date; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sakaiproject.tool.tasklist.api.Task; import org.sakaiproject.tool.tasklist.api.TaskListService; import org.sakaiproject.tool.tasklist.impl.TaskImpl; public class TaskListBean { // use commons logger private final Log log = LogFactory.getLog(this.getClass()); private TaskListService taskListService; private String currentUserDisplayName; private String taskText; private DataModel tasksModel; // Constructor public TaskListBean() { log.debug("Constructor"); } // IoC public void setTaskListService(TaskListService taskListService) { log.debug("setTaskListService"); this.taskListService = taskListService; currentUserDisplayName = taskListService.getCurrentUserDisplayName(); } public String getCurrentUserDisplayName() { return currentUserDisplayName; } public void setCurrentUserDisplayName(String currentUserDisplayName) { this.currentUserDisplayName = currentUserDisplayName; } public String getTaskText() { log.debug("getTaskText"); return taskText; } public void setTaskText(String taskText) { log.debug("setTaskText"); this.taskText = taskText; } public String processActionAdd() { log.debug("processActionAdd"); // Test for empty tasks and don't add them if(null != taskText && !taskText.equals("")) { Task task = new TaskImpl(); task.setOwner(taskListService.getCurrentUserId()); task.setTask(taskText); task.setCreationDate(new Date()); task.setSiteId(taskListService.getSiteId()); taskListService.addTask(task); // Reset task taskText = ""; } return "newTask"; } public DataModel getAllTasks() { log.debug("getAllTasks"); Collection tasks = taskListService.getAllTasks(taskListService.getSiteId()); tasksModel = new ListDataModel(); tasksModel.setWrappedData(tasks); return tasksModel; } }
- The TaskList.jsp dataTable makes a call to getAllTasks to retrieve a DataModel that it can render
- It uses the javax.faces.model.ListDataModel
- We are getting a Java Collection from our TaskListService and wrapp it in a javax.faces.model.ListDataModel by calling its "setWrappedData()" method.
- From:
- Screen Capture
!TaskListShowTable!