JSF Adding A Task
Adding A Task
- 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}"/> </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:form> </sakai:view_content> </sakai:view_container> </f:view>
- Now we have to edit the TaskListBean.java file, adding the "taskText" member as well as the "processActionAdd" method.
- TaskListBean.java changes:
- From:
package org.sakaiproject.tool.tasklist.jsf; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.sakaiproject.tool.tasklist.api.TaskListService; public class TaskListBean { // use commons logger private final Log log = LogFactory.getLog(this.getClass()); private TaskListService taskListService; private String currentUserDisplayName; // 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; } }
- To:
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"; } }
- From:
- The "TaskListBean.processActionAdd()" method returns a string witch, is used by the JSF framework to navigate to the same or to a different JSF page
- These navigation rules are setup in "tool/src/webapp/WEB-INF/faces-config.xml"
- Navigation rules:
<!-- Navigation Rules --> <navigation-rule> <from-view-id>/tasklist/TaskList.jsp</from-view-id> <navigation-case> <from-outcome>newTask</from-outcome> <to-view-id>/tasklist/TaskList.jsp</to-view-id> </navigation-case> </navigation-rule>
- The "processActionAdd()" method returns the "newTask" string, which matches the navigation case "<from-outcome>newTask</from-outcome>" in the above code sample
- Screen Capture
!TaskList-AddingTask!