Java Server Faces

Transcription

Java Server Faces
Java Server Faces (JSF)
Martin Klíma
JSF Framework

Helps to build web applications in Java

MVC

Custom tag library for front-end

Event handlers

Validators
Architecture
Source: http://www.tutorialspoint.com/jsf/jsf_architecture.htm
JSF livecycle
May have been stored before
Custom validators apply here
Converters take place hare
Simple application – backing bean

Let’s have a simle data object – a bean with firstName, lastName
@ManagedBean
@RequestScoped
public class PersonManagedBean {
private String lastName;
private String firstName;
public PersonManagedBean() {
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getWholeName() {
return this.getFirstName() + ", "+this.getLastName();
}
}
View

firstPage.xhtml
visible as /faces/firstPage.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<f:view>
Expressions using
<h:form>
backing bean
Name: #{personManagedBean.firstName} <br/>
Surname: #{personManagedBean.lastName}
<br/>
First name:
Input for first name
<h:inputText value="#{personManagedBean.firstName}"
id="firstName" label="First name"/>
<br/>
Input for last name
Last name:
name
<h:inputText value="#{personManagedBean.lastName}"
id="lastName"
label="Last name">
error message
</h:inputText>
placeholder
<h:message for="lastName" style="color:red" />
Expression using
<br/>
backing bean
Whole name: #{personManagedBean.wholeName}
<br/>
Submit button
<h:commandButton value="Submit"/>
</h:form>
</f:view>
</h:body>
</html>
Calling application logic
<h:commandButton
value="Submit"
action="#{personManagedBean.submit}"
/>
call submit() method of
backing bean
public String submit() {
// do something with the values
// return name of page to go to
internal forward,
return "index";
will call index.xhtml
}
Validation

Existing internal validators
Validator Class
BeanValidator
Tag
validateBean
DoubleRangeValidator
validateDoubleRange
LengthValidator
validateLength
LongRangeValidator
validateLongRange
RegexValidator
validateRegEx
RequiredValidator
validateRequired
Function
Registers a bean validator for the
component.
Checks whether the local value
of a component is within a certain
range. The value must be
floating-point or convertible to
floating-point.
Checks whether the length of a
component’s local value is within
a certain range. The value must
be a java.lang.String.
Checks whether the local value
of a component is within a certain
range. The value must be any
numeric type or String that can
be converted to a long.
Checks whether the local value
of a component is a match
against a regular expression from
the java.util.regex package.
Ensures that the local value is
not empty on
anjavax.faces.component.Editabl
eValueHolder component.
Example - validation
…
…
Last name:
<h:inputText value="#{personManagedBean.lastName}"
id="lastName"
label="Last name">
<f:validateLength minimum="3" maximum="8" />
</h:inputText>
<h:message for="lastName" style="color:red" />
…
…

More validators can be assigned to one field
Example result
How to change this
message?
Custom message
1.
Add your message into the .xhtml
…
…
Last name:
<h:inputText value="#{personManagedBean.lastName}"
id="lastName"
label="Last name"
validatorMessage="Wath the size of last name! It should be 3 to 8.">
<f:validateLength minimum="3" maximum="8" />
</h:inputText>
<h:message for="lastName" style="color:red" />
Custom message
<br/>
…
…
Custom messages

Introduce your own property file
file: cz/cvut/fel/wa2/wa2messages.properties
javax.faces.validator.LengthValidator.MINIMUM=Field must be of size of at least ''{0}''
characters.

register this file in faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/webfacesconfig_2_2.xsd">
<application>
<message-bundle>
cz.cvut.fel.wa2.wa2messages
</message-bundle>
</application>
</faces-config>
Custom validators
FacesValidator("wa2Validator")
public class Wa2Validator implements Validator {
@Override
public void validate(FacesContext fc, UIComponent uic, Object o) throws
ValidatorException {
// log("Validating submitted email -- " + value.toString());
String content = o.toString();
if (!content.toLowerCase().startsWith("wa2")) {
FacesMessage msg
= new FacesMessage("WA2 validation failed",
"The value must start with string WA2");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
Apply the validator in a form
First name:
<h:inputText value="#{personManagedBean.firstName}"
id="firstName" label="First name">
<f:validator validatorId="wa2Validator"/>
</h:inputText>
<h:message for="firstName" style="color:red" />

Similar documents

JavaServer Faces

JavaServer Faces • JSF maintains the state of components over several requests.

More information