Hey PJ,
I'm just becoming familiar with Spring myself, but here is what I've learned from my first app...
There are a couple of things you can do. First, you can define an ExceptionResolver in your servlet context file:
Code:
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
</props>
</property>
</bean>
Then, depending on how you set up your ViewResolver, you just set the URL of the dataAccessFailure view. If you're using ResourceBundleViewResolver, your views.properties file would contain something like the following:
Code:
dataAccessFailure.class=org.springframework.web.servlet.view.JstlView
dataAccessFailure.url=/WEB-INF/jsp/dataAccessFailure.jsp
Using this method, you can "catch" any RuntimeException and "throw" it to a specific view, jsp page in this case.
As a second point, if the "baddata" you are refering to is the failure to pass a validator, then the same view will be redisplayed. If you've bound in any error messages, they would then appear. Here is an example using jsp. It is a simple form field expecting an IP address:
Code:
<B>IP Address:</B>
<spring:bind path="command.ip">
<FONT color="red">
<B><c:out value="${status.errorMessage}"/></B>
</FONT>
<INPUT type="text" maxlength="30" size="30" name="ip" value="<c:out value="${status.value}"/>" >
</spring:bind>
<P>
If on validation, the controller throws a validation error, this jsp will display that error message.
The other thing I've seen is to have your controller handle specific data by returning a Redirection view...
Code:
public ModelAndView computerHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException {
Computer computer = getApplicationFacade()
.loadComputer(RequestUtils.getIntParameter(request, "computerId", 0));
if (computer == null) {
return new ModelAndView("findComputersRedirect");
}
Map model = new HashMap();
model.put("computer", computer);
return new ModelAndView("computerView", "model", model);
}
In this case, the controller looks for a QueryString called computerId, and uses it to look up a computer in the database. If it doesn't return a computer, the controller then redirects it to the view called "findComputersRedirect".
Like I said, I'm by no means an expert at Spring yet, but I hope this helps you a bit.
Take care,
James