views:

34

answers:

1

I'm developing an image rendering service that takes a bunch of query string parameters as arguments to render an image using Spring 3.0.3. I'm trying to validate the query string parameters and display errors if any exist.

Validation seems to be working just fine as the conditional statement on the BindingResult result parameter will display the error view when necessary. What I'm unsure of is as to why the tag does not render and error messages. I'm guessing I'm just missing some sort of crucial concept with regards to handling forms in Spring.

At any rate, here's the various pieces of code that I'm using to trying to achieve this with:

Controller:

@Controller
@RequestMapping( value = "/render" )
public class RenderController 
{
    @InitBinder
    public void initBinder( WebDataBinder binder )
    {
        binder.setValidator( new RenderJobValidator() );
    }

    @ModelAttribute
    public RenderJob newRenderJob( 
        @RequestParam( value = "icb", defaultValue = "" ) String icb, 
        @RequestParam( value = "icj", defaultValue = "" ) String icj,
        @RequestParam( value = "ics", defaultValue = "" ) String ics,
        @RequestParam( value = "icf", defaultValue = "" ) String icf,
        @RequestParam( value = "icc", defaultValue = "1" ) int icc,
        @RequestParam( value = "icd", defaultValue = "0" ) int icd )
    {
        RenderJob result = new RenderJob();
        result.setIcb( icb );
        result.setIcj( icj );
        result.setIcs( ics );
        result.setIcf( icf );
        result.setIcc( icc );
        result.setIcd( icd );
        return result;
    }

    @RequestMapping( method = RequestMethod.GET )
    public String handleJob( @Valid RenderJob renderJob, BindingResult result )
    {
        if( result.hasErrors() )
        {
            return "render/error";
        }
        return "render/debug";
    }
}

Validator:

public class RenderJobValidator implements Validator 
{
    @Override
    public boolean supports( Class<?> clazz ) 
    {
        return RenderJob.class.equals( clazz );
    }

    @Override
    public void validate( Object obj, Errors errors ) 
    {
        ValidationUtils.rejectIfEmptyOrWhitespace( errors, "icb", "Parameter 'icb' is required" );
        ValidationUtils.rejectIfEmptyOrWhitespace( errors, "icj", "Parameter 'icj' is required" );
        ValidationUtils.rejectIfEmptyOrWhitespace( errors, "ics", "Parameter 'ics' is required" );
        ValidationUtils.rejectIfEmptyOrWhitespace( errors, "icf", "Parameter 'icf' is required" );
    }
}

View (WEB-INF/views/render/error.jsp

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title>Render Error</title>
</head>
<body>
<h1>Render Error</h1>
<form:errors path="*"/>
</body>
</html>
+1  A: 

When <form:errors> is used inside a <form:form>, it displays the errors of the form's command object:

<form:form commandName = "renderJob">
    <form:errors path = "*" />
</form:form>

Otherwise you need a full path:

<form:errors path = "renderJob.*" />
axtavt
Thanks, that did the trick.
Matt W