Sunday, September 26, 2010

Javascript: How to fix stopped animated gif after submit a form in IE

There is a known limitation in IE where the animated gif will stop animate after submit a form. This would give a problem to us if we have a situation where we want to show a loading image while waiting for the process to complete and it's non-ajax.

Below is the solution and it works in IE, FF and Chrome:
javascript:
function showIndicator() {
   var pb = document.getElementById("loading_image");
   pb.innerHTML = '';
   pb.style.display = '';
}
html:


source : http://stackoverflow.com/questions/780560/animated-gif-in-ie-stopping

Tuesday, September 21, 2010

My favorite shortcuts in Eclipse

Below are the shortcuts that I like to use in Eclipse while doing my project.

ctrl + shift + r --> to search for specific file name within my projects.
ctrl + f : to search for specific words or when I want to replace certain words.
ctrl + h --> file search : searching for specific words that reside in any files within my project.
ctrl + i : to easily fix indentation in my code
ctrl + / : to easily comment certain lines of codes
right click --> source : all the functions that make jobs easy such as generate element comment,generate getter setter,generate constructor etc.

Thursday, September 9, 2010

oracle : display all tables in database


SELECT owner, table_name FROM all_tables order by OWNER ;

Tuesday, September 7, 2010

JSF 2 In and Out Injection with @ManagedProperty

Usually during login proses, we validate the user credential, fetch the user details from database and store it in a session for the future use. Normally HttpSession is used to achieve this goal. In Seam we do this injection by using @In and @Out annotation, in JSF 2, we use @ManagedProperty.

Consider we have a login bean with request scope and a user session bean with session scope. So it would be like this :
1.user session bean
@ManagedBean(name="userSession")
@SessionScoped
public class UserSession {

   private String username;
   //getter and setter for username
}

2.login bean
@ManagedBean(name="loginActionBean")
@RequestScoped
public class loginActionBean { 

   @ManagedProperty(value = "#{userSession}")
   private UserSession userSession ;
   //getter and setter for userSession

   private String username;
   //getter and setter for username

   public String authenticate() {
      userSession.setUsername(this.username);
      return "nextpage.jsf";
   }
}

then we can get the user session value in other managed beans or our view(xhtml) by calling something like userSession.getUsername or #{userSession.username} . Again, @ManagedProperty(value = "#{userSession}") with its getter and setter must be set in the managed bean where we want to use the session.

Alternatively, we can also use FacesContext to set and get the session.

How to easily break from the inner and outer loop

This how to break from the inner and outer loop.
search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length; j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search;
            }
        }
    }

reference:
Refer to: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html

JSF 2 :my application issue with browser back button

I created a simple app where only logged in users are able to view the page. So :
1st, I implemented a filter that checks every page if the login session is alive before continue , else it will be redirected to the login page.
2nd, To prevent users from using back button to get the previous page without login, I implemented a phase listener that will set the page header using pragma no cache.

But then I found a security flaw when testing my app using FF3.0+ browser. At first, it worked as expected where pages couldn't be accessed using back button, but after 5 trials of clicking the back button, surprisingly I was able to go back to the previous page and called the action method! When I debugged it, it did go through the filter check but the login session was alive again even though it was before null and invalidated using the session.invalidate() when logout.

I did a guess that it has something to do with the default JSF 2 behavior where URL is not updated in the URL bar even though the server forwarded the navigation URL since my filter depends on the page URL to check the login session. So I added faces-redirect=true parameter at the end of the navigation URL and it solved the problem.

But another problem came. My request scope bean didn't work since adding faces-redirect=true will make the server called twice where request scope only survives a call. To solve this problem I used flash scope where it survives 2 calls - a complete redirect with URL gets updated in the URL bar.

reference:
http://www.jsfsummit.com/blog/max_katz/2010/07/learning_jsf2_using_flash_scope

JSF 2 invoke action method on page load

There are several ways to automatically invoke back-end bean/action in JSF 2 on page load or when reach a specific URL. The ways that I know are :

1. using annotation @PostConstruct
If we annotate a method with @PostConstruct, it will be called at bean initialization.
   @PostConstruct
   public void init() {
      //code here
   }

2. using f:event.

   


3. using binding attribute
JSF 2 tags which have binding attibute such as h:form ,h:panelGroup, h:commandLink will invoke the method that binds to these elements.



It's important to understand the scope concept in JSF 2 to get the desired result. We might want to load the action method once and then proceed with another logic that trigger another action in the same page but end up calling this on load action method every time before invoke the real method that we want. Check the scope.

If it's request scope then it will be called each time we invoke any methods in the page since request scope only lasts for a call and the bean will be initialized again in the next call. Change it to view scope or session scope will extend the life time of the bean.