Table of contents
1.
Introduction
2.
JSF h:outputStylesheet Tag
2.1.
JSF Tag
2.2.
Rendered Output
2.3.
Example Algorithm
2.4.
styles.css
2.5.
pom.xml
2.6.
home.xhtml
2.7.
Output
3.
Frequently Asked Questions
3.1.
What is the use of JSF?
3.2.
Is JSF superior to JSP?
3.3.
What exactly is the JSF tag library?
3.4.
In Outputstylesheet, how do you use H?
3.5.
In JSF, how many levels of style sheets are there?
4.
Conclusion
Last Updated: Mar 27, 2024

JSF h:outputStylesheet

Author Ishita mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

By offering a component-centric method for creating Java Web user interfaces, JSF streamlines development. A wide range of Java/Web developers are drawn to JavaServer Faces. While "systems developers" will find that the extensive and robust JSF API offers them unparalleled power and programming flexibility, "corporate developers" and Web designers will find that JSF development may be as simple as dragging and dropping user interface (UI) components into a page.

In this article, we will study h:outputStyleSheet/> and how using the standard link tag, you can incorporate a style sheet in your code. Let's take a closer look at this concept with the help of an example.

JSF h:outputStylesheet Tag

The JSF framework offers a collection of common HTML tags. Each tag will produce the appropriate HTML output. The output (h) an HTML element of type "link" with the type "text/css" is rendered by the stylesheet tag. The external stylesheet file can be added to the JSF page using this tag.

JSF Tag

<h:outputStylesheet library = "css" name = "styles.css" /> 

Rendered Output

<link type = "text/css" rel = "stylesheet"  
   href = "/Coding Ninjas/javax.faces.resource/styles.css.jsf?ln = css" /> 

Example Algorithm

To test the aforementioned tag, let's build a test JSF application.

Step

Description

1

Make a project called Coding Ninjas under the com.codingninjas.test package.

2

Add a resources folder to the main src folder.

3

Add a CSS folder to the resources folder under the src folder.

4

Make a styles.css file and place it in the src/main/resources/css folder.

5

Adjust the styles.css file as described below.

6

Adjust pom.xml as described below.

7

Make the following changes to home.xhtml. Leave the remaining files alone.

8

Compile and run the programme to ensure that the business logic is operating in accordance with the specifications.

9

Create the programme as a war file, then deploy it to the Apache Tomcat Webserver.

10

As stated in the final step below, launch your web application using the proper URL.

 

styles.css

.messege{
Color: blue;
}

pom.xml

<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.codingninjas.test</groupId>
   <artifactId>Coding Ninjas</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>Coding Ninjas Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>com.sun.faces</groupId>
         <artifactId>jsf-api</artifactId>
         <version>2.1.7</version>
      </dependency>
      <dependency>
         <groupId>com.sun.faces</groupId>
         <artifactId>jsf-impl</artifactId>
         <version>2.1.7</version>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
      </dependency>
   </dependencies>
   <build>
      <finalName>helloworld</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
               <source>1.6</source>
               <target>1.6</target>
            </configuration>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
               <execution>
                  <id>copy-resources</id>
                  <phase>validate</phase>
                  <goals>
                     <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>${basedir}/target/helloworld/resources
                        </outputDirectory>
                     <resources>
                        <resource>
                           <directory>src/main/resources</directory>
                           <filtering>true</filtering>
                        </resource>
                     </resources>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>

home.xhtml


<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//CN//DTD XHTML 1.0 Transitional//EN"
"http://www.encoding.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.CodingNinjas/2022/xhtml"
   xmlns:f = "http://java.sun.com/jsf/core"
   xmlns:h = "http://java.sun.com/jsf/html">
   <h:head>
      <title>Blog on JSF</title>
      <h:outputStylesheet library = "css" name = "styles.css"  />
   </h:head>
   <h:body>
      <h2>h:outputStylesheet example</h2>
      <hr />
      <h:form>
         <div class = "message">Coding Ninjas</div>
      </h:form>
   </h:body>
</html>

Output

Frequently Asked Questions

What is the use of JSF?

The JavaServer Faces (JSF) framework is a new Java standard for developing Web applications. It simplifies development by allowing developers to create Java Web user interfaces using a component-centric architecture.

Is JSF superior to JSP?

JSF is a framework that connects a data source to a reusable UI component, supports various libraries, and reduces the time and effort required to develop and manage applications. JSF provides a significant security advantage over JSP because it is component-based.

What exactly is the JSF tag library?

A standard HTML tag library is included with JSF. These tags are rendered into Html output that corresponds to them. It would help if you used the following URI namespaces in the Html node for these tags.

In Outputstylesheet, how do you use H?

h:output is a variable that represents the output of a program. The stylesheet tag creates an HTML element with the type "text/css" and the type "link." This tag is used to incorporate an external stylesheet into a JSF page.

In JSF, how many levels of style sheets are there?

In JSF, there are three types of style sheets, namely- external, internal, and inline.

Conclusion

In this article, we have extensively learned how to render HTML link elements with the type “text/css” using h:outputStylesheet tag. We hope this blog has helped you broaden your knowledge regarding h:outputStylesheet in JSF.

Do enroll in our courses and refer to the mock test and problems available. Also, have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding, ninjas!

Live masterclass