Credits
Yann Arthur Nicolas
yannart@gmail.com
www.merlinsource.com
Objective
Create a first application with JSF using tags for JSP and a ManagedBean, understand the
basic configuration of the facesconfig.xml file.
Prerequisites
Basic Java, basic HTML, an IDE that supports JSF and a container JSP / Servlets.
The steps are indicated for NetBeans 5 and higher with Tomcat 5.
Note: As the tutorial was done in Linux environment, the diagonals are "/" for Windows normally the
bar is inverted "" but the NetBeans environment allows under Windows the format
Unix is used.
What are we going to do?
There will be a main page (index.jsp) that will have a link to another (input.jsp) with a form
to put our name.
By pressing the button of the form, it will be validated if the field is not empty and if the number of characters
is in a certain range (from 2 to 15). If the entry is correct, a welcome message is displayed with the
name entered, if there is an error, the page with the form is reloaded and a message is displayed
specifying the type of error.
Project
Open NetBeans and create a new project:
File> New Project Categories> Web> Web Application
The New Web Application window appears We are going to leave almost everything as it is by default:
• Project Name: Hello
• Project Location: leave as default or change to change to a
private directory (example: / home / yannart / projects)
• Project Folder: it fills only
• Source Structure : Java BluePrints
• Server: Bundled Tomcat (for those who want to use Sun Application Server or
JBoss, it does not matter)
Java EE Version: J2EE 1 .4 (those using the sun server can use 1.5)
•
• Context Path: leave as is. (/ Hi)
• We put the two pigeons in the checkboxes.
Click the Next button.
• Put the pigeon in the JavaServer Faces checkbox
• Leave the Struts checkbox blank
• Servlet URL Mapping: we leave as it comes (/ faces / * )
• We put the pigeon in the checkbox Validate XML
• We leave the checkbox in Verify Objects
Press the Finish button. At last! =)
JSP
We have to create some additional files to those that were generated when creating the project:
entrada.jsp
•
• hola.jsp
For this we do right click in the folder "Web Pages" of your project and then "NEW>
JSP …"
• Only change the field JSP File Name: We put the pigeon in the JavaServer checkbox
Facesentry
[19659002] Press Finish
Repeat the same steps to create hello.jsp
We can delete the file welcomeJSF.jsp since it will not be used: right click on the file
welcomeJSF.jsp of the Web Pages folder and then Delete.
Now you are going to replace the contents of each jsp file.
index.jsp
< %@page contentType="text/html"% >
< %@page pageEncoding="UTF8"% >
<! DOCTYPE HTML PUBLIC "// W3C // DTD HTML 4.01 Transitional / / EN "
" http://bit.ly/kTyqzh ">
< html >
< head >
< meta httpequiv="ContentType" content="text/html; charset=UTF8" >
< title > JSF tutorial < /title >
< /head >
< body >
< h1 > Welcome to the JSF tutorial < /h1 >
< br/ >
< a href="./faces/entrada.jsp" > Give your name < /a >
< /body >
< /html >
entry.jsp
< % Importa los tags html para jsf% >
< %@ taglib uri="http://java.sun.com/jsf/html" prefix="h" % >
< % Importa los tags de control para jsf% >
< %@ taglib uri="http://java.sun.com/jsf/core" prefix="f" % >
< % Utiliza el siguiente archivo de recursos% >
< f:loadBundle basename="hola.recursos.mensajes" var="msj"/ >
< html >
< head >
< title > Give name
< /head >
< body >
< f:view >
< h1 >
< % Pintamos texto% >
< h:outputText value="#{msj.cabezal}"/ > [19659000] < /h1 >
< /head >
< body >
< f:view >
< /p >
< % Formulario% >
< h:form id="helloForm" >
< % Pintamos texto% >
< h:outputText value="#{msj.dar_nombre}"/ > ]
<% Mandatory text field whose value is sent to attribute name
of Bean personBean%>
< h:inputText value="#{personaBean.nombre}" required="true" >
< % la entrada debe de tener entre 2 y 15 caracteres % >
< f:validateLength minimum="2" maximum="15"/ >
< /h:inputText >
< % boton que ejecuta la accion% >
< h:commandButton action="saluda" value="#{msj.boton}" / >
< /h:form > [19659077]
< /body >
< /html >
hola.jsp
< % Importa los tags html para jsf% >
< %@ taglib uri="http://java.sun.com/jsf/html" prefix="h" % >
< % Importa los tags de control para jsf% >
< %@ taglib uri="http://java.sun.com/jsf/core" prefix="f" % >
< % Utiliza el siguiente archivo de recursos% >
< f:loadBundle basename="hola.recursos.mensajes" var="msj"/ >
[1 9459007]
< head >
< title > Welcome < /title >
< /head >
< body >
< f:view >
< h1 >
< h:outputText value="#{msj.signo1}" / >
< h:outputText value="#{msj.signo1}" / >
< h:outputText value="#{msj.saludo1}" / >
< % se recupera el valor nombre del Bean personaBean% >
< h:outputText value="#{personaBean.nombre}" / >
< h:outputText value="#{msj.saludo2}" / >
< h:outputText value="#{msj.signo2}" / >
< /h1 >
< /f:view > ]
< /body >
< /html >
JavaBean
File> New File …
Categories> JavaBeans Object
File Types> JavaBeans Component
Next Button
• Class Name: PersonaBean
• Package: hello
Finish button
We replaced the contents of the ParsonaBean.java file:
package hello;
import java.beans. *;
import java.io.Serializable;
/ **
* @author yannart
* /
public class PersonaBean {
private String name;
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
.properties
We create the file that will contain the messages used in the JSP:
File> New File
Categories> Other
File Tipes> Properties File [19659002] Next button
We change the following fields:
• File Name: messages
• Folder: src / java / hi / resources
Press Finish
We changed the contents of this file by: [19659002] #list of messages used in the JSP
spindle = JSF Tutorial
dar_name = Please enter your name:
salutation1 = Welcome
salutation2 = a JSF
button = Greetings
sign1 = ¡
signo2 =!
facesconfig.xml
Now we just need to replace the contents of the facesconfig.xml file in the Configuration folder
faces:
< ?xml version="1.0"? >
<! DOCTYPE facesconfig PUBLIC
"// Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1 // EN"
"http://bit.ly/2GID8E0">
< facesconfig >
< navigationrule >
< fromviewid > /entrada.jsp< /fromviewid >
< navigationcase >
< fromoutcome > greets < /fromoutcome >
< toviewid > /hola.jsp< /toviewid >
] < /navigationcase >
< /navigationrule >
< managedbean >
< managedbeanname > personBean < /managedbeanname >
< managedbeanclass > hello.PersonaBean < /managedbeanclass >
< managedbeanscope > request < /managedbeanscope >
< /managedbean >
< /facesconfig >
Execution of the project
If we did everything right, we should execute the project without problem:
Run> Run Main Proyect (or press F6)
Some screenshots of the window to browser:
from Nettech Post http://bit.ly/2RtHqmV
No hay comentarios:
Publicar un comentario