I am using Spring MVC to build my web application, and I have a question about validating parameters I receive in the URL. What is the best way to detect invalid parameters and display errors to the user?
Suppose I have a "View User Profile" page. The profile that is displayed is based on a user ID parameter specified in the URL. I might go to the following address to view the profile of the user with ID 92:
I have created a ProfileControlller object which will do the following:
- Get the
idparameter from therequestobject - Load a
UserProfileobject from my database - Add the
UserProfileobject to the model - Construct a
ModelAndViewwith my model and the "View User Profile" JSP
That works fine if the user enters a valid ID number. My UserProfile object is loaded and displayed perfectly using JSP. But what if someone passes user ID -30294 to my page? My data access layer will simply return a null object if the ID is invalid, but I would like to show a friendly error message to the user. Is checking for a null UserProfile object in the JSP code really the best solution here?
Since I am new to Spring, I'm not sure. I like the way Validator classes can be used with FormController objects, if that is any help.