JSF notifications (quick dirty tutorial)

Idea

Just simple idea, to use javascript api to jsf ajax events to show notifications from server on page after request or ajax.

<script>
jsf.ajax.addOnEvent(notifi);
<script>

and then use it for display notification on website.

First create bean class for storing messages

@Component
@Scope(value="session")
public class Notifi {
  public String msg;
  public String getMsg() {
    String tmp = msg;
    msg = ""; // reset msg
    return tmp;}
}

Next implement notifi javascript method

<script>
function notifi() {
jQuery.get(‘#{request.contextPath}’ + "/notifi.jsp", {}, function (data) {console.log(data)};
}
</script>

Then in main folder create file notifi.jsp, it will be page that will shows messages for ajax requests

<%@page contentType="text/html" pageEncoding="UTF-8"%>${widokKosci.powiadomienia.wiadomosc}

Last one, use notifi bean after first page render, for non ajax requests, for example at the bottom of template file

<script>
notifi();
</scrpt>

Finally just use Notifi bean

@Autowired
Notifi notifi;

public void action() {
notifi.msg = "Hello!";
}

..next

You could add extra data for notifications, exceptions etc.

You could use other method to show messages, rather than use “console.log” eg. pnotiffi

You could add stack for messages and other ways to manage messages.

Happy coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s