The problem You just created a nice form with XForms. But how do you get it to load initial data, for example coming from a database or a service? You have two ways of doing this, depending on whether a push or pull method is more suitable to your situation.If you designed your form with Form Builder, rather than writing XForms by hand, see how to load an initial instance in Form Runner. The "push" solutionMany frameworks use a solution that goes like this:
In the Java world, frameworks like Apache Struts implement this type of solution. With Orbeon Forms, you can achieve something similar when using Java and the Orbeon XForms filter:
<xforms:instance id="user-data"> <registration> <first-name/> <last-name/> </registration></xforms:instance>Your Java code stores the property into the request, e.g.: request.setAttribute("my-user-data", myDataAsXML)In the XForms page, the following action reads the attribute and uses it to replace the initial content of the user-data instance:<xforms:insert ev:event="xforms-model-construct-done" nodeset="instance('user-data')"Run it and get the sourceThe "pull" solutionWith XForms, another solution often feels more natural, maybe because XForms has a natural fit for web services. It goes like this:
You do this simply with a submission that runs during form initialization: <xforms:send ev:event="xforms-model-construct-done" submission="load-data-submission"/><xforms:submission id="load-data-submission" method="get" serialization="none" resource="http://example.org/service/load-initial-form-data-pull-instance" replace="instance" instance="user-data"/>Often, initial instance data depends on parameters. A submission running during initialization can get and set URL parameters: <xforms:submission id="load-data-submission" method="get" serialization="none" resource="http://example.org/service/load-initial-form-data-pull-instance?customerId={xxforms:get-request-parameter('customerId')}" replace="instance" instance="user-data"/>The above submission passes through to the called service a parameter called customerId:
If all the submission is doing is using HTTP GET and if the URL of the service is static, you can get by with a simple src or resource attribute on <xforms:instance>:<xforms:instance id="user-data" src="http://example.org/service/load-initial-form-data-pull-instance"/>The simple <xforms:instance> solution works in scenarios where the data to retrieve is static, or only dependent on the time it is retrieved, or some parameters to the current request.NOTE: In that case, you cannot append dynamic request parameters as with <xforms:submission>.Run it and get the sourceThe "POST" solutionIf your XForms page responds to an HTTP POST containing XML, then it can access the content of the POST data with a special URL calledinput:instance:<xforms:instance id="user-data" src="input:instance"/>This results in the user-data instance being populated with the XML data posted to the XForms page. It's as easy as this!NOTE: Nothing prevents you to combine this method with getting data from the request or a service. The "dynamically generated page" solution
In theory, you could also, when using Java and/or JSP, directly insert
the right instance data into a dynamically-generated form.This however has some negative performance impact, because this means that the XForms engine can no longer cache the page definition, because it is likely to change at every request. Therefore, we do not recommend this solution, and instead using a request object or a service as described above. |