Adding JSF Forms to your Process Application

A working example can be found in the .

The BPMN process used for this example is shown in the image below:

In this process model we added so called form keys to

  • the Start Event “invoice received”. This is the form the user has to complete to start a new process instance.
  • the User Tasks. These are the forms the user has to complete when completing user tasks that are assigned to him.
    This is how the forms are referenced in the BPMN 2.0 XML with the attribute:

Creating Simple User Task Forms

Create a JSF page in src/main/webapp/WEB-INF representing a form used for User Tasks. A very simple task form is shown below:

  1. <!DOCTYPE HTML>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:ui="http://java.sun.com/jsf/facelets"
  4. xmlns:h="http://java.sun.com/jsf/html"
  5. xmlns:f="http://java.sun.com/jsf/core">
  6. <f:view>
  7. <h:head>
  8. <f:metadata>
  9. <f:event type="preRenderView" listener="#{camundaTaskForm.startTaskForm()}" />
  10. </f:metadata>
  11. <title>Task Form: #{task.name}</title>
  12. </h:head>
  13. <h:body>
  14. <h1>#{task.name}</h1>
  15. <h:form id="someForm">
  16. <p>
  17. Here you would see the actual form to work on the task in a design
  18. normally either matching your task list or your business application
  19. (or both in the best case).
  20. </p>
  21. <h:commandButton id="complete" value="Task Completed"
  22. action="#{camundaTaskForm.completeTask()}" />
  23. </h:form>
  24. </h:body>
  25. </f:view>
  26. </html>

Note that you need camunda-engine-cdi in order to have the camundaTaskForm bean available.

How does this work?

If the user clicks on “Start to work on task”

  • starts a conversation,
  • remembers the callback URL
  • starts the User Task in the process engine, meaning the bean sets the start date and assigns the task to the CDI business process scope (see CDI Integration for details).
    For that, you just need to add this code block to the beginning of your JSF view:
  1. <f:metadata>
  2. <f:event type="preRenderView" listener="#{camundaTaskForm.startTaskForm()}" />
  3. </f:metadata>

Submit the form by calling the camundaTaskForm bean again, which:

  • Completes the task in the process engine, causing the current token to advance in the process
  • Ends the conversation
  • Triggers a redirect to the callback URL of the tasklist

Note that the command button doesn’t have to be on the same form, you might have a whole wizard containing multiple forms in a row before having the completeTask button. This will work because of the conversation running in the background.

Access Process Variables

In the forms you can access your own CDI beans as usual and also access the Camunda CDI beans. This makes it easy to access process variables, e.g., via the processVariables CDI bean:

  1. <p>Here you would see the actual form to work on the task in some design normally either matching you task list or your business application (or both in the best case).</p>
  2. <tr>
  3. <td>
  4. Process variable <strong>x</strong> (given in in the start form):
  5. </td>
  6. <td>
  7. <h:outputText value="#{processVariables['x']}" />
  8. </td>
  9. </tr>
  10. <tr>
  11. <td>
  12. Process variable <strong>y</strong> (added in this task form):
  13. </td>
  14. <td>
  15. <h:inputText value="#{processVariables['y']}" />
  16. </td>
  17. </tr>
  18. <tr>
  19. <td></td>
  20. <td>
  21. <h:commandButton id="complete" value="Task Completed"
  22. action="#{camundaTaskForm.completeTask()}" />
  23. </td>
  24. </tr>
  25. </table>
  26. </h:form>

This is rendered to a simple form:

JSF Task Forms - 图1

The same mechanism can be used to start a new process instance:

  1. <!DOCTYPE HTML>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:ui="http://java.sun.com/jsf/facelets"
  4. xmlns:h="http://java.sun.com/jsf/html"
  5. xmlns:f="http://java.sun.com/jsf/core">
  6. <f:view>
  7. <f:metadata>
  8. <f:event type="preRenderView" listener="#{camundaTaskForm.startProcessInstanceByKeyForm()}" />
  9. </f:metadata>
  10. <h:head>
  11. <title>Start Process: #{camundaTaskForm.processDefinition.name}</title>
  12. </h:head>
  13. <h:body>
  14. <h1>#{camundaTaskForm.processDefinition.name}</h1>
  15. <p>
  16. Here you see the actual form to start a new process instance, normally
  17. this would be in some design either matching you task list or your business
  18. application (or both in the best case).
  19. </p>
  20. <table>
  21. <tr>
  22. <td>
  23. Process variable <strong>x</strong>:
  24. </td>
  25. <td>
  26. <h:inputText value="#{processVariables['x']}" />
  27. </td>
  28. </tr>
  29. <tr>
  30. <td></td>
  31. <td>
  32. <h:commandButton id="start" value="Start Process Instance"
  33. action="#{camundaTaskForm.completeProcessInstanceForm()}" />
  34. </td>
  35. </tr>
  36. </table>
  37. </h:form>
  38. </h:body>
  39. </f:view>
  40. </html>

JSF Task Forms - 图2
in the tasklist and chooses the process your start form is assigned to, he will follow a link to this form, including the processDefinitionKey and the callback URL (the URL to access the central tasklist) as GET-Parameters. Accessing this form will trigger the special CDI bean camundaTaskForm which:

  • Starts a conversation
  • Remembers the callback URL to the centralized tasklist
    You need to add this code block to the beginning of your JSF view:

Submitting the start form now:

  • Starts the process instance in the process engine
  • Ends the conversation
  • Triggers a redirect to the callback URL of the tasklist
    1. <h:commandButton id="start" value="Start Process Instance" action="#{camundaTaskForm.completeProcessInstanceForm()}" />

Note that the command button doesn’t have to be on the same form, you might have a whole wizard containing multiple forms in a row before having the completeProcessInstanceForm button. This will work because of the conversation running in the background.

Styling your Task Forms

We use in our tasklist - so best add this to your Process Application as well and you can easily polish your UI:

To include CSS and Javascript libraries in your project you can add them to your maven project as dependencies.

  1. <dependencies>
  2. <!-- ... -->
  3. <dependency>
  4. <groupId>org.webjars</groupId>
  5. <artifactId>bootstrap</artifactId>
  6. <version>3.1.1</version>
  7. </dependency>

To use them, add tags like the following ones to your JSF page. If you have several forms, it may be helpful to create a template that you can refer to from your forms to avoid redundancies..