Problem pre-filling form with get-request-attribute () and JSP

23 views
Skip to first unread message

Patxi Alba

unread,
Jul 10, 2020, 5:10:31 AM7/10/20
to Orbeon Forms
Hi,

I am having trouble being able to pass an XML to pre-fill a form, when using the JSP integration.

I have tried different XML combinations with no result, the form always appears without pre-filling. Any idea where the error is?

Greetings and thanks.

JSP Code:

<%@ page
    pageEncoding="utf-8"
    contentType="text/html; charset=UTF-8"
    import="org.orbeon.oxf.fr.embedding.servlet.API"
    import="java.util.HashMap" %>
<!DOCTYPE HTML>
<html>
<body>
    <%   
   
    //String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><form><seccio1><camp1>mola</camp1><camp2>trola</camp2></seccio1></form>";
    //String xml = "<form><seccio1><grid-1><camp1>mola</camp1><camp2>trola</camp2></grid-1></seccio1></form>";
    
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><form><seccio1><grid-1><camp1>mola</camp1><camp2>trola</camp2></grid-1></seccio1></form>";          
           
        request.setAttribute("dades", xml);
        
        API.embedFormJava(
            request,            // HttpServletRequest: incoming HttpServletRequest
            out,                // Writer: where the embedded form is written
            "Proves",   // String: Form Runner app name
            "senzill2",          // String: Form Runner form name
            "new",              // String: Form Runner action name
            null,               // String: Form Runner document id (optional)
            null,               // String: query string (optional)
            null                // Map<String, String>: custom HTTP headers (optional)
        );
    %>
</body>
</html>

XForm code:

<xh:html xmlns:xh="http://www.w3.org/1999/xhtml"
         xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
         xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
         xmlns:saxon="http://saxon.sf.net/"
         xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
         xmlns:xf="http://www.w3.org/2002/xforms"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:sql="http://orbeon.org/oxf/xml/sql"
         xmlns:ev="http://www.w3.org/2001/xml-events"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         xmlns:exf="http://www.exforms.org/exf/1-0"
         xmlns:array="http://www.w3.org/2005/xpath-functions/array"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:fb="http://orbeon.org/oxf/xml/form-builder"
         xmlns:map="http://www.w3.org/2005/xpath-functions/map"
         xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
         fr:data-format-version="4.0.0">
    <xh:head>
        <xh:title>Formulari senzill2 per pre-omplir per request-attribute</xh:title>
        <xf:model id="fr-form-model" xxf:expose-xpath-types="true" xxf:analysis.calculate="true">

            <!-- Main instance -->
            <xf:instance id="fr-form-instance" xxf:exclude-result-prefixes="#all" xxf:index="id">
                <form>
                    <seccio1>
                        <grid-1>
                            <camp1/>
                            <camp2/>
                        </grid-1>
                    </seccio1>
                </form>
            </xf:instance>
            <xf:insert event="xforms-model-construct-done" ref="instance('fr-form-instance')"
                       origin="xxf:get-request-attribute('dades')"/>
            <!-- Bindings -->
            <xf:bind id="fr-form-binds" ref="instance('fr-form-instance')">
                <xf:bind id="seccio1-bind" name="seccio1" ref="seccio1">
                    <xf:bind id="grid-1-bind" ref="grid-1" name="grid-1">
                        <xf:bind id="camp1-bind" name="camp1" ref="camp1" xxf:whitespace="trim"/>
                        <xf:bind id="camp2-bind" ref="camp2" name="camp2" xxf:whitespace="trim"/>
                    </xf:bind>
                </xf:bind>
            </xf:bind>

            <!-- Metadata -->
            <xf:instance id="fr-form-metadata" xxf:readonly="true" xxf:exclude-result-prefixes="#all">
                <metadata>
                    <application-name>Proves</application-name>
                    <form-name>senzill2</form-name>
                    <title xml:lang="en">Formulari senzill2 per pre-omplir per request-attribute</title>
                    <description xml:lang="en"/>
                    <created-with-version>2019.2.0.201912301747 CE</created-with-version>
                    <updated-with-version>2019.2.0.201912301747 CE</updated-with-version>
                    <library-versions>
                        <orbeon>1</orbeon>
                    </library-versions>
                </metadata>
            </xf:instance>

            <!-- Attachments -->
            <xf:instance id="fr-form-attachments" xxf:exclude-result-prefixes="#all">
                <attachments/>
            </xf:instance>

            <!-- All form resources -->
            <xf:instance xxf:readonly="true" id="fr-form-resources" xxf:exclude-result-prefixes="#all">
                <resources>
                    <resource xml:lang="en">
                        <seccio1>
                            <label>Secció 1</label>
                        </seccio1>
                        <camp1>
                            <label>Camp1</label>
                            <hint/>
                        </camp1>
                        <camp2>
                            <label>Camp2</label>
                            <hint/>
                        </camp2>
                    </resource>
                </resources>
            </xf:instance>
        </xf:model>
    </xh:head>
    <xh:body>
        <fr:view>
            <fr:body xmlns:xbl="http://www.w3.org/ns/xbl" xmlns:p="http://www.orbeon.com/oxf/pipeline"
                     xmlns:oxf="http://www.orbeon.com/oxf/processors">
                <fr:section id="seccio1-section" bind="seccio1-bind">
                    <xf:label ref="$form-resources/seccio1/label"/>
                    <fr:grid id="grid-1-grid" bind="grid-1-bind">
                        <fr:c y="1" x="1" w="6">
                            <xf:input id="camp1-control" bind="camp1-bind">
                                <xf:label ref="$form-resources/camp1/label"/>
                                <xf:hint ref="$form-resources/camp1/hint"/>
                                <xf:alert ref="$fr-resources/detail/labels/alert"/>
                            </xf:input>
                        </fr:c>
                        <fr:c y="1" x="7" w="6">
                            <xf:input id="camp2-control" bind="camp2-bind">
                                <xf:label ref="$form-resources/camp2/label"/>
                                <xf:hint ref="$form-resources/camp2/hint"/>
                                <xf:alert ref="$fr-resources/detail/labels/alert"/>
                            </xf:input>
                        </fr:c>
                    </fr:grid>
                </fr:section>
            </fr:body>
        </fr:view>
    </xh:body>
</xh:html>

Alessandro Vernet

unread,
Jul 10, 2020, 3:08:00 PM7/10/20
to orb...@googlegroups.com
Hi Patxi,

The Java embedding API makes an HTTP request to Orbeon Forms, so the
`HttpServletRequest` object Orbeon Forms receives isn't the same you have in
your code; you just can't set a request attribute on it, an expect it to
arrive on the other side.

You can either pass values by request parameters or headers. In your case, I
would use a header, which you can access from the other side with
`xxf:get-request-header()`. You'll let me know if this works for you!

‑Alex

-----
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
--
Sent from: http://discuss.orbeon.com/

Patxi Alba

unread,
Jul 13, 2020, 7:28:23 AM7/13/20
to Orbeon Forms
Yes, this works for me using saxon:parse(), whithout this don't fill data, this is correct?

<xf:insert event="xforms-model-construct-done" ref="instance('fr-form-instance')"
                       origin="saxon:parse(xxf:get-request-header('dades','UTF-8'))"/>

Otra pregunta es: ¿es posible poner este código en la plantilla de sección? para que secciones se puedan pre-rellenar con los datos sin intervención del usuario en el edit source del form-builder.


Alessandro Vernet

unread,
Jul 13, 2020, 2:51:13 PM7/13/20
to orb...@googlegroups.com
Hi Patxi,

Yes, you indeed need to call `saxon:parse()`, and you code looks correct.
And you can't put this in a section template, but you might be able to put
it in a custom model (see link below), where you can put your custom XForms
code that gets "inserted" into the forms you'd like.

https://doc.orbeon.com/form-runner/advanced/custom

You'll let me know if this works for you.

Patxi Alba

unread,
Jul 20, 2020, 2:59:46 AM7/20/20
to Orbeon Forms
Yes, it has worked.

At the moment it seems more interesting to put a get-request-header() in each field of the template. 
In that way don't have to know the variable part of the form.

Thanks

Patxi Alba

unread,
Jul 20, 2020, 6:56:45 AM7/20/20
to Orbeon Forms
One more question.

We have a template with a section with read-only fields that is filled by the headers. But if we hide this section, the XML it sends to the servlet does not include this section.

I have a way to hide the section, but the secction comes in the result XML?

Alessandro Vernet

unread,
Jul 20, 2020, 7:56:33 PM7/20/20
to orb...@googlegroups.com
Hi Patxi,

I imagine that you are using `send()`, in which case use the `nonrelevant =
"keep"` parameter (see link below).

https://doc.orbeon.com/form-runner/advanced/buttons-and-processes/actions-form-runner/actions-form-runner-send

Patxi Alba

unread,
Jul 21, 2020, 5:45:31 AM7/21/20
to Orbeon Forms
There is a small problem with the keept.

It seems that since it has no visibility, it is not executing the formula, which fills in the fields with the header, nor initial value and / or calculated value.

Its sending the full XML but with the no visibility fields empty, is there any way to force them to execute the get-recuest-headers ()?

Alessandro Vernet

unread,
Jul 21, 2020, 8:13:11 PM7/21/20
to orb...@googlegroups.com
Hi Patxi,

Are you saying that you a hidden field calculate as
`xxf:get-request-header(…)`, and that the value isn't present in the data
when it is sent? If so, is it present when the data is saved, in the
database? If so, can you check if making the field always visible makes a
difference?

Patxi Alba

unread,
Jul 22, 2020, 5:09:25 AM7/22/20
to Orbeon Forms
Yes, but it only happens with a hidden template section.

The data not come when doing a GET to the orbeon CRUD.

Explain, we have a template section with the fields filled in with the get-request-headers (). If we hide this section that comes from the template it doesn't preload them, the empty XML tags come when forcing the keept.
However, if you hide a section that does not come from a template, it preloads the data. It is more if what is hidden is a field of the template if it preloads it.

I am testing with the orbeon version: 2019.2.0.201912301747 CE, using the embedded eXist database built into a tomcat.

Server version: Apache Tomcat/9.0.33
Server built:   Mar 11 2020 09:31:38 UTC
Server number:  9.0.33.0
OS Name:        Windows 10
OS Version:     10.0
Architecture:   amd64
JVM Version:    1.8.0_211-b12
JVM Vendor:     Oracle Corporation

Alessandro Vernet

unread,
Jul 22, 2020, 7:04:29 PM7/22/20
to orb...@googlegroups.com
Hi Patxi,

Got it: this is because calculations are not happening for hidden section
templates. Others have stumbled upon this in the past, and I've created
issue #4620 for this (see 1st link below). As a workaround, you could merge
the section template into the form (see 2nd link below). You'll let me know
if this works for you.

https://github.com/orbeon/orbeon-forms/issues/4620
https://doc.orbeon.com/form-builder/advanced/section-templates#merging-section-templates

Patxi Alba

unread,
Jul 24, 2020, 6:51:06 AM7/24/20
to Orbeon Forms
Hi Alessandro.

Yes, this has worked. 

But in the future we plan to integrate HTTP services and DB access into templates, it would be interesting to be able to hide the sections and incorporate services into the template.

Alessandro Vernet

unread,
Jul 24, 2020, 9:13:59 PM7/24/20
to orb...@googlegroups.com
Hi Patxi,

If you don't have any "private" information in the fields of the section
template, an alternative would be to keep the section template visible at
the level of Form Builder, and hide it with CSS. Users will be able to see
the values if they look at the DOM sent to the browser, but not change the
values, if you use Calculated Value fields. Would that work for your use
case?

Patxi Alba

unread,
Jul 29, 2020, 3:29:10 AM7/29/20
to Orbeon Forms
Yes, for now it works. We do not have private data in the sections.

Greetings and thanks.

Alessandro Vernet

unread,
Jul 29, 2020, 1:33:22 PM7/29/20
to orb...@googlegroups.com
Excellent, I'm glad CSS did the trick, and thank you for the update.
Reply all
Reply to author
Forward
0 new messages