TAP5-2458: fix redirect to current page from page activation method #close
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AjaxComponentEventRequestHandler.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AjaxComponentEventRequestHandler.java
index c4a852e..4862ebd 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AjaxComponentEventRequestHandler.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/AjaxComponentEventRequestHandler.java
@@ -87,12 +87,12 @@
 
         queue.setRenderingPage(activePage);
 
+        request.setAttribute(InternalConstants.PAGE_NAME_ATTRIBUTE_NAME, parameters.getActivePageName());
+
         if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters
                 .getPageActivationContext(), interceptor))
             return;
 
-        request.setAttribute(InternalConstants.PAGE_NAME_ATTRIBUTE_NAME, parameters.getActivePageName());
-
         Page containerPage = cache.get(parameters.getContainingPageName());
 
         ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
diff --git a/tapestry-core/src/test/app1/OnActivateRedirect.tml b/tapestry-core/src/test/app1/OnActivateRedirect.tml
new file mode 100644
index 0000000..89ffdcd
--- /dev/null
+++ b/tapestry-core/src/test/app1/OnActivateRedirect.tml
@@ -0,0 +1,6 @@
+<html t:type="border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+
+  Message: <span id="message">${message}</span>
+
+  <t:actionlink id="link" async="true">perform AJAX request</t:actionlink>
+</html>
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/AjaxTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/AjaxTests.java
index 5d3ca65..830ad92 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/AjaxTests.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/AjaxTests.java
@@ -271,4 +271,17 @@
         waitForElementToAppear("content2");
         assertText("content2", "Music Library");
     }
+
+    /**
+     * TAP5-2458
+     */
+    @Test
+    public void redirect_from_page_activation_method()
+    {
+        openLinks("OnActivateRedirect Demo");
+
+        clickAndWait("link");
+
+        assertText("message", "Redirected from XHR");
+    }
 }
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
index a12ce0b..79aa58d 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java
@@ -597,8 +597,12 @@
                     new Item("textfieldwithnullvalidateparameter", "TextField with null validate parameter", "A TextField whose validate parameter is bound to null"),
 
                     new Item("validateInErrorEvent", "Validate in error Event", "A form that trigger validate in " +
-                            "error event on submit when textfield is empty")
-            );
+                            "error event on submit when textfield is empty"),
+
+                    new Item("onactivateredirect", "OnActivateRedirect Demo", "A page that redirects to itself from"
+                        + " its activation method")
+
+                );
 
     static
     {
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/OnActivateRedirect.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/OnActivateRedirect.java
new file mode 100644
index 0000000..557f17b
--- /dev/null
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/OnActivateRedirect.java
@@ -0,0 +1,29 @@
+package org.apache.tapestry5.integration.app1.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.services.Request;
+
+public class OnActivateRedirect {
+
+  @Persist("flash")
+  @Property(write = false)
+  private String message;
+
+  @Inject
+  private Request request;
+
+  Object onActivate() {
+    if (request.isXHR()) {
+      message = "Redirected from XHR";
+      return this;
+    }
+    return null;
+  }
+
+  void onAction() {
+
+  }
+
+}