package net.viralpatel.jsf.helloworld;
public class UserBean {
private int id;
private String name;
//Action method to add user
public String addUser() {
return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code language: Java (java)
Above Java class is a User bean that we will use to store our user’s information. This class acts like a form bean and action class. The addUser() method will get called when we click Add button on our Add User page. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>Add New User Form</title>
</head>
<body>
<f:view>
<p>
<h:message id="errors" for="User_ID" style="color:red"/>
</p>
<h:form>
<h:panelGrid border="1" columns="2">
<h:outputText value="ID"></h:outputText>
<h:inputText id="User_ID" value="#{userBean.id}" required="true">
<f:validateLongRange minimum="1" maximum="500"/>
</h:inputText>
<h:outputText value="Name"></h:outputText>
<h:inputText value="#{userBean.name}"></h:inputText>
<h:commandButton action="#{userBean.addUser}"
value="Add Customer"></h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
Code language: HTML, XML (xml)
We have added a validation rule for ID using f:validateLongRange tag and required=”true” attribute. The ID must be in between 1 and 500. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>List of Users</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="User #{userBean.name} is added successfully.">
</h:outputText>
</h:form>
</f:view>
</body>
</html>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>
net.viralpatel.jsf.helloworld.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>AddUser</display-name>
<from-view-id>/AddUser.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/ListUser.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
Code language: HTML, XML (xml)
In faces config we have defined a managed bean UserBean with scope session and mapping from AddUser.jsp to ListUser.jsp. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Very simple and ease to follow.
Many thanks
Thanks for the comment Jide. :)
The last screen shot of the browser still has AddUser.jsp as the URL instead of ListUser.jsp. This creates confusion.
Hi Navier,
JSF uses internal FORWARD instead of REDIRECT to navigate to the success page. Hence the URL will not get changed.
Well, it uses forward "by default". Just add a "redirect" tag to your navigation handler in faces-config.xml and there you are .... No more confusion (but you loose requestMap parameters, in case you use them or FacesMessages).
Eduard
Very good example Viral.. helped me to understand jsf..
Thanks for the comment Nishant. You may want to subscribe your email for the Articles.
This helped me for executing my first JSF program. Thanks for your work.
hiiii viral great example .. i understood well bt wanna know more abt jsf..
could u jst forward me one simple application using jsf ..
Thanks
Hi Viral.. Great example for the beginners.
Thanks,
Rajan