As usual, when learning a new technology, the first application to write is a "Hello World" style application. To continue this tradition, I've written a very simple Hello World JSF application. After setting up the Faces servlet as described in my previous post, I've written a simple JSP page called hello.jsp which has the following contents:
<%@page contentType="text/html"%> 
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<f:view>
<h:outputText value="Hello World" />
</f:view>
</body>
</html>
This simple JSP page specifies that the 2 JSF taglibs "core" and "html" are to be used and specifies their appropriate prefixes. The HTML is pretty standard up until the <f:view> tag. This tag tells the container we are going to start using JSF components. Inside this tag, the outputText component is used to print out the text "Hello World" to the browser.
This trivial application is started by pointing the browser to http://localhost:8080/LearningJSF/hello.jsf
Goofy
2/8/2011 06:11:36 pm

One question: where’s the best place to put the tag? First line of the page? Before the tag? Before the tag? Or any of them is just OK? I don’t like that the spec is not specific in this regard. Sure it doesn’t matter, but at least a recommendation could be worthy…

Reply
Goofy
2/8/2011 06:11:57 pm

Oops… my tags are eaten up:

One question: where’s the best place to put the f/view tag? First line of the page? Before the html tag? Before the body tag? Or any of them is just OK? I don’t like that the spec is not specific in this regard. Sure it doesn’t matter, but at least a recommendation could be worthy…

Reply
David
2/8/2011 06:12:24 pm

Thanks for the comment. I don’t know the correct answer here. I tend to put the f/view tag as close as possible around the necessary JSF code – which is always within the body tag. Whether this is best practice I don’t know.

If anyone knows what the best practice here is, please let us know by leaving a comment.

Reply



Leave a Reply.