NEXT PAGE
1. Eclipse
For JSP development you need the Eclipse WTP and an installed Tomcat.See Installation of Eclipse WTP and Tomcat.
A JSF library is required. We will later use Eclipse to download and install the Apache
MyFaces JSF
implementation during project creation.
Download the JSLT library from
https://jstl.dev.java.net/.
Your first JSF project
1.1. Create JSF Project
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf10.gif)
Press next until you see the following screen.
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf20.gif)
The first time you create a JSF project you need to install / download a JSF implementation. Press the "Download library..." button and select the Apache Library and install it.
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf30.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf34.gif)
Press Manage libraries and create a library for JSTL.
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf35.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf36.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf37.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf38.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf39.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf40.gif)
Click "Finish". Your project has been created
1.2. Review the generated project
Review theweb.xml
file. It has an entry for the Faces Servlet
and for the servlet mapping. Also the file "faces-config.xml" has
been
created.
Tip
To add the JSF settings to an existing dynamic web project, right-click on your project, select Project Properties -> Project Facets and add then JSF facet to your project.
Create a package "de.vogella.jsf.first.model" and the class
"TemperatureConvertor".
package de.vogella.jsf.first.model; public class TemperatureConvertor { private double celsius; private double fahrenheit; private boolean initial= true; public double getCelsius() { return celsius; } public void setCelsius(double celsius) { this.celsius = celsius; } public double getFahrenheit() { return fahrenheit; } public boolean getInitial(){ return initial; } public String reset (){ initial = true; fahrenheit =0; celsius = 0; return "reset"; } public String celsiusToFahrenheit(){ initial = false; fahrenheit = (celsius *9 / 5) +32; return "calculated"; } }
Double-click on faces-config.xml in the WEB-INF directory and
select the tab "ManagedBeans".
Press add and maintain your class.
The result should look like the following:
![](http://www.vogella.com/articles/JavaServerFaces/images/managedbeans10.gif)
Press add and maintain your class.
![](http://www.vogella.com/articles/JavaServerFaces/images/managedbeans20.gif)
![](http://www.vogella.com/articles/JavaServerFaces/images/managedbeans22.gif)
The result should look like the following:
![](http://www.vogella.com/articles/JavaServerFaces/images/managedbeans30.gif)
Select your project, right click on it, select New -> JSP.
Create the JSP page "Convertor.jsp". Use the "New JavaServer Faces
(JSF) Page (html)" template.
Change the coding to the following.
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf44.gif)
Change the coding to the following.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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=ISO-8859-1"> <title>Celsius to Fahrenheit Convertor</title> </head> <body> <f:view> <h:form> <h:panelGrid columns="2"> <h:outputLabel value="Celsius"></h:outputLabel> <h:inputText value="#{temperatureConvertor.celsius}"></h:inputText> </h:panelGrid> <h:commandButton action="#{temperatureConvertor.celsiusToFahrenheit}" value="Calculate"></h:commandButton> <h:commandButton action="#{temperatureConvertor.reset}" value="Reset"></h:commandButton> <h:messages layout="table"></h:messages> </h:form> <h:panelGroup rendered="#{temperatureConvertor.initial!=true}"> <h3> Result </h3> <h:outputLabel value="Fahrenheit "></h:outputLabel> <h:outputLabel value="#{temperatureConvertor.fahrenheit}"></h:outputLabel> </h:panelGroup> </f:view> </body> </html>
Tip
All JSF tag must be always be enclosed in a <f:view> tag.
Select Convertor.jsp, right
mouse-click- >run as -> run on
server.
Congratulations. You should be able to use your JSF application.
![](http://www.vogella.com/articles/JavaServerFaces/images/firstjsf50.gif)
Congratulations. You should be able to use your JSF application.
1.7. Layout via css
JFP application can get styles via css files. To load a style sheet include the following in your JSP page in the header section. This then related to a mystyle.css file under your folder WebContent/css..<LINK href="<%=request.getContextPath()%>/css/mystyle.css" rel="stylesheet" type="text/css">
Super blog sir. do u think i can learn
ReplyDelete