0
votes

My XPage creates a new document by default, and redirects to a new page when it passes validation.

If the user submits a page and some fields fail validation then error messages are displayed in Message controls as expected. If the user corrects the errors and the page then passes validation it redirects to the ThankYou.xsp page as expected.

However, if they then press the browser Back button they return to the XPage and see the validation errors again.

Is there a method to avoid this behaviour and, instead, reset the XPage when submitted successfully so that a fresh page is displayed if Back is pressed?

Here is the Submit button settings...

<xp:eventHandler event="onclick" submit="true" refreshMode="complete" immediate="false" save="true" id="eventHandler1">

The Navigation...

<xp:this.navigationRules> <xp:navigationRule outcome="xsp-success" viewId="/ThankYou.xsp"> </xp:navigationRule> </xp:this.navigationRules>

1

1 Answers

2
votes

I have had similar issues. In my case, it duplicated the document if the user refreshed page. I was able to solve this by using a function to redirect to the current page. Tommy Valand originally wrote this: http://dontpanic82.blogspot.com/2010/06/xpages-avoid-saving-duplicate-documents.html

I modified it to redirect to the current tab of a tabbed table. If you are not using those, then strip out that from the functionality. I believe in your case if you use this function prior to redirecting to the ThankYou page, then the back button will not show the validation errors. It will probably just show the validated document.

You will want to include this function in a SSJS code library. You call the function after you save the new document. Here is how I call it:

 redirectToCurrentDocument( false, "Invoices" )    

Invoices is the name of the tab I want to redirect to.

 function redirectToCurrentDocument( switchMode:boolean, tab){
  try {
    if( typeof currentDocument === 'undefined' || viewScope.stopRedirect ){ return;     }
    // Gets the name of the XPage. E.g. /Person.xsp
    var page = view.getPageName();

    // Finds extra parameters, strips away XPages parameters/trims leading &
    var parameters = context.getUrl().getQueryString().substring(1);
    var extraParameters = parameters.replace( 
     /([&\?^]*_[a-z0-9=]+[&]{0,1})|(tab=\w{0,}[\&]{0,1})|(action=\w{4}Document[\&]{0,1})|(documentId=[\w]{32}[\&]{0,1})/ig, '').replace( /\&$/, '' );

    // Gets the unid of the current document
    //var unid = currentDocument.getDocument().getUniversalID();
    //var unid = document1.getDocument().getUniversalID();  //changed this line to make work with multiple data sources - SJZ
    var unid = document1.getDocument().getNoteID();  //also changed to use NoteID since it gave runtime errors for new documents - SJZ

    // Sets/changes the action according according to the mode the document is in
    var isEditable = currentDocument.isEditable();
    if( switchMode ){ isEditable = !isEditable; } // false -> true / true -> false

    var action = ( isEditable ) ? 'editDocument' : 'openDocument';

    // Open the current document via a get request
    var redirectUrl = page + '?documentId=' + unid + '&action=' + action;

    if( extraParameters ){ redirectUrl += '&' + extraParameters; }

    //print("tab=" + tab)
    redirectUrl += '&tab=' + tab

    context.redirectToPage( redirectUrl );
  } catch(e){ /* Exception logging */ }
}

Hope that helps you out. It works great for me.