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

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

Adding additional PrimeFaces themes

In this section, we are going to turn to the power of Maven to add additional themes. We can do this because the PrimeFaces team has made them available as dependencies. The only difference here is that they haven't made them available on Maven Central Repository. Instead, they are available on PrimeFaces Maven repository.

Note

Did you know that anyone can set up a Maven repository for themselves and provide libraries to the public? Doing this means not having to wait to get your work added to Maven Central Repository.

In order to get Maven to point to the PrimeFaces repository, we need to add an entry in the project's pom.xml file.

Open the pom.xml file, which will be found in the project's Project Files folder. Under the name tag (highlighted in the following code), add everything in the repositories tag and save the file:

 <name>PFThemes</name>
  <repositories>
    <repository>
      <id>prime-repo</id>
      <name>PrimeFaces Maven Repository</name>
      <url>http://repository.primefaces.org</url>
      <layout>default</layout>
    </repository>
  </repositories>

Now, after doing this, we can add additional themes as dependencies to our project. In the pom.xml file, scroll down until you find the dependencies tag. We will now add a dependency for the bootstrap theme.

By adding the following snippet of XML to the pom project, we add all the openly available PrimeFaces themes to the project:

    <dependency>
      <groupId>org.primefaces.themes</groupId>
      <artifactId>all-themes</artifactId>
      <version>1.0.10</version>
    </dependency>

By building the project, we cause Maven to download the theme.jar file and add it to the project's runtime.

What happens when we run the project? Try it and see what happens. Navigate to the difference.xhtml page that we created earlier. Is anything different now that we have added the bootstrap theme?

The theme being used is still the default theme—aristo. So, we obviously need to tell the project to use the new one. Fortunately, PrimeFaces makes this very easy for us.

We need to open the web.xml file and add the following XML code:

  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bootstrap</param-value>
  </context-param>

Now, save the changes and rerun, build, and run the project.

Tip

NetBeans can deploy your project when you save files in it. In the project properties sheet, select the Run option and check off the Deploy on save checkbox.

Refresh the difference.xhtml page. See the changes? We can display the value of the theme setting in our page. You need to add the following highlighted code to difference.xhtml before the </h:panelGrid> tag page:

  <h:outputText id="currentThemeLabel" value="Current theme"/>
  <h:outputText id="currentThemeValue" value= "#{initParam['primefaces.THEME']}"/>
</h:panelGrid>

Refresh the difference.xhtml page in your browser and you should see the current theme bootstrap.

We can not only set the theme for our project, but also change it while the project is running. Furthermore, we can allow users to choose which theme they wish to use without affecting the choices that other users make.

主站蜘蛛池模板: 青神县| 达拉特旗| 琼中| 和龙市| 呼图壁县| 清水县| 华阴市| 鹤山市| 肇东市| 城市| 格尔木市| 沙雅县| 昭苏县| 亳州市| 澳门| 株洲市| 南陵县| 隆化县| 蓝山县| 寿阳县| 昌宁县| 庆安县| 宜州市| 镇赉县| 辰溪县| 峨眉山市| 申扎县| 甘南县| 乳山市| 尉犁县| 商河县| 万盛区| 麟游县| 靖州| 黑龙江省| 安乡县| 麻栗坡县| 于都县| 嘉鱼县| 潢川县| 句容市|