Skip to content
jumperchen edited this page May 7, 2012 · 1 revision

How to use

Simply put theme jar in WEB-INF/lib directory.

How to switch different themes dynamically

The default theme provider checks the current theme settings in order of preferences;

  • First priority: Cookie Set cookie zktheme=name
    For instance, write cookie zktheme=breeze, breeze theme will be activated

  • Second priority: Library property The default theme provider will obtain the current theme setting from library property if cookie is empty.

    org.zkoss.theme.preferred name

For example, the following code will activiate the breeze theme.

<library-property>
 <name>org.zkoss.theme.preferred</name>
     <value>breeze</value>
</library-property>
  • Third priority: System's default theme The default theme provider maintains a default theme. If theme provider can't find theme setting in cookie or library property, default theme will be activated.

Appendix

Sample code

Set cookie

You can use the Themes util class that is attached in theme package to set cookie. The following is the sample code to set and obtain cookie.

String THEME_COOKIE_KEY = "zktheme";
/**
 * Sets the theme style in cookie
 */
public static void setTheme (Execution exe, String theme) {
	Cookie cookie = new Cookie(THEME_COOKIE_KEY, theme);
	cookie.setMaxAge(60*60*24*30); //store 30 days
	String cp = exe.getContextPath();
	// if path is empty, cookie path will be request path, which causes problems
	if(cp.length() == 0)
		cp = "/";
	cookie.setPath(cp);
	((HttpServletResponse)exe.getNativeResponse()).addCookie(cookie);
}

/**
 * Returns the theme specified in cookies
 * @param exe Execution
 * @return the name of the theme or "" for default theme.
 */
public static String getTheme (Execution exe) {
	Cookie[] cookies = 
		((HttpServletRequest)exe.getNativeRequest()).getCookies();
	if(cookies == null) 
		return "";
	for(int i=0; i < cookies.length; i++){
		Cookie c = cookies[i];
		if(THEME_COOKIE_KEY.equals(c.getName())) {
			String theme = c.getValue();
			if(theme != null) 
				return theme;
		}
	}
	return "";
}

###Dynamically switch themes using cookie### Here are a list of cookie sample codes that you can choose from.

  • Breeze

      Themes.setTheme(Executions.getCurrent(), "breeze");
      Executions.sendRedirect(null);
    
  • Sapphire

      Themes.setTheme(Executions.getCurrent(), "sapphire");
      Executions.sendRedirect(null);
    
  • Silvertail

      Themes.setTheme(Executions.getCurrent(), "silvertail");
      Executions.sendRedirect(null);
    
  • Classic blue

      Themes.setTheme(Executions.getCurrent(), "classicblue");
      Executions.sendRedirect(null);
    

###Dynamically switch themes using library property###

  • Breeze

      Library.setProperty("org.zkoss.theme.preferred", "breeze");	
      Executions.sendRedirect(null);
    
  • Sapphire

      Library.setProperty("org.zkoss.theme.preferred", "sapphire");
      Executions.sendRedirect(null);
    
  • Silvertail

      Library.setProperty("org.zkoss.theme.preferred", "silvertail");
      Executions.sendRedirect(null);
    
  • Classic blue

      Library.setProperty("org.zkoss.theme.preferred", "classicblue");
      Executions.sendRedirect(null);
    

###Retrieve web app supported theme names###

String names = Library.getProperty("org.zkoss.theme.names");
Clone this wiki locally