Tuesday, February 9, 2010

Login Page : Default focus on UserName field and Login operation on Enter Key

Today my task demanded me to focus on username field
Task : 1. Default focus on UserName field when page loads.
2. Login operation when user hits Enter key

Solution 1 : For a simple page the tasks becomes much easier
You just need these 2 lines of code.
af:document intialFocusId="inputText1" --- the intialFocusId property of af:document decides the focus of the field. It needs the id of the field in question.

af:inputText label="User Name" id="inputText1" clientComponent="true" ----clientComponent property should be set to true.

In my case there was a twist , where UserName field is on template and the af:document on home page that consumed the template.
Now I need to pass in the id of the inputText from template to the Home page.
I chose a backing bean way to do it.Luckily I had a backing bean associated with my template.

test.java
private String userNameId;
public void setUserNameId (String onLoadId) {
this.userNameId = userNameId; }
public String getUserNameId() {
FacesContext facesContext = FacesContext.getCurrentInstance();
String id = username.getClientId(facesContext);
this.setUserNameId(id);
return userNameId; }

The userNameId returns the Id of inputText from the template
This can be passed to the Home page something like this
af:document intialFocusId="#{test.userNameId}"

Solution 2: When user hits the enter key we need the login operationt o happen this can be achieved by setting the defaultCommand with the login button id.
something like this: af:form defaultCommand="loginButton"

Task Completed!! Time for Coffee.

6 comments:

  1. Hi
    How we can enbale autocomplete user name and password for the form of login.

    ReplyDelete
  2. Hey

    You can use the browser autocomplete functionality.
    Tools > Internet Options > Content > AutoComplete

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Excellent tip. Exactly what I was looking for. Thank you so much!

    ReplyDelete
  5. You have a bug in your code.

    public void setUserNameId (String onLoadId) {
    this.userNameId = userNameId; }

    Should be

    public void setUserNameId (String onLoadId) {
    this.userNameId = onLoadId; }

    Thanks for the tip though, I found it useful

    ReplyDelete