Oh yes, and when the validation is ok, you could just make a little helper method in your form so that your Action does not have to handle the conversion it self;
private String cost;
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public double getCostAsDouble() {
return Double.valueOf(cost);
}
Main Topics
Browse All Topics





by: JathrekPosted on 2007-05-24 at 22:37:51ID: 19154859
You'd need to change the type of your "cost" property in your form to a String, otherwise Struts will already try to convert it to a double and it may skip some errors (like if the first characters of what the user entered are digits but the end of the string is not, it may keep only the digits as value).
Then, you can implement the "validate" method of Struts' ActionForm class and make your checks in there (it will be called after Struts set up the user-entered value in the form but before the form is used in your Action).
There, you can check for the value of the String you have for "cost" and, if it is not correct, you can add an ActionError (I think it's the right name, not sure as it is quite old for me ^^) to a list of them (which is returned by the validate method).
When Struts receives a non empty list of ActionErrors after validating a form, it will go back to the "input" page you defined for the Action that is based on this form. But you need to have this "input" property set for the action as well as the "validate" property set to "true"
An other way would be to use the Struts' validator, but the only time I'd have needed it, it was not "powerful" enough (like I needed to validate multiple number fields as one, ...) so I just developped my own little framework based on those validate methods.