Hi,
I am new to JSF. Infact i am not a programmer. But, recently i started
using this application using Exadel IDE.
I am facing a problem with reset button. I have used converters and
validators in my application. I have two inputfields where user needs to
enter the values in numbers only. If not it gives error message. After
giving this error message, if i click on reset button it's not removing
the values from the page.
I just used managed beans to create this application. Before validation
if i click reset it is resetting the form to clear the fields.
Please have a look at the following code where I am getting problem with
the Reset button.
Cardnum.java
package jsftraining;
public class Cardnum {
private String number;
public Cardnum (String number){
this.number = number;
}
public String toString (){
return this.number;
}
}
CardnumConverter.java
package jsftraining;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
public class CardnumConverter implements Converter {
public Object getAsObject(FacesContext context,
UIComponent component,
String newValue) {
StringBuffer buffer = new StringBuffer(newValue);
boolean foundInvalidChar = false;
int i = 0;
while (i < buffer.length()) {
char ch = buffer.charAt(i);
if (Character.isDigit(ch) ||
Character.isWhitespace(ch) ||
ch == '-'){
i++;
continue;
}
else { // found invalid char
foundInvalidChar = true;
break;
}
}
if (foundInvalidChar) {
ResourceBundle bundle =
ResourceBundle.getBundle("jsftraining.messages_en",
context.getViewRoot().getLocale());
String key = "invalidCardnumChar";
String msgSummary = bundle.getString(key);
String msgDetail = bundle.getString(key+"_detail");
FacesMessage message = new FacesMessage (msgSummary,msgDetail);
throw new ConverterException (message);
}
return new Cardnum( buffer.toString() );
}
public String getAsString(FacesContext context,
UIComponent component,
Object value) {
return value.toString();
}
}
CardnumValidator.java
package jsftraining;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class CardnumValidator implements Validator {
public void validate(FacesContext context,
UIComponent component,
Object value)
throws ValidatorException {
System.out.println ( "------- INSIDE Custom Validator");
String cardnumNumber = "";
if ( value instanceof Cardnum)
cardnumNumber = value.toString();
else
cardnumNumber = value.toString(); // if not Telephone object
if (cardnumNumber.length() == 0) return;
Pattern creditCardPattern =
Pattern.compile("[345]\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d")
;
Matcher creditCardMatcher = creditCardPattern.matcher(cardnumNumber);
if (!creditCardMatcher.matches()) {
ResourceBundle bundle =
ResourceBundle.getBundle("jsftraining.messages_en",
context.getViewRoot().getLocale());
String key = "invalidCardNumber";
String msgSummary = bundle.getString(key);
String msgDetail = bundle.getString(key+"_detail");
FacesMessage message = new FacesMessage (msgSummary,msgDetail);
throw new ValidatorException (message);
}
}
}
Credit.java
package jsftraining;
import jsftraining.CreditInput;
public class Credit {
public CreditInput input = new CreditInput();
public CreditInput getInput() {
return input;
}
public void setInput(CreditInput input) {
this.input = input;
}
public String paid(){
return "paid";
}
public void clearCreditInput() {
input = new CreditInput();
}
}
CreditInput.java
package jsftraining;
public class CreditInput {
private java.lang.Float amount;
private Cardnum cardnum;
public java.lang.Float getAmount() {
return amount;
}
public void setAmount(java.lang.Float amount) {
this.amount = amount;
}
public Cardnum getCardnum() {
return cardnum;
}
public void setCardnum(Cardnum cardnum) {
this.cardnum = cardnum;
}
}
messages_en.properties:
detailsPageTitle=Credit Card Payment
amountDetails=Amount\:
buttonLabel=Process
numberPrompt=Credit Card Number\:
resultPageTitle=Thank you for the payment
invalidCardnumChar=Invalid Card number entered.
invalidCardnumChar_detail=Please enter numeric values only.
invalidCardNumber=Invalid card number.
invalidCardNumber_detail=Please enter a valid card number that starts
with 345 and exactly has 16 digits. Please follow the following format
xxxx-xxxx-xxxx-xxxx
CardInfo.jsp
<%@ taglib uri="
http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="
http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="jsftraining.messages_en" var="msg" />
<html>
<head>
<title>JSF Application</title>
</head>
<body>
<f:view>
<h:form>
<h2><h:outputText value="#{msg.detailsPageTitle}"/></h2>
<table>
<tr>
<td>
<h:outputText value="#{msg.numberPrompt}"/>
</td>
<td>
<h:inputText id="num" value="#{billForm.input.cardnum}"
required="true">
<f:validator validatorId="jsftraining.Cardnum"/>
</h:inputText>
<h:message for="num"/>
<h:outputText value="#{billForm.input.cardnum}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{msg.amountDetails}"/>
</td>
<td>
<h:inputText id="amt" value="#{billForm.input.amount}" />
<h:message for="amt"/>
</td>
</tr>
<tr>
<td colspan="2">
<h:commandButton value="#{msg.buttonLabel}"
action="#{billForm.paid}"/>
<h:commandButton value="clear"
action="#{billForm.clearCreditInput}"/>
</td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>
paiddetails.jsp
<%@ taglib uri="
http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="
http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="jsftraining.messages_en" var="msg" />
<html>
<head>
<title>JSF Application</title>
</head>
<body>
<f:view>
<h:form>
<h2><h:outputText value="#{msg.resultPageTitle}"/></h2>
<table>
<tr>
<td>
<h:outputText value="#{msg.numberPrompt}"/>
</td>
<td>
<h:outputText id="num" value="#{billForm.cardnum}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{msg.amountDetails}"/>
</td>
<td>
<h:outputText id="amt" value="#{billForm.amount}"/>
</td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 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>
<converter>
<converter-for-class>jsftraining.Cardnum</converter-for-class>
<converter-class>jsftraining.CardnumConverter</converter-class>
</converter>
<managed-bean>
<managed-bean-name>billForm</managed-bean-name>
<managed-bean-class>jsftraining.Credit</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/pages/cardinfo.jsp</from-view-id>
<navigation-case>
<from-outcome>paid</from-outcome>
<to-view-id>/pages/paiddetails.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<validator>
<validator-id>jsftraining.Cardnum</validator-id>
<validator-class>jsftraining.CardnumValidator</validator-class>
</validator>
<lifecycle/>
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
</application>
<factory/>
</faces-config>
index.jsp
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head></head>
<body>
<jsp:forward page="/pages/cardinfo.jsf" />
</body>
</html>
Thanks,
Krishna Perumahanti
Usability Engineer
Tel: +040-666 71195
Mobile: +91-9989700019
Krishna.Perumahanti_at_ca.com
visit: www.ca.com
<
http://www.ca.com/>