0

ZK Textbox browser cache

asked 2011-06-20 20:01:39 +0800

Fujitsu gravatar image Fujitsu
117

updated 2011-06-20 20:04:10 +0800

Hi All,

We would like the ZK Textbox to act like a normal HTML Input tag where when a user has typed text in, the browser "remembers" it the next time they enter in a similar component.

We have a logon screen which has a textbox for the Username and a textbox for the password.

<textbox id="username" value="@{fixed.username}" maxlength="12" focus="true" width="100px" />
<textbox id="password" value="@{fixed.password}" maxlength="12" fieldType="password" width="100px" />


Any ideas on how to do this?

Thanks!

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-06-21 22:41:59 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

Hi,

please read:

http://www.zkoss.org/forum/listComment/15181

Cheers, Maik

link publish delete flag offensive edit

answered 2011-06-22 19:30:16 +0800

Fujitsu gravatar image Fujitsu
117

Thanks Maik - We went with the cookie approach.

Actually found and used this example:

http://www.zkexamples.com/blog/posts/saving-login-data-to-the-browser/

SessionController.java

package com.zkexamples.blog.loginbrowsersave;
 
import javax.servlet.http.HttpSession;
 
import org.zkoss.zk.ui.Executions;
 
/**
 * Manages sessions
 */
public class SessionController {
  /**
   * Returns the session object
   *
   * @return The session
   */
  public HttpSession getSession() {
    return (HttpSession) Executions.getCurrent().getSession().getNativeSession();
  }
 
  /**
   * Gets a object from the current session
   *
   * @param name
   *          The name of the object to get.
   * @return The object
   */
  public Object getSessionObject(String name) {
    HttpSession session = getSession();
    return session.getAttribute(name);
  }
 
  /**
   * Returns true if the session instance is new. False, if not.
   *
   * @return The status of the session
   */
  public boolean sessionIsNew() {
    HttpSession session = getSession();
    return session.isNew();
  }
 
  /**
   * Sets a new session object
   *
   * @param name
   *          The name of the object.
   * @param object
   *          The object
   */
  public void setSessionObject(String name, Object object) {
    HttpSession session = getSession();
    if (session != null) {
      session.setAttribute(name, object);
    }
  }
}

CookieController.java

package com.zkexamples.blog.loginbrowsersave;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.zkoss.zk.ui.Executions;
 
/**
 * Manages cookies
 */
public class CookieController {
  /**
   * Returns the value of the cookie with the given name
   *
   * @param name
   *          The name of the cookie
   * @return Returns the value of the cookie
   */
  public String getCookie(String name) {
    Cookie[] cookies = ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getCookies();
    if (cookies != null) {
      for (Cookie cookie : cookies) {
        String cookieName = cookie.getName();
        if (cookieName.equals(name)) {
          String value = cookie.getValue();
          return value;
        }
      }
    }
    return null;
  }
 
  /**
   * Sets a new cookie
   *
   * @param name
   *          The name of the cookie
   * @param value
   *          The value of the cookie
   * @param expire
   *          The number of seconds after the cookie should expire
   */
  public void setCookie(String name, String value, int expire) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(expire);
    ((HttpServletResponse) Executions.getCurrent().getNativeResponse()).addCookie(cookie);
  }
}

index.zul

<?page title="ZK examples : Saving login data to the browser"?>
<window title="Hello user!" border="normal" width="200px" apply="com.zkexamples.blog.loginbrowsersave.ViewController">
 
  <!-- User is logged out -->
  <vbox id="vbox_logged_out" visible="true">
    Username <textbox id="tb_username" />
    Password <textbox id="tb_password" />
    <checkbox id="cb_savedata" label="Save username and password on this computer" />
    <button id="btn_login" label="Login" />
  </vbox>
 
  <!-- User is logged in -->
  <div id="div_logged_in" visible="false">
    You are logged in. <button id="btn_logout" label="Log out" />
  </div>
 
</window>

ViewController.java

package com.zkexamples.blog.loginbrowsersave;
 
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
 
/**
 * The controller class for the view
 */
public class ViewController extends GenericForwardComposer {
 
  private static final long serialVersionUID  = 3611872886887987776L;
  Checkbox                  cb_savedata;
  CookieController          cookieController  = new CookieController();
  Div                       div_logged_in;
  SessionController         sessionController = new SessionController();
  Textbox                   tb_password;
  Textbox                   tb_username;
  Vbox                      vbox_logged_out;
 
  /**
   * Tries to fill the form with data stored to cookie
   */
  private void autoFillForm() {
    // Get cookie data
    String username = cookieController.getCookie("username");
    String password = cookieController.getCookie("password");
    String storeDataChecked_str = cookieController.getCookie("storeData");
    boolean storeDataChecked = false;
    if (storeDataChecked_str != null) {
      storeDataChecked = Boolean.valueOf(storeDataChecked_str);
    }
 
    // Fill form
    if (username != null) {
      tb_username.setValue(username);
    } else {
      tb_username.setValue("");
    }
    if (password != null) {
      tb_password.setValue(password);
    } else {
      tb_password.setValue("");
    }
    cb_savedata.setChecked(storeDataChecked);
  }
 
  /**
   * Tries to automatically login the user
   */
  private void autoLogin() {
    if (isLoggedIn()) {
      // Log in the man
      switchToLoggedInView();
    } else {
      switchToLoggedOutView();
      // Try to fill the form with saved data from cookie
      autoFillForm();
    }
  }
 
  @Override
  public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);
    init();
  }
 
  /**
   * Initializes this component
   */
  private void init() {
    // Try to auto login if session is still ok
    autoLogin();
  }
 
  /**
   * Checks if the user is already logged in
   *
   * @return Returns true if the user is logged in, false if not.
   */
  private boolean isLoggedIn() {
    if (sessionController.sessionIsNew()) {
      // Return false if session is fresh
      return false;
    } else {
      // Returns the status that's set in the session object
      Object status = sessionController.getSessionObject("isLoggedIn");
      if (status == null) {
        return false;
      } else {
        return (Boolean) status;
      }
    }
  }
 
  /**
   * Logs in the user
   */
  private void login() {
    sessionController.setSessionObject("isLoggedIn", true);
    if (cb_savedata.isChecked()) {
      // Save login info to cookie
      cookieController.setCookie("username", tb_username.getValue(), 999999999);
      cookieController.setCookie("password", tb_password.getValue(), 999999999);
      cookieController.setCookie("storeData",
        String.valueOf(cb_savedata.isChecked()), 999999999);
    } else {
      // Remove login information from cookie
      cookieController.setCookie("username", null, 0);
      cookieController.setCookie("password", null, 0);
      cookieController.setCookie("storeData", null, 0);
    }
    switchToLoggedInView();
  }
 
  /**
   * Logs out the user
   */
  private void logout() {
    // Set logout flag in session and switch to login view
    sessionController.setSessionObject("isLoggedIn", false);
    autoFillForm();
    switchToLoggedOutView();
  }
 
  /**
   * Event handler. Gets called when the user clicks the login
   * button.
   */
  public void onClick$btn_login() {
    login();
  }
 
  /**
   * Event handler. Gets called when the user presses the logout button.
   */
  public void onClick$btn_logout() {
    logout();
  }
 
  /**
   * Event handler. Gets called when the user presses the enter key while the
   * textbox for the password has focus.
   */
  public void onOK$tb_password() {
    login();
  }
 
  /**
   * Event handler. Gets called when the user presses the enter key while the
   * textbox for the username has focus.
   */
  public void onOK$tb_username() {
    login();
  }
 
  /**
   * Displays the logged in view
   */
  private void switchToLoggedInView() {
    vbox_logged_out.setVisible(false);
    div_logged_in.setVisible(true);
  }
 
  /**
   * Displays the logged out view
   */
  private void switchToLoggedOutView() {
    vbox_logged_out.setVisible(true);
    div_logged_in.setVisible(false);
  }
 
}

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-06-20 20:01:39 +0800

Seen: 386 times

Last updated: Jun 22 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More