blaiso
blaiso

Reputation: 491

Tapestry: How to display an alert dialog from my Java code

I'm actually writing a java code in the setupRender() method. Depending of a value provided by the server side, i would like to display an Alert dialog box to the user. By clicking on ok, the application should be closed.

I have not already found how to display an Alert dialog box with tapestry. Do somebody know how to procedd?

Thanks

Upvotes: 1

Views: 627

Answers (1)

user6708591
user6708591

Reputation: 356

It's not quite clear to me what you are trying to achieve, but perhaps the following two suggestions are useful.

Suggestion 1 - Display a message using AlertManager

In the page class, inject AlertManager and add the message to it.

public class YourPage {

   @Inject
   AlertManager alertManager;

   Object setupRender() {
      // ...
      alertManager.alert(Duration.UNTIL_DISMISSED, Severity.INFO, "Love Tapestry");
   }

}

Then use the <t:alerts/> component in the page template file to have the message displayed.

Note: The user may dismiss the message, that is, make it disappear. But it doesn't 'close the application' (whatever it is that you mean by that).

Suggestion 2 - Redirect to another page

The setupRender method can return different things. For example, it could return another page class, causing a redirect to that page. On that page, you could have the messages displayed and the session subsequently invalidated (if that's what you meant by 'application should be closed'.

public class YourPage {

   Object setupRender() {
      // ...
      return AnotherPage.class;
   }

}

public class AnotherPage {

   @Inject
   Request request;

   void afterRender() {
      Session session = request.getSession(false);
      session.invalidate();
   }

}

See the Tapestry docs for details about what setupRender() can return.

Suggestion 3 - Use JavaScript to display Alert and trigger Component Event

This approach uses JavaScript to display an Alert and subsequently trigger a component event via ajax. The event handler takes care of invalidating the session.

Note: Closing the current browser windows/tab with JavaScript isn't as easy as it used to be. See this Stackoverflow question for details.

YourPage.java

public class YourPage {

    boolean someCondition;
    
    void setupRender() {
        someCondition = true;
    }
    
    @Inject
    private JavaScriptSupport javaScriptSupport;
    
    @Inject
    ComponentResources resources;
    
    public static final String EVENT = "logout";
    
    void afterRender() {
        if (someCondition) {
            Link link = resources.createEventLink(EVENT);
            JSONObject config = new JSONObject(
                    "msg", "See ya.",
                    "link", link.toAbsoluteURI()
            );
            javaScriptSupport.require("LogoutAndCloseWindow").with(config);
        }
    }
    
    @Inject Request request;
    
    @OnEvent(value = EVENT)
    void logout() {
        Session session = request.getSession(false);
        if (session != null) session.invalidate();
    }
    
}

YourPage.tml

<!DOCTYPE html>
<html
    xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd"
    xmlns:p="tapestry:parameter">
    
    <h1>Hit the Road Jack</h1>
    
</html>

LogoutAndCloseWindow.js

define(["jquery"], function($) {

    return function(config) {
        alert(config.msg);
        $.ajax({
            type: "GET",
            url: config.link
        });

        window.close(); // Legacy. Doesn't work in current browsers.
        // See https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window
    }

})

Upvotes: 0

Related Questions