Developing JSF2 Validations Application

ABC.PROPERTIES

Sid= student id

Sname-student name

Saddr-student address

Semail-student email

Smobile-student mobile

Button=add student

Javax.faces. validator.  Regexvalidator. Not.matched- detail

Value must be provided in “{o}” format

Sid-validation-msg=student id must be started with dss

 

Index.html

<html>

<head>

<meta http-equal=”Refresh” context=”2; url=http://local host:1010/ Jsf2validations app/student add form.dss”>

<head>

<body>

<h1>Application is loading </h1>

</body>

</html>

 

Student add form.jsp

<%@laglib prefix=”f”url=

<%@                            ”h”

<html>

<f:view>

<h1>soft pvt ltd</h1>

<h2>student add form</h2>

<h:form>

<h:panel Grid columns=”3” >

<h:output text value=”#{msg.sid}”/>

<h: input text id=”sid” value =”#. { student bean.sid}”

Required=”true” required message=student IN is required”>

<f:validator validator In=”sid validator”/>

</h: input text>

<h:message for=”sid”/>

<h:output text value=”#{msg. sname}”/>

<h: input text id=”s name” value =”#. { student bean. name }”

Required=”true” required message=student name is required”>

</h:input text>

<h:message for=”sname”/>

<h:output text value=”#{msg. saddr}”/>

<h: input text id=”s addr” value =”#. { student bean. naddr }”

Required=”true” required message=”student address is required”>

<h: message for=”saddr”/>

<h:output text value=”#{msg. semail}”/>

<h: input text id=”s email” value =”#. { student bean. semail }”

Required=”true” required message=”student email is required”>

<h: message for=”semail”/>

<h:output text value=”#{msg. s mobile}”/>

<h: input text id=”s mobile” value =”#. { student bean. S mobile }”

Required=”true” required message=”student mobile required”>

<f:validate Ragex pattern=”/d{2}-/d{10}$”/>

</h:input text>

<h: message for=”smobile”/>

<h:command button value=”#{msg. button}”action=#{student bean. add}”/>

</h:panel Grid>

</h:form>

</f:view>

</body>

</html>

 

Success.jsp

<%@laglib prefix=”f”prefix=”h”

<html>

<body>

<f:view><h1>student Registration success</h1></fiview>

</body>

</html>

 

Studentbean.java

Package com. soft;

Public class student bean{

Private string sid;

Private string sname;

Private string saddres;

Private string semail;

Private string smobile;

Private string saddres;

//provide setter()&getter()

Public string add(){

Return”success”;

}

}

 

SIDVALIDATOR.JAVA

Package com.durgasoft;

Import java.util.x;

Import java. faces. application. applicaiton;

Import java. faces. application. facesnessage;

Import java. faces. Component.uicomponent;

Import java. faces. Context.faces context;

Import java. faces. Validator, validator;

Import java. faces. Validator, validator exception;

Public class SID validator implements validator{

Public Void validate(faces context tc,UI Component ue,object val)

Throws validate exception{

String sid=(string)val;

If(!sid.strtswith(DSS-“)){

Faces message fm=new faces message();

Application application=fc.get application();

String prop-path=application. Get message bundle();

Resource bundle rb=resource bundle. get bundle(prop-path);

Strubg err-msg=rb.get string(“sid-validation-msg”);

Fm.set detail.(err-msg);

Throw new validate exception (fm);

}

}

 

Faces-config.xml

<faces-config…..

-----xsd>

<managed-bean>

<managed-bean-name>student bean</managed-bean-name>

<managed-bean-scope>session</>

<managed-bean>

<navigation rule >

<from-view-id>/student add from.jsp</  >

<navigation case >

<from-outcome>success</    >

<to-view-id>/success.jsp</    >

</navigation case >

</navigation rule>

<validator>

<validator-id>sid validator</   >

<validator-class>com. soft.SID Validator</   >

</validator>

<application>

<resource-bundle>

<base-name>abc</  >

<var>msg</  >

</resource-bundle>

<message- bundle >abc</message- bundle >

</application >

</faces-config >

Web.xml

 

 

 

Step2 -  Configure user- defined converter class in faces – config.xml file

<faces- config>

---------

<Converter>

<converter -id>converter logical name</  >

<converter-class>fully qualified converter class</  >

</converter>

-----

</faces-config>

  

Step-3

Use. User- defined converter in both i/p and o/p UI Component

To use converter in i/p and o/p UI Components wl have to use the following tag

<f:converter conver ID=”--”/>

Where “converter Id” attribute will take logical name of the converter class which we provided in

[for example appl n on JSF data table, GUI Components & converters refer hand out given o n16 th Nov 2013]