• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double Validation problem in Struts

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!
 
Harini Iyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic