> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aseeflow.org/llms.txt
> Use this file to discover all available pages before exploring further.

# JSF Task Forms

## Adding JSF Forms to your Process Application

<Info>
  **Heads-up!**

  The required CDI beans for this functionality are currently not available in [Quarkus applications](/user-guide/quarkus-integration/cdi-integration#task-form-beans) out of the box.
</Info>

If you add JSF forms as described below, you can easily use them as <br />
[external task forms](/user-guide/task-forms#external-task-forms).

A working example can be found in the [examples repository](https://github.com/camunda/camunda-bpm-examples/tree/master/usertask/task-form-external-jsf).

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

<img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/task-form-process.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=e8e1e094ec592a2ce3f4c95ab1eab921" alt="Task Form Process" width="937" height="273" data-path="user-guide/task-forms/img/task-form-process.png" />

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 `camunda:formKey` attribute:

```xml theme={null}
<startEvent id="start" name="invoice received"
    camunda:formKey="app:sample-start-form.jsf"/>

<userTask id="categorize-invoice" name="Categorize Invoice"
    camunda:formKey="app:sample-task-form-1.jsf" />

<userTask id="file-invoice" name="File Invoice"
    camunda:formKey="app:sample-task-form-2.jsf" />

<userTask id="acknowledge-categorization" name="Acknowledge Categorization"
    camunda:formKey="app:acknowledge-form.jsf" />
```

## 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:

```xml theme={null}
<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">

<f:view>
  <h:head>
    <f:metadata>
      <f:event type="preRenderView" listener="#{camundaTaskForm.startTaskForm()}" />
    </f:metadata>
    <title>Task Form: #{task.name}</title>
  </h:head>
  <h:body>
    <h1>#{task.name}</h1>
    <h:form id="someForm">
      <p>
        Here you would see the actual form to work on the task in a design
        normally either matching your task list or your business application
        (or both in the best case).
      </p>

      <h:commandButton id="complete" value="Task Completed"
          action="#{camundaTaskForm.completeTask()}" />
    </h:form>
  </h:body>
</f:view>
</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" <img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/start-task-button.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=d32ef0900740a0cf6ad99d0c22b55ec2" alt="Start Task Button" width="24" height="19" data-path="user-guide/task-forms/img/start-task-button.png" /> in the tasklist, he will follow a link to this form, including the taskId 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
* 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](/user-guide/cdi-java-ee-integration) for details).

For that, you just need to add this code block to the beginning of your JSF view:

```xml theme={null}
<f:metadata>
  <f:event type="preRenderView" listener="#{camundaTaskForm.startTaskForm()}" />
</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

```xml theme={null}
<h:commandButton id="complete" value="task completed" action="#{camundaTaskForm.completeTask()}" />
```

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 ASEE Flow CDI beans. This makes it easy to access process variables, e.g., via the `processVariables` CDI bean:

```xml theme={null}
<h:form id="someForm">
  <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>
  <table>
    <tr>
      <td>
        Process variable <strong>x</strong> (given in in the start form):
      </td>
      <td>
        <h:outputText value="#{processVariables['x']}" />
      </td>
    </tr>
    <tr>
      <td>
        Process variable <strong>y</strong> (added in this task form):
      </td>
      <td>
        <h:inputText value="#{processVariables['y']}" />
      </td>
    </tr>
    <tr>
      <td></td>
      <td>
        <h:commandButton id="complete" value="Task Completed"
            action="#{camundaTaskForm.completeTask()}" />
      </td>
    </tr>
  </table>
</h:form>
```

This is rendered to a simple form:

<img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/variablesTaskFormExample.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=a111f244b5ad7fb57ad1e05a025ce228" alt="Varibales Task Form Example" width="544" height="326" data-path="user-guide/task-forms/img/variablesTaskFormExample.png" />

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

```xml theme={null}
<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">

<f:view>
  <f:metadata>
    <f:event type="preRenderView" listener="#{camundaTaskForm.startProcessInstanceByKeyForm()}" />
  </f:metadata>
  <h:head>
    <title>Start Process: #{camundaTaskForm.processDefinition.name}</title>
  </h:head>
  <h:body>
    <h1>#{camundaTaskForm.processDefinition.name}</h1>
    <p>Start a new process instance in version: #{camundaTaskForm.processDefinition.version}</p>
    <h:form id="someForm">
      <p>
        Here you see the actual form to start a new process instance, normally
        this would be in some design  either matching you task list or your business
        application (or both in the best case).
      </p>

      <table>
        <tr>
          <td>
            Process variable <strong>x</strong>:
          </td>
          <td>
            <h:inputText value="#{processVariables['x']}" />
          </td>
        </tr>
        <tr>
          <td></td>
          <td>
            <h:commandButton id="start" value="Start Process Instance"
                action="#{camundaTaskForm.completeProcessInstanceForm()}" />
          </td>
        </tr>
      </table>
    </h:form>
  </h:body>
</f:view>
</html>
```

<img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/startFormExample.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=f7fa5dc860f66ef0bf90f87062fce345" alt="Start Form Example" width="544" height="356" data-path="user-guide/task-forms/img/startFormExample.png" />

If the user clicks on "Start a process instance" <img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/start-process-button.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=0c5f8f52dca8b2d5dfec51a42d7669e1" alt="Start Process Button" width="19" height="19" data-path="user-guide/task-forms/img/start-process-button.png" /> 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:

```xml theme={null}
<f:metadata>
  <f:event type="preRenderView" listener="#{camundaTaskForm.startProcessInstanceByIdForm()}" />
</f:metadata>
```

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

```xml theme={null}
<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 [Twitter Bootstrap](http://getbootstrap.com/) in our tasklist - so best add this to your Process Application as well and you can easily polish your UI:

<img src="https://mintcdn.com/aseeflow/EOwJaxQ7KmXM1Gws/user-guide/task-forms/img/tasklist-forms-layouted-start.png?fit=max&auto=format&n=EOwJaxQ7KmXM1Gws&q=85&s=e3d0cf3441fe615bd0e2e9376ec2b721" alt="Tasklist Start Forms layouted" width="573" height="425" data-path="user-guide/task-forms/img/tasklist-forms-layouted-start.png" />

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

```xml theme={null}
<dependencies>

  <!-- ... -->

  <dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.1.1</version>
  </dependency>

</dependencies>
```

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..

```xml theme={null}
<h:head>
  <title>your title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <!-- CSS Stylesheets -->
  <h:outputStylesheet library="webjars/bootstrap/3.1.1/css" name="bootstrap.css"/>
  <h:outputStylesheet library="css" name="style.css"/>

  <!-- Javascript Libraries -->
  <h:outputScript type="text/javascript" library="webjars/bootstrap/3.1.1/js" name="bootstrap.js" />
</h:head>
```
