Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top