Author: Prince Kumar
Introduction
First of all let me make you familiar with the topics of Load static resources operation and Parse Template mule component as it lays the foundation for today’s use case.
Load static resource – It is actually the operation that serves the static content with the help of resource base path that helps in finding the static resource. It can be found under the HTTP module.
- Resource base path – where you have to give the path of your static resource
- Default file – where you have to give the default file name. Example – index.html
- Attributes – it is the default one that is already present when you drag and drop in canvas
Parse Template – It is the mule component under the core module that helps in processing the template. It also evaluates the embedded dataweave expression and renders the result.
Yes you can write the dataweave expressions inside your template and accordingly it is evaluated and replaced with their result.
It has basically five fields.
- Display Name – It is just for naming thing
- Content – Instead of defining an external file you can write your content here that contains embedded expressions.
- Location – Otherwise you can use this field to give the location of your template
- Target Variable – stores the result of evaluating the expression defined in Target Value
- Target Value – it is just a variable value
Use Case – Serves the static resource page to the user and user should fill their credential eg. name and employee code. Now we will be grabbing those details and inserting them into the database. After that, it redirects users to a new page that dynamically fetches their name.
Flow
Structure
Step 1. In the first flow, drag and drop the HTTP listener and click on the + icon to configure the connection and after that from the mule palette drag and drop the Load static resource as shown above.
Step 2. Create a package called “web” inside the src/main/resources and inside it create two html files.
- home.html and
- Thankyou.html
Click here to get the code
home.html
thankyou.html
Important points
** In home.html while submitting the form make sure that you have used the correct URL in the form action to initiate the second flow i.e. details-insertion-in-database.
formaction=”http://localhost:8081/thankyou.html”
** In thankyou.html you must ensure that you have used the correct expressions in the template.
#[vars.emp_name] to dynamically fetch the data.
Step 3. Make a second flow drag and drop the HTTP listener and configure as shown in the below.
Step 4. In the transform message I have grabbed the query parameters that are used for logging the payload.
Step 5. In the set variable I have grabbed the employee name and employee code that are used to insert the data inside the database in the next component. Also this variable will be used for writing the dataweave expressions inside the template.
Step 6. Drag and drop the insert connector in the canvas and click on the + button to configure the database connection. I have used the local database.
Database – apisero
Table – employee
Host – localhost
User – root
Step 7. Now drag and drop the parse template which can be found under the core module.
Here you need to specify the thankyou.html path. Now save all and run the application.
Code for xml file
saveempdetails.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<mule xmlns:db=”http://www.mulesoft.org/schema/mule/db” xmlns:ee=”http://www.mulesoft.org/schema/mule/ee/core”
xmlns:http=”http://www.mulesoft.org/schema/mule/http”
xmlns=”http://www.mulesoft.org/schema/mule/core” xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd”>
<flow name=”post-employee-details” doc:id=”e8233598-d53d-4c55-8a38-633c028318a3″ >
<http:listener doc:name=”/employee_details” doc:id=”2a569519-023b-4eb1-ac60-b6ea5d04b385″ config-ref=”HTTP_Listener_config” path=”/employee_details” allowedMethods=”GET”/>
<http:load-static-resource doc:name=”Load static resource” doc:id=”16601260-a17a-496f-b9fb-ffbf1cc54dc1″ resourceBasePath=”C:\Users\princekumar\AnypointStudio\studio-workspace\work\saveempdetails\src\main\resources\web” defaultFile=”home.html” />
</flow>
<flow name=”details-insertion-in-database” doc:id=”2d62a846-7d52-4283-81a4-a35091a5ef58″ >
<http:listener doc:name=”/thankyou.html” doc:id=”86b7ba1f-783f-4d78-b190-6e76d183711b” config-ref=”HTTP_Listener_config” path=”/thankyou.html”/>
<ee:transform doc:name=”Transform Message” doc:id=”c0ec55b0-8743-415e-ba77-3a6dbb6fa61f” >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
—
{
“Name”: attributes.queryParams.yourname,
“Employee Code”: attributes.queryParams.employeecode
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<set-variable value=”#[attributes.queryParams.yourname]” doc:name=”emp_name” doc:id=”46ea1a5e-83f5-48ac-bd8e-9ee6709c993f” variableName=”emp_name”/>
<set-variable value=”#[attributes.queryParams.employeecode]” doc:name=”emp_code” doc:id=”4c440bcf-cb4a-4dfd-b6c3-887c89d0271d” variableName=”emp_code”/>
<logger level=”INFO” doc:name=”payload” doc:id=”2fb1257f-1c01-4a34-ac7a-9106bb043c6f” message=”#[payload]”/>
<db:insert doc:name=”Insert” doc:id=”e2a81a48-9951-46db-b188-54cc5fc5e5b6″ config-ref=”Database_Config”>
<db:sql ><![CDATA[INSERT INTO `employee` (`Name`, `Employee_Code`) VALUES (:Emp_Name, :Emp_Code)]]></db:sql>
<db:input-parameters ><![CDATA[#[{
Emp_Name : vars.emp_name,
Emp_Code : vars.emp_code
}]]]></db:input-parameters>
</db:insert>
<parse-template doc:name=”Parse Template” doc:id=”d57783e3-674a-4787-9868-0b7b34ee7036″ location=”C:\Users\princekumar\AnypointStudio\studio-workspace\work\saveempdetails\src\main\resources\web\thankyou.html”/>
</flow>
</mule>
How to call API
- Hit this URL http://localhost:8081/employee_details
It will open like this as shown below in the screenshot.
- Submit the details here
For example I want to add James Gosling and employee code let say AMA1782
and click on the submit button.
It will redirect you to another page that looks as below.
- Now you can see that a new record is created inside the table.