PDA

View Full Version : How to get Errors from XSLT view?


msqr
08-18-2004, 07:43 PM
How do I gain access to the Errors instance created during form processing from an XSLT view implementation?

davison
08-19-2004, 02:37 AM
In your views.properties, or equivalent XML file, define a requestContextAttribute as you would for any other view type..

myXsltView.class=org.foo.MyXsltView
myXsltView.requestContextAttribute=rc
You can also add this attribute to your view resolver definition, in which case all views resolved by it will have the value set.

This causes a RequestContext object to be placed in the model with the name specified (rc).

In your AbstractXsltView subclass (MyXsltView) you implement the createDomNode() method which is passed the model as one of its parameters. Get the errors from here via the RequestContext and domify it however you want..

protected Node createDomNode(Map model, String rootName, HttpServletRequest req, HttpServletResponse res) throws Exception {

RequestContext rc = (RequestContext) model.get("rc");
// 'command' is the name of your bind object
Errors errors = rc.getErrors("command");

// domify your errors however you like..

}

msqr
08-19-2004, 11:30 AM
Excellent, thank you!

msqr
08-19-2004, 07:11 PM
You can also add this attribute to your view resolver definition, in which case all views resolved by it will have the value set.


I'd like to do this with the ResourceBundleViewResolver, but as far as I can tell only the UrlBasedViewResolver has this requestContextAttribute field. I could of course extend the ResourceBundleViewResolver to add this field myself, but I was wondering if perhaps the ResourceBundleViewResolver would benefit from this field? Or am I missing something completely here?

derek
04-26-2005, 03:36 AM
I'm also puzzled why the View doesn't have access to the RequestContext by default.

Is there a reason?

D.

captrespect
10-14-2005, 11:06 AM
I got all this working but it doesn't lookup my custom error messages from messages.properties. How do I go about doing that?

captrespect
10-14-2005, 12:50 PM
You have to use the BindStatus to get a custom error message.

so... to loop through all the binding errors on the page.


Errors errors = rc.getErrors("command");
List list = errors.getAllErrors();
Iterator i = list.iterator();
while (i.hasNext()) {
FieldError error = (FieldError)i.next();

String field = error.getField();

String objectName = error.getObjectName();

BindStatus bind = rc.getBindStatus(objectName + "." + field);

String message = bind.getErrorMessage();

logger.debug(message);
}