Clear Native URL from browser address

0
Is there a way to clear the native URL of the last page visited if a URL has been assigned to it? For example, I have a page which I assigned a URL of resources. If I navigate to that page then go to another page that doesn’t have a URL, the browser address still shows http://localhost:8080/p/resources. I also have a custom Java RequestHandler mapped to the path imitate that imitates a user. The page that has the microflow that calls it does not have a URL, so if I call the microflow after navigating to http://localhost:8080/p/resources, it puts /p/ on the path and the resulting URL is http://localhost:8080/p/imitate which doesn’t exist. I’ve tried mapping an additional RequestHandler to p/imitate so there will be one at /imitate and /p/imitate but Core.addRequestHandler(path, requestHandler) doesn’t map to a path that has / in it. For example, I can map to imitate, but I can’t map to user/imitate or p/imitate. The problem could be solved if there was a way for a page URL to revert to the default /index.html. I’ve tried using a URL of just / and index.html, but this ends up becoming /p and /p/index.html. Any suggestions?
asked
2 answers
0

Redirecting to the next page in your case should not depend on the previously visited page, so you are better off finding the root cause of the wrong redirection to /p/imitate.

Questions:

  • before having visited  http://localhost:8080/p/resources, does it work fine?
  • which of the two request handlers is handling the call, when it goes to /p/imitate? The customJavaRequesthandler or Mendix? Use breakpoint during debugging to find out.
  • if customJavaRequesthandler: Where does it get its url?
  • How does the microflow call page imitate?

Add screenshots of the customJavaRequesthandler and the microflow call to your question.

answered
0

It looks like the root cause of the wrong redirection to /p/imitate is the behavior of Mendix when you set a URL in the page. I use the URLRedirector widget to call our custom Java RequestHandler at /imitate. I set the URL (prefix) in URLRedirector to imitate?name= 

 

 

Looking at the URLRedirector widget Javascript on GitHub, it’s using the standard Javascript function window.location.replace(url) to redirect to imitate?name=username. If the current URL is just the Mendix default http://localhost:8080/index.html then it redirects correctly to http://localhost:8080/imitate?name=username. However if the current URL is http://localhost:8080/p/resources after visiting a page with a URL assigned, it redirects to http://localhost:8080/p/imitate?name=username, which is an invalid endpoint. Mendix must have a servlet filter or handler running behind the scenes that rewrites URLs if the current URL has a /p in it. It’s not the URLRedirector widget that’s causing the problem.

The solution is for Mendix to reset the URL to the default (for example, http://localhost:8080/index.html) when navigating to any page that does not have a URL assigned to it. There is no good reason I can think of to keep the /p/ prefix in the URL after you have navigated away from a page with a URL assigned to it. I consider this a bug.

I have developed a workaround by modifying the URLRedirector.js _redirectTo function like this:

            _redirectTo : function(url) {
                /**
                 * To work around the Mendix bug when a page is assigned a URL,
                 * causing the URL to be prefixed with /p/ even when navigating
                 * to a page that does not have a URL, construct the root URL
                 * from the protocol and host and prefix it to the incoming URL.
                 * If the incoming URL contains a protocol prefix already (http...),
                 * just use it rather than generating a protocol and host prefix.
                 */
                if (url.indexOf('http') === 0) {
                    if(this.Target === "Page")
                    {
//                    window.location.href = url;
                        window.location.replace(url);
                    }
                    else
                    {
                        window.open(url);
                    }
                }
                else {
                    // String leading / from input URL, if any.
                    while ( url.charAt(0) == "/" ) url = url.substr(1);
                    // Generate the root URL from the protocol and host (e.g.; http://localhost:8080)
                    var rootUrl = window.location.protocol + "//" + window.location.host;

                    if(this.Target === "Page")
                    {
                        window.location.replace(rootUrl + "/" + url);
                    }
                    else
                    {
                        window.open(rootUrl + "/" + url);
                    }
                }
            }

 

answered