问题
When I try to render a component with f:ajax inside a multipart/form-data form, the target attribute of the form gets changed to JSFFrameId. Is this a bug?
Here is a sample code when this occur:
<h:form enctype="multipart/form-data">
<h:inputText>
<f:ajax event="change" render="@this" execute="@this"/>
</h:inputText>
<h:inputFile />
</h:form>
In my real code this is not what I'm trying to do but the same issue occurs with the sample code.
When visiting this page I get the following HTML output:
<form id="form" name="form" method="post" action="/AdApp/faces/test.xhtml" enctype="multipart/form-data">
<input type="hidden" name="form" value="form">
<input id="form-textfield" type="text" name="form-textfield" onchange="mojarra.ab(this,event,'change','@this','@this')">
<input id="form-filefield" type="file" name="form-filefield">
<input type="hidden" name="javax.faces.ViewState" id="j_id1-javax.faces.ViewState-0" value="-1920816151200438245:-3719618601043085373" autocomplete="off">
</form>
Nothing wrong here. But when the ajax event triggers, this is what happends with the HTML output(My issue? target attribute of the form):
<form id="form" name="form" target="JSFFrameId" method="POST" action="http://DOMAIN_PATH/AdApp/faces/test.xhtml" enctype="multipart/form-data">
<input type="hidden" name="form" value="form">
<input id="form-textfield" type="text" name="form-textfield" onchange="mojarra.ab(this,event,'change','@this','@this')">
<input id="form-filefield" type="file" name="form-filefield">
<input type="hidden" name="javax.faces.ViewState" id="j_id1-javax.faces.ViewState-0" value="-1920816151200438245:-3719618601043085373" autocomplete="off">
</form>
<iframe src="about:blank" id="JSFFrameId" name="JSFFrameId" width="0" height="0" frameborder="0">
<partial-response id="j_id1">
<changes>
<update id="form-textfield">
<![CDATA[<input id="form-textfield" type="text" name="form-textfield" value="das" onchange="mojarra.ab(this,event,'change','@this','@this')" />]]>
</update>
<update id="j_id1-javax.faces.ViewState-0">
<![CDATA[-1920816151200438245:-3719618601043085373]]>
</update>
</changes>
</partial-response>
</iframe>
I don't care about the iframe keeping a record of the changes. My issue is that the target of the form has been changed. So after the ajax event triggers, a submit will target a hidden iframe and not the window itself.
If I remove the enctype attribute or set ajax to render @form instead, it doesn't change the target attribute. I need the enctype="multipart/form-data" to upload files, and I dont want to use render="@form" because some of the content in my real form might have been modified by javascript code. Am I missing something? Or just bug? I will add some javascript to remove the target attribute but if I'm doing something wrong, I should use another approach.
I'm using the following tools:
Netbeans IDE 8
Glassfish 4 with Mojarra 2.2.7
回答1:
There appear to be a couple (2843, 3018, 3048) of bugs related to fileuploads and ajax-reloads. They all are marked as resolved in various versions (JSF 2.2.1 and 2.2.5). I am on 2.2.3 and I am not in charge of the JSF versioning so i'm stuck with that. I tried 2.2.8 on my development machine, but could still reproduce the problem.
Like you, i also found out that using render="@form"
leads to a problem with disappearing changes. However, if I also add execute="@form"
the changes are stored in the bean and are remain visible after the render.
来源:https://stackoverflow.com/questions/24772011/ajax-changes-target-attribute-on-multipart-form-data-form