Search code examples
javajavabeansjava-7menuitemtapestry

Tapestry 4.1 RedirectException to open external link in different tab


I've am working on a Tapestry project. I just have to work with it for now, even though Tapestry lacks supports since 2008 or so. In addition, we work with Tapestry v4.1.1 (so not even the latest version 5).

Anyway, I'm currently trying to open an external URL from a sub menu-item. We have an XML-file like this:

<beans>
  ... // Other menu-item and sub menu-items

  <bean id="thisSubmenuId" class="OurSecuredJSCookMenuItem">
    <constructor-arg type="java.lang.Object" value="Sub Menu-item Name"/>
    <constructor-arg type="java.util.Set">
      <set>
        <value type="java.lang.String">ROLE_APPLICATIONADMINISTRATOR</value>
      </set>
    </constructor-arg>
    <property name="externalLink" value="${urlAdres}" />
    <property name="id" value="65000" />
  </bean>
</beans>

In the OurSecuredJSCookMenuItem class I have the following code segments:

public class OurSecuredJSCookMenuItem extends BasicJSCookMenuItem {

    private String externalLink;        

    public void onNavigate(final IRequestCycle cycle) {
        final String externalLink = getExternalLink();
        if (externalLink != null) {
            try {
                cycle.sendRedirect(externalLink);
                return;
            } catch (final Exception ex) {
                // Tapestry is strange.. In order to redirect to an external link you have to throw a RedirectException
                // example usage: http://stackoverflow.com/questions/1642889/how-to-use-pageevent-in-tapestry4-to-sendredirect-to-another-page/1645316#1645316
                if (ex instanceof RedirectException) {
                    throw ex;
                }
            }
        }

        ... // more code irrelevant for this question
    }

    public String getExternalLink() {
        return this.externalLink;
    }

    public void setExternalLink(final String externalLink) {
        this.externalLink = externalLink;
    }
}

As you can see, the IRequestCycle#sendRedirect is used with a String url, which will throw an org.apache.tapestry.RedirectException (to open the url). This works, and it does indeed open the url in my browser. It does however navigate to the url in the open tab in which I clicked on the sub menu-item. Is there a way to open the external link in a new tab (cross-browser)?

Here are the docs for Tapestry 4.0 where you can find the IRequestCycle, sendRedirect-method and RedirectException (and more) if needed.


Solution

  • We've tried all kind of things and changed the code a lot of times, but in the end we finally have it working with only a small adjustment in comparison to the code I had in my question..

    1. I've made a sub-class of the class in my question.
    2. I've used an if (item instanceof subclass) in our Menu Java-class to use the external link component.
    3. And I've added this external link component right below the other type of a-href component in our Menu html-file.

    So very simple even though not very elegant. We simply use the exact same code as for pages, but with the external link we use a target="_blank" in the "copy-pasted" a-href component.