JSF ViewParam Required +AJAX breaks page

巧了我就是萌 提交于 2019-11-29 21:12:12

问题


When I click the command button on the page below navigations fails. (Clicking on the button refreshes the page, removes the URL parameter and displays the required error message instead of navigating to the index page)

However if I remove the required attribute OR I remove the f:ajax tag the navigation works fine.

Using com.sun.faces JSF 2.1.13 and primefaces 3.4.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head></h:head>

 <f:metadata>
        <f:viewParam name="product"
            required="true" requiredMessage="Bad request. Please use a link from within the system."/>
    </f:metadata>

<body>

    <h:form id="form">
        <h:selectBooleanCheckbox>
            <f:ajax update="form" />
        </h:selectBooleanCheckbox>
        <h:commandButton value="buy" action="/index.xhtml?faces-redirect=true" />
    </h:form>

</body>
</html>

回答1:


I'll assume that the update="form" is a careless oversimplification and that you actually meant to use render="@form" and confused it with PrimeFaces or so.

Coming back to the concrete problem, that's caused by performing a non-ajax postback which triggers the processing of view parameters as well.

Either make it required on non-postback only (make sure that the bean is view scoped though),

<f:viewParam ... required="#{not facesContext.postback}" />

or pass it on postback as well

<h:commandButton ...>
    <f:param name="product" value="#{bean.product}" />
</h:commandButton>

or use OmniFaces <o:viewParam> instead if you're already using a view scoped bean. It won't process the view parameter on postbacks.

<... xmlns:o="http://omnifaces.org/ui">
...
<o:viewParam ... />


来源:https://stackoverflow.com/questions/14026702/jsf-viewparam-required-ajax-breaks-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!