I'm building an web application using Spring 3.0 MVC.
I have a method which has prototype below.
#RequestMapping(value = "/blahblah/blah.do", method=RequestMethod.GET)
public void searchData(#RequestParam(value="uniqOid", required=false) String uniqOid, #ModelAttribute("MasterVo") MasterVo searchVo,
ModelMap model, HttpServletResponse response, HttpServletRequest request)
The problem is that, the view (jsp) contains inputs that matches to searchVo(ModelAttribute).
When the int or long value of searchVo didn't come from the jsp, the server throws 404 page not found exception.
If the type of value is "String", it has no problem.
In my opinion, it is the problem of type casting.
How could I solve this problem, and which part of the server code that I have to check?
Thanks in advance.
I will go ahead and assume a few things about your problem.
It is not a type-cast problem. Spring has default converters that can easily convert from a String to some primitive type.
Now what you are facing is I think a null assigment to primitive type problem. Suppose the name of the property that's causing the problem is named primitiveProperty. Now, the request-paramters could include a parameter named primitiveProperty with an empty-String value, or some value that cannot be converted to a number. If the type of the primitiveProperty is String, it can assign the value of that parameter to it without any problem.
If the type of the primitiveProperty is int, long or some other primitive type that cannot have a null value, a problem occurs. When Spring converts the empty-string or a non-numeric string valued request-param named primitiveProperty, it cannot do so since that string can't be converted to a valid int or long value. So it is converted to null. Now, when Spring tries to assign that null value to a property that cannot have a null value (any primitve type), you get an Exception. If you are getting an empty-string as your request-param, you can replace the troublesome property in your domain object with its equivalent wrapper class (int with Integer, long with Long and so on). If you are getting a non-numeric value from your view, well, make sure that you don't get a non-numeric value.
You need to check the setter of the fields that are giving the typecast problem, in your case MasterVo .
The Spring will call the setter of the property to bind the value, where i presume you will see the error coming.
Just add a debug point to this setter and you will see the problem.
Related
I have a class :
class Con {
private List<Ind> inds;
}
I am using Gson in the usual way to convert a JSON string to this class object. so in case, the JSON doesn't have the key inds present this variable inds is assigned a null value. Is there a way to assign inds an empty ArrayList instead?
My Thoughts:
One straightforward way could be once the Gson object is built. Go over all the null objects and assign them to the new ArrayList<>(). Is there a better approach?
public List<Ind> getInds() {
return inds;
}
Currently I am using the above getter in a code like : con.getInds().stream() which is causing NullPointerException.
I am not sure what would be a good way to resolve this. Instead of List Should I return an Optional or Should I modify this getter like
public List<Ind> getInds() {
inds==null?new ArrayList<>():inds;
}
The above will also resolve the NullPointerException. Not sure if there are pros and cons to using this approach. Although now there is no way to identify if the Json has a key with name inds or not. For the current code that I am writing this may not be required. But there is a meaning loss here certainly.
One solution to this would be to assign default values to the fields, for example:
class Con {
private List<Ind> inds = new ArrayList<>();
}
Gson will keep this default value; only if the field is present in the JSON data it will reassign the field value.
There are however a few things to keep in mind:
Your class needs a no-args constructor (implicit or explicit); otherwise Gson might create instances without invoking the initializer blocks of the class, and therefore the field will be null
If the field is present in JSON but has a JSON null value, then Gson will still set that as value
You cannot tell afterwards whether the field was present in JSON but had an empty JSON array as value, or whether it was missing
Is it possible to give an object a default value using #RequestParam?
When I name the form tag the same as the field in the object
I know that it automatically assigns a value to an object.
But if the object's field is an int, null value is entered, an error occurs.
★ Plant_list2VO class
★ form
★ controller:
public String reg4(HttpServletRequest request, HttpServletResponse response,
Plant_list2VO plant_list2VO,
#RequestParam(name="inv_count", defaultValue="0") int inv_count,
#RequestParam(name="inv_count_disable", defaultValue="2") int inv_count_disable,
Model model) {
}
★ console:
WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'plant_list2VO' on field 'inv_count': rejected value [];
codes [typeMismatch.plant_list2VO.inv_count,typeMismatch.inv_count,typeMismatch.int,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [plant_list2VO.inv_count,inv_count]; arguments [];
default message [inv_count]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'inv_count'; nested exception is java.lang.NumberFormatException: For input string: ""]
Field error in object 'plant_list2VO' on field 'inv_count_disable': rejected value [];
codes [typeMismatch.plant_list2VO.inv_count_disable,typeMismatch.inv_count_disable,typeMismatch.int,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [plant_list2VO.inv_count_disable,inv_count_disable]; arguments []; default message [inv_count_disable]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'inv_count_disable'; nested exception is java.lang.NumberFormatException: For input string: ""]]
I see you define inv_count and inv_count_disable are int attributes. So, you should change values of defaultValue to a number to resolve java.lang.NumberFormatException
Judging by the console log, the inv_count and inv_count_disable values passed to your controller are not null, but empty Strings.
I can assume you use an old version of Spring, because before 3.2.x version empty String values were accepted by AbstractNamedValueMethodArgumentResolver as actual values (see method resolveArgument here), and defaultValue wasn't used in that case, resulting in conversion exception, because empty String cannot be converted to an integer.
In 3.2.x and later versions of Spring empty String is considered as there were no value passed, and defaultValue is used in case it's present (see method resolveArgument here).
So, if you want to stick to an older version of Spring, you may want to change your form to send some default number if the field is empty, or create a DTO, where these fields are of type String with some logic in setters.
Also this may help: Is it possible to have empty RequestParam values use the defaultValue?
Grails has a bug with regards to databinding in that it throws a cast exception when you're dealing with bad numerical input. JIRA: http://jira.grails.org/browse/GRAILS-6766
To fix this I've written the following code to manually handle the numerical input on the POGO class Foo located in src/groovy
void setPrice(String priceStr)
{
this.priceString = priceStr
// Remove $ and ,
priceStr = priceStr.trim().replaceAll(java.util.regex.Matcher.quoteReplacement('$'),'').replaceAll(',','')
if (!priceStr.isDouble()) {
errors.reject(
'trade.price.invalidformat',
[priceString] as Object[],
'Price:[{0}] is an invalid price.')
errors.rejectValue(
'price',
'trade.price.invalidformat')
} else {
this.price = priceStr.toDouble();
}
}
The following throws a null reference exception on the errors.reject() line.
foo.price = "asdf" // throws null reference on errors.reject()
foo.validate()
However, I can say:
foo.validate()
foo.price = "asdf" // no Null exception
foo.hasErrors() // false
foo.validate()
foo.hasErrors() // true
Where does errors come from when validate() is called?
Is there a way to add the errors property without calling validate() first?
I can't exactly tell you why, but you need to call getErrors() explicitly instead of accessing it as errors like a property. For some reason, Groovy isn't calling the method for it. So change the reject lines in setPrice() to
getErrors().reject(
'trade.price.invalidformat',
[priceString] as Object[],
'Price:[{0}] is an invalid price.')
getErrors().rejectValue(
'price',
'trade.price.invalidformat')
That is the easiest way to make sure the Errors object exists in your method. You can check out the code that adds the validation related methods to your domain class.
The AST transformation handling #Validateable augments the class with, among other things
a field named errors
public methods getErrors, setErrors, clearErrors and hasErrors
The getErrors method lazily sets the errors field if it hasn't yet been set. So it looks like what's happening is that accesses to errors within the same class are treated as field accesses rather than Java Bean property accesses, and bypassing the lazy initialization.
So the fix appears to be to use getErrors() instead of just errors.
The errors are add to your validateable classes (domain classes and classes that have the annotation #Validateable) dinamically.
Allowing the developer to set a String instead of a number doesn't seem a good way to go. Also, your validation will work only for that particular class.
I think that a better approach is to register a custom property editor for numbers. Here's a example with dates, that enable the transform of String (comming from the form) to Date with a format like dd/MM/yyyy. The idea is the same, as you will enforce that your number is parseable (eg. Integer.parseInt() will throw exception).
In your domain class, use the numeric type instead of String, so by code developers will not be allowed to store not number values.
I was following a tutorial today that had me scratching my head for an hour. Consider:
public class MyClass {
public int getTotal() {
amount = 100;
return amount;
}
}
and an excerpt from a JSP:
<p>Total: ${objectOfTypeMyClass.total}</p> //object instantiated elsewhere
Nowhere in the code was an instance variable named "total" ever declared or used. The only reference to the word "total" in the whole project (other than in the JSP) was the method getTotal().
So after some desperate last-ditch experimentation, it appears that Expression Language evaluates ${someObject.var} as "call the getVar() method of the someObject object.
I worked with this long tutorial for over a week thinking that ${someObject.var} was saying "directly fetch the saved instance variable "var" from someObject.
Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?
Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".
And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?
What gives?
Did I have it wrong the whole time and is my observation correct that in order to reference any instance variable using EL, you have to provide a corresponding getter method named getVarname() where "Varname" is the name of the instance variable?
That's correct. EL adheres the JavaBeans specification as described in the EL specification.
Also, EL seems to be case-insensitive in this regard. In my example above, "total" in ${objectOfTypeMyClass.total} is all lowercase where the method getTotal() has a capital "T".
No, it's certainly not case insensitive. It's specified behaviour. ${bean.Total} would not have worked.
And while we're at it, why don't we need to instantiate the variable "total"? I guess EL isn't actually referencing an instance variable...just a getter method?
It's because it's supposed to adhere the Javabean specification.
All with all, read the both specifications and everything will be clear :)
See also:
What are the advantages of Javabeans?
The . in objectOfTypeMyClass.total is the JSTL EL Dot Operator. It can do a few different things. Including:
map.key accessed a value from map stored under key. or
object.property accesses property from object using "JavaBeans" conventions.
This should work:
public class MyClass {
private int total = 100;
public int getTotal() {
return total;
}
...
}
I'm formatting a ResultSet to output to a CSV file. As such I really don't care about the Java types of the result set, beyond maybe knowing if it's text or numbers.
Does JDBC guarantee getString will always give a string representation of the values,atleast for single values (I don't need to concern myself about java.sql.Types.ARRAY,java.sql.Types.JAVA_OBJECT and a few others).
e.g. given resultSetMetaData.getColumnType(i) is a Types.FLOAT or a Types.BIGDECIMAL. will rs.GetString(i) always yield some String ?
i.e. Are there cases getString will throw an SQLException or return null when a getXXX would give me the value ?
Yup, check this : http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html
JDBC allows a lot of latitude as far as which getXXX methods you can use to retrieve the different SQL types. For example, the method getInt can be used to retrieve any of the numeric or character types. The data it retrieves will be converted to an int; that is, if the SQL type is VARCHAR , JDBC will attempt to parse an integer out of the VARCHAR. The method getInt is recommended for retrieving only SQL INTEGER types, however, and it cannot be used for the SQL types BINARY, VARBINARY, LONGVARBINARY, DATE , TIME, or TIMESTAMP.
But be careful, different JDBC driver may yield different result.
java.lang.String is a final class - it cannot, ever, have a subclass. So any method that returns String will either return an instance of the class java.lang.String, or a null, or throw an exception.
As for conversion, it is up to the JDBC driver if it will allow you to convert from non-String types. I suspect many will have an issue with it.
I would suggest that you do this instead:
Object item = resultSet.getObject(i);
String strValue = (item == null ? null : item.toString());
That should be more robust, since getObject() will always do the sensible thing.