It's not a secret anymore!*
The moose likes Struts and the fly likes Double Validation problem in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Double Validation problem in Struts" Watch "Double Validation problem in Struts" New topic
Author

Double Validation problem in Struts

Harini Iyer
Greenhorn

Joined: Jul 01, 2009
Posts: 4
Hi all..

I have problem in validating double field..am using struts 1.3 and am manually validating the fields in my action class and not using any framework.. Issue is, I have declared a field 'double' in my formbean.

And if i enter any invalid characeter say, a string in the field, it is not throwing error, instead, it takes it as 0.0. How to avoid this..

Thank you,
Meena
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Harini Iyer wrote:Hi all..

I have problem in validating double field..am using struts 1.3 and am manually validating the fields in my action class and not using any framework..

You can anytime start using Struts Validator, its not that much difficult.
For start: http://struts.apache.org/1.2.4/userGuide/dev_validator.html
Harini Iyer wrote:
Issue is, I have declared a field 'double' in my formbean.
And if i enter any invalid characeter say, a string in the field, it is not throwing error, instead, it takes it as 0.0.

You mean, at server side/action class, you're getting a value 0.0 for non numbers ? Have you checked your form bean, have you resetting somewhere property value in action form ?

Also a code snippet of JSP, FORM and action class will help.

And Welcome to JR !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Harini Iyer
Greenhorn

Joined: Jul 01, 2009
Posts: 4
Hi..

Thank you..

You can anytime start using Struts Validator, its not that much difficult.
For start: http://struts.apache.org/1.2.4/userGuide/dev_validator.html



yea, but it doesnt help much in our application.

You mean, at server side/action class, you're getting a value 0.0 for non numbers ? Have you checked your form bean, have you resetting somewhere property value in action form ?


yes, am getting 0.0 in my action class, for non numbers.

no am not resetting anywhere.

Also a code snippet of JSP, FORM and action class will help.


Login jsp:

Username

<font face="Trebuchet MS"><html:text value="" property="userName" size="30"/></font>


Password

<input type= "password" name= "password" >

Test

<input type= "text" name="dtest">

<input type="image" src="Images/login.bmp" onclick="login();">

function login()
{
document.forms[0].action="loginAuthentication.do";
document.forms[0].submit();
}


Formbean:

public class Formbean extends ActionForm {

private String userName, password;
//
private double dtest;

//private String dtest;


// public String getDtest() {
// return dtest;
// }
// public void setDtest(String dtest) {
// this.dtest = dtest;
// }
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public double getDtest() {
return dtest;
}
public void setDtest(double dtest) {
this.dtest = dtest;
}


}



}


Login Action:

public class LoginAction extends MappingDispatchAction {
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)


{

return mapping.findForward("login");
}


public ActionForward loginAuthentication(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)

{


Formbean formbean= (Formbean)form;
String username= formbean.getUserName();
String password= formbean.getPassword();
double var= formbean.getDtest();


//Boolean varstatus= var.isNaN();

System.out.println(username);
System.out.println(password);
System.out.println(var);
// System.out.println(varstatus);


if(username.equals("a")&& password.equals("a"))
{
System.out.println();
return mapping.findForward("Success");
}
else
{
return mapping.findForward("Failure");
}
}


}


The sysout above prints 0.0, when i enter a non numeric data.

Thanks,
Meena

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Pleas UseCodeTags.

The code looks OK and interestingly I'm not aware of this behavior, i.e converting to a default value rather than raising NuberFormatException in Struts 1.x.
Anyways, the quick solution will be to replace double with "String" property, and in getter, convert it into double
like


If, you want to keep it double, then I you have to use client side(JS) validation
Harini Iyer
Greenhorn

Joined: Jul 01, 2009
Posts: 4
Thank you..

Am not very much used to struts..

So how should my set method be?

The variables are double in my db too.. so, should i also have to convert and send???

Thanks a bundle,
Meena
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Harini Iyer wrote:So how should my set method be?

set method will be same, there will be no change in syntax.
Harini Iyer wrote:The variables are double in my db too.. so, should i also have to convert and send???

As getter method converting a value into double type for you, so you can simply use setDouble() method.
Harini Iyer
Greenhorn

Joined: Jul 01, 2009
Posts: 4
Hi..

It's throwing error, if i set it as double.

It says, cannot conver double to string.

private String dtest;
public double getDtest()
{
return Double.parseDouble(dtest);
}

public void setDtest(Double dtest)
{
this.dtest = dtest();

}

the above throws error..

so, i tried converting to string..

public void setDtest(Double dtest)
{
this.dtest = dtest.toString();

}

Thsi doesnt throw error, but whatever i enter, am getting a exception.

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
java.lang.Double.parseDouble(Unknown Source)
com.lnt.mdm.item.web.form.Formbean.getDtest(Formbean.java:47)
com.lnt.mdm.item.action.LoginAction.loginAuthentication(LoginAction.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
org.apache.struts.actions.MappingDispatchAction.execute(MappingDispatchAction.java:166)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

One more time, Pleas UseCodeTags.
Harini Iyer wrote:
It says, cannot conver double to string.


Your setter is wrong, take the parameter as a string.



Harini Iyer wrote:so, i tried converting to string..

javax.servlet.ServletException: java.lang.NullPointerException

Check what is getting null ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double Validation problem in Struts