留言板

p:filedownload, p:dataExporter with validation

Linh Viet Nguyen,修改在6 年前。

p:filedownload, p:dataExporter with validation

New Member 帖子: 19 加入日期: 08-2-26 最近的帖子
Hi,
I'm newbie on liferayfaces.
How to use primefaces p:filedownload, p:dataExporter with form validation?
I tried to test on primfaces application portlet (added <p:inputText name="test-validate" required="true"></p:inputText> in form 2 ):
					<h:form id="f2" enctype="multipart/form-data" prependid="false">
						<p:inputtext name="test-validate" required="true"></p:inputtext>
						<p:confirmdialog message="#{i18n['are-you-sure-you-want-to-delete-this']}" severity="alert" widgetvar="confirmation">
							<p:commandbutton id="confirm" actionListener="#{applicantBackingBean.deleteUploadedFile}" oncomplete="PF('confirmation').hide()" update="uploadedFilesTable" value="#{i18n['yes']}" />
							<p:commandbutton id="decline" onclick="PF('confirmation').hide()" value="#{i18n['no']}" type="button" />
						</p:confirmdialog>
						<h3>#{i18n['attachments']}</h3>
						<p:commandbutton id="exportButton" ajax="false" icon="ui-icon-disk" value="#{i18n['export-as-csv']}" immediate="false">
							<p:dataexporter fileName="uploaded-files" target="uploadedFilesTable" type="csv" />
						</p:commandbutton>
</h:form>

when click download with form validarion, the page reload in resource phase so the UI is broken :
http://localhost:8081/web/guest/testpage?p_p_id=1_WAR_comliferayfacesdemoprimefacesapplicantportlet310_INSTANCE_tQoCfiqurUqp&p_p_lifecycle=2
Thanks,
thumbnail
Kyle Joseph Stiemann,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
Hi Linh,
In a portlet evironement, the implementation of p:dataExporter requires the use of the RESOURCE_PHASE to export the data. Due to this restriction, other inputs including commandButtons and inputTexts (which require the ACTION_PHASE to function correctly) should not be included in the h:form alongside p:dataExporter or p:fileDownload (with the exception of ajaxified command components since Ajax requests are also executed in the RESOURCE_PHASE).

Could you provide more detail on what you are trying to accomplish (with some simple sample code)? Perhaps we can provide an alternative solution.

- Kyle
Linh Viet Nguyen,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

New Member 帖子: 19 加入日期: 08-2-26 最近的帖子
Thank Kyle,
I'm migrating our 2 mvc portlets to liferayfaces (Liferay CE 6.2).
1st portlet is:
1- Guest input data in application form.
2- When click save, system save data to database (with form validation) and export to pdf file. If not success, error message show on screen.
2nd portlet:
2- Download a report (in old search container ) with captcha validation
Are there alternative solution?
Thanks.
thumbnail
Kyle Joseph Stiemann,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
Okay, I think I've figured out a solution. Since the other form inputs must be submitted and validated via Ajax, you can include them in the form and submit them with an Ajax commandButton. Once the Ajax request is complete, you can execute some JavaScript to click on a hidden Export button to cause the data to be exported. That way the user only needs to click once to submit info and export data (although those actions will actually occur across two requests).

example.xhtml:

<h:form enctype="multipart/form-data">

<h:panelGroup id="inputs">
<p:inputText id="input" value="#{bean.inputValue}" required="true" />
<p:message for="input" />
</h:panelGroup>

<p:commandButton value="Submit + Export"
actionListener="#{bean.clickExportButtonAfterAjaxRequest}" update="inputs" process="inputs @this" />

<p:commandButton id="hiddenNonAjaxExportButton" value="hiddenNonAjaxExportButton" ajax="false"
style="display: none;">
<p:dataExporter fileName="data" target="dataTable" type="csv" />
</p:commandButton>

<p:dataTable id="dataTable" value="#{bean.data}" var="dataRow">
<!-- ... your dataTable code here ... -->
</p:dataTable>

<!-- ... your code here ... -->
</h:form>


Bean.java:

@ManagedBean(name = "bean")
@RequestScoped
public class Bean {

public void clickExportButtonAfterAjaxRequest(ActionEvent actionEvent) {

RequestContext requestContext = RequestContext.getCurrentInstance();
UIComponent component = actionEvent.getComponent();
UIComponent hiddenNonAjaxExportButton = component.findComponent("hiddenNonAjaxExportButton");
String hiddenNonAjaxExportButtonClientId = hiddenNonAjaxExportButton.getClientId();
requestContext.execute("document.getElementById('" + hiddenNonAjaxExportButtonClientId + "').click();");
}

// ... your code here ...
}


On the user's side all that they see is that they've clicked a button to submit the form and export the data (seemingly in 1 action or request), but in reality, the form was submitted and validated in an Ajax request and the data was exported in a non-Ajax request. This ensures that the data is never exported until the form inputs are valid. Then the data is exported because the Export button is clicked via JavaScript. Note that validation and model value updates will occur twice for the inputs---once for the Ajax request and once for the non-Ajax export request. You might be able to skip the first or second model value update (since the same value will be submitted in the non-Ajax export request) using FacesContext.renderResponse(), but I couldn't find an easy way to do that. Other than that duplicate update, I think this is the simplest possible way to validate/submit data and export data on the same button press with p:dataExporter. Note that validation must be performed on both the Ajax and non-Ajax export requests since a hacker could potentially click the hidden export button without clicking the Ajax one (thus submitting invalid data or bypassing validation completely if validation is disabled).

- Kyle
thumbnail
Kyle Joseph Stiemann,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
I didn't notice that you also wanted to perform captcha validation before exporting the file in your second portlet. If you simply replace the p:inputText with portal:captcha, the code will not work, since the second request will fail to validate the captcha. In order to work around this, I'd recommend un-rendering the captcha after it is validated the first time by adding the following code to the clickExportButtonAfterAjaxRequest() actionListener:

component.findComponent("captchaId").setRendered(false);

In theory, a user could fill out the captcha once and request the data export multiple times since this workaround only prompts for the captcha once, but I doubt that would be a problem. If you are extremely concerned about that being a potential problem, you could send an Ajax request after the data is exported to re-render to captcha.

- Kyle
Linh Viet Nguyen,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

New Member 帖子: 19 加入日期: 08-2-26 最近的帖子
Thank Kyle.
Tomorow, I've read carefully liferayfaces demo and found another solution using CustomResourceHandler (in jsf-export-pdf-portlet).
I executed resourceURL in form submit action like that:
ApplicationExportResource resource=new ApplicationExportResource(application);
String url = resource.getRequestPath();
RequestContext.getCurrentInstance().execute(
		"window.location.href = '" + url + "'");

But the problem is the resourceURL is easy to guess with resource primary key so i must add a secure key in getRequestPath method. Is it OK?

if (requestPath == null) {
			StringBuilder buf = new StringBuilder();
			buf.append(ResourceHandler.RESOURCE_IDENTIFIER);
			buf.append("/");
			buf.append(getResourceName());
			buf.append("?ln=");
			buf.append(getLibraryName());
			buf.append("&amp;");
			buf.append(PARAM_NAME_CUSTOMER_ID);
			buf.append("=");
			buf.append(getCustomer().getCustomerId());
			buf.append(PARAM_NAME_SECURE_KEY);
			buf.append("=");
			buf.append(getCustomer().getSecureKey());
			requestPath = buf.toString();
			requestPath = facesContext.getExternalContext().encodeResourceURL(requestPath);
		}

I also tested your solution and it work well.
Now I have 2 options to solve my problem.
Linh,
thumbnail
Kyle Joseph Stiemann,修改在6 年前。

RE: p:filedownload, p:dataExporter with validation

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
Hi Linh,
Thanks for posting an alternative solution. If you are trying to make the URL unguessable for security reasons, you may want to take a look at this StackOverflow Q&A.

- Kyle