1

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

user6708591
  • 339
  • 3
  • 10
blaiso
  • 347
  • 4
  • 6
  • 15
  • Please clarify what you mean by 'the application should be closed'. Is the browser window/tab supposed to close? Or the user session to be terminated? Or perhaps you meant to write 'alert box', not 'application'? – user6708591 Mar 17 '22 at 21:09
  • In fact, It's both. By clicking on the Ok button of alert dialog box, the user session should be terminated and the browser window tab closed – blaiso Apr 07 '22 at 11:09
  • Sounds like you are looking for a JavaScript-based solution. I added a third suggestion, using JavaScript, to my answer. – user6708591 Apr 07 '22 at 20:15

1 Answers1

0

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
    }

})
user6708591
  • 339
  • 3
  • 10