未呈现验证错误我的 Spring 3.0.3 Web MVC 应用程序中的标签

发布于 2024-09-08 23:34:46 字数 2720 浏览 3 评论 0原文

我正在开发一个图像渲染服务,它采用一堆查询字符串参数作为参数来使用 Spring 3.0.3 渲染图像。我正在尝试验证查询字符串参数并显示错误(如果存在)。

验证似乎工作得很好,因为 BindingResult 结果参数上的条件语句将在必要时显示错误视图。我不确定的是为什么标签不呈现和错误消息。我猜我只是错过了一些关于在 Spring 中处理表单的关键概念。

无论如何,这是我用来尝试实现此目的的各种代码:

控制器:

@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";
    }
}

验证器:

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" );
    }
}

视图(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">
<html>
<head>
<title>Render Error</title>
</head>
<body>
<h1>Render Error</h1>
<form:errors path="*"/>
</body>
</html>

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">
<html>
<head>
<title>Render Error</title>
</head>
<body>
<h1>Render Error</h1>
<form:errors path="*"/>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-09-15 23:34:46

中使用时,它会显示表单命令对象的错误:

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

否则您需要完整路径:

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

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.*" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文