简体   繁体   English

点击后保留网址参数<h:commandButton>

[英]Keep url parameters after clicking <h:commandButton>

I've got a page which takes an id as a url parameter and uses it to get an object and display data about it. 我有一个页面,它使用id作为url参数,并使用它来获取对象并显示有关它的数据。 On this page we've got a modal which includes a file upload component, so we cannot use ajax to process the save. 在此页面上,我们有一个包含文件上传组件的模式,因此我们无法使用ajax处理保存。 Instead we do this: 相反,我们这样做:

    <rich:modalPanel domElementAttachment="parent" id="profilePanel" styleClass="popUp" resizeable="false" moveable="false" autosized="true" zindex="200" top="-10">
        <a4j:form enctype="multipart/form-data" id="profilePanelFormId">
            <q:box title="Edit Advocacy Profile" width="width6x" wantFocus="true">
                <s:div id="profilePanelForm">
                    <rich:messages rendered="#{messagesUtil.errorsExist}"/>
                    <a4j:include viewId="/panel/advocacy/advocateProfileEdit.xhtml"/>
                </s:div>
                <h:commandButton id="cancel" immediate="true" value="Cancel" action="#{advocateManager.cancelEditCommitment}" styleClass="button cancel" tabindex="100"/>
                <h:commandButton id="save" value="Save" action="#{advocateManager.saveCommitment}" styleClass="submit" tabindex="101"/>
            </q:box>
        </a4j:form>
    </rich:modalPanel>

We populate the parameter in pages.xml like so: 我们像这样在pages.xml中填充参数:

<page view-id="/advocacy/advocateCommitmentView.xhtml" login-required="true">
    <param name="confirmationCode" value="#{advocateManager.commitmentId}"/>
</page>

So far so good. 到现在为止还挺好。 The problem we're running into is when a user clicks save or cancel the url gets rewritten without the necessary id parameter. 我们遇到的问题是,当用户单击“保存”或“取消”时,URL被重写而没有必要的id参数。 If the user then refreshes the page it throws errors because it doesn't have this key. 如果用户随后刷新页面,则由于没有此键,将引发错误。 Does anyone know how I can call my save method without using ajax and still keep the url parameter or intercept the call and add the parameter back in? 有谁知道我不用ajax就能调用我的save方法,而仍然保留url参数或拦截该调用并重新添加该参数吗?

?includeViewParams=true放在操作字符串的末尾。

This happens because <h:commandButton/> posts the form, and form posts do not include parameters in the URL. 发生这种情况是因为<h:commandButton/>发布了表单,并且表单后的URL中不包含参数。 To add the parameters, create a navigation rule that redirects to the same page: 要添加参数,请创建一个重定向到同一页面的导航规则:

<navigation from-action="#{advocateManager.saveCommitment}">
    <rule>
        <redirect view-id="/same-page.xhtml" />
    </rule>
</navigation>

When doing the redirect, page parameters are encoded and added to the URL. 进行重定向时,页面参数将被编码并添加到URL。 Note that this requires that #{advocateManager.commitmentId} is populated when the redirect is done. 请注意,这要求在重定向完成后填充#{advocateManager.commitmentId}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM