JSF Validation In Login Application

This section gives you the best approach for the login application. You have learnt about the login application without validating the application. But in this section you will learn how to validate a form in JSF using it's tags. JSF tags give you the validation facilities for validate the JSF form on server. Validation of the form restricts you for the wrong entries.

This section also provides you the complete source code of the application that validate your login application. You can download the zip file and copy it inside the tomcat/webapps folder and run it by type the following url in your browser.

URL: http://localhost:8080/LoginValidation/login.jsf.

Output of the login application with the complete validation is shown as follows:

Login ID: chandan
Password: chand

Login Form:

This is the simple login form in which you have to enter the login id and the password. This form is validated. If you do not enter login id and password and click on the Login button then the message will be opened like:

This image is showing you the validation error message for required value. If you enter the login id only and click on the Login button to submit the form then the application generate a validation error message for the required value of the password field like:

This application will generate the error message for the particular JSF form component until you enter the login id and the password value something else.

Code of the login.jsp file that makes the login form:

<%@ page contentType="text/html"%>

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

<f:view>

</span><span style="color:#000000;">JSF Simple Login Example</span><span style="color:#0000ff;">


<h:form>













<h:outputText value="Enter
Login ID: "
/>
<h:inputText id="loginname"
value="#{SimpleLogin.loginname}" required="true" />color="#FF0000">
<
h:message for="loginname"/>
<h:outputText value="Enter Password:
"
/>
<h:inputSecret id="password" value="
#{SimpleLogin.password}"
required="true" />color="#FF0000"><h:message for="
password"
/>
<h:commandButton value="Login" action
="#{SimpleLogin.CheckValidUser}" />

h:form>


f:view>

Here is the code for the resultforfail.jsp file:

Login Failed!

Here is the code for the resultforsuccess.jsp file:

Login Successful!

Here is the code for the Backing Bean SimpleLogin.java file:

package roseindia;


public class SimpleLogin{
String loginname;
String password;

public SimpleLogin(){}

public String getLoginname(){
return loginname;
}

public void setLoginname(String loginname){
this.loginname = loginname;
}

public String getPassword(){
return password;
}

public void setPassword(String password){
this.password = password;
}

public String CheckValidUser(){
if(loginname.equals("chandan") && password.equals("chand")){
System.out.println("chandan");
return "success";
}
else{
return "fail";
}
}
}

faces-config.xml file code:



version="1.0"?>
faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>SimpleLoginmanaged-bean-name>
<managed-bean-class>roseindia.SimpleLoginmanaged-bean-class>
<managed-bean-scope>requestmanaged-bean-scope>
managed-bean>
<navigation-rule>
<from-view-id>/login.jspfrom-view-id>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}from-action>
<from-outcome>successfrom-outcome>
<to-view-id>resultforsuccess.jspto-view-id>
navigation-case>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}from-action>
<from-outcome>failfrom-outcome>
<to-view-id>resultforfail.jspto-view-id>
navigation-case>
navigation-rule>
faces-config>

web.xml file code:

version="1.0"?> 

web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2
.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHODparam-name>
<param-value>serverparam-value>
context-param>


<servlet>
<servlet-name>Faces Servletservlet-name>
<servlet-class>javax.faces.webapp.FacesServletservlet-class>
<load-on-startup> 1 load-on-startup>
servlet>


<servlet-mapping>
<servlet-name>Faces Servletservlet-name>
<url-pattern>*.jsfurl-pattern>
servlet-mapping>
web-app>

Download This Complete Application.

0 comments:

Post a Comment