官术网_书友最值得收藏!

  • PrimeFaces Theme Development
  • Andy Bailey Sudheer Jonna
  • 428字
  • 2021-07-09 21:12:23

The PrimeFaces ThemeSwitcher component

PrimeFaces provides a component called ThemeSwitcher, which allows us to change themes dynamically without having to refresh the page that we are on. In order to get this to work, we need to create some classes known as JavaBeans, which provide things that the ThemeSwitcher needs in order to work its magic.

We are going to add some new packages to our project and then create our Beans. Perform the following steps to add new packages to our project:

  1. Right-click on the project in the Project view and select New….
  2. You may have to scroll down a bit or even select the Other… option, but we are looking for the Java option. This displays a list of things that we can create, and we want a package.
  3. We add a package with the name com.andyba.pfthemes.session keeping the other values as they are.
  4. Then, add another package named com.andyba.pfthemes.themes.

Now, in order to create our first Bean, perform the following steps:

  1. Select New… by right-clicking on the themes package menu (open the Sources tree from the project) and look for JSF Managed Bean. The following dialog box opens:
    The PrimeFaces ThemeSwitcher component
  2. Change the Class Name field to ThemesService. When you do this, the Name field changes automatically. You can leave this field as it is.
  3. Change the Scope field to Application and then click on Finish.

The source code file for the ThemesService Bean is then opened by NetBeans, and it should look like this:

package com.andyba.pfthemes.themes;

import java.util.logging.Logger;
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;

/**
 *
 * @author Andy_2
 */
@ManagedBean@ApplicationScoped
public class ThemesService {

  private static final Logger logger = Logger.getLogger(ThemesService.class.getName());

  /**
  * Creates a new instance of ThemesService
  */
  public ThemesService() {

}

Now, for those of you who are familiar with JSF Contexts and Dependency Injection (CDI), the annotations won't be a mystery. For those of you who are not, I will briefly explain the following:

  • @ManagedBean: This tells the application server that instances of this Bean can be referred to by the name as set in the value attribute of the annotation. This means that we can use the name in JSF pages to magically access instances of our Bean.
  • @ApplicationScoped: This annotation tells the application server that only one instance of the ThemesService Bean needs to be created and that it shall remain available for the entire time that the application is running. We only need one because we will use the Bean to provide a list of theme choices to the next Bean that we create, the CurrentTheme Bean.
主站蜘蛛池模板: 荃湾区| 霍州市| 漳平市| 阳曲县| 故城县| 巴南区| 五原县| 紫阳县| 蒙山县| 宁都县| 黑河市| 农安县| 五原县| 普兰店市| 宁晋县| 临清市| 开江县| 新营市| 大同市| 林西县| 霍邱县| 洛阳市| 连城县| 武清区| 宁南县| 武义县| 郓城县| 阿图什市| 陆河县| 满城县| 灵宝市| 紫云| 哈巴河县| 白朗县| 山阴县| 平阴县| 米易县| 合川市| 山阳县| 苏尼特右旗| 扎赉特旗|