未呈现验证错误我的 Spring 3.0.3 Web MVC 应用程序中的标签
我正在开发一个图像渲染服务,它采用一堆查询字符串参数作为参数来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
在
中使用时,它会显示表单命令对象的错误:否则您需要完整路径:
When
<form:errors>
is used inside a<form:form>
, it displays the errors of the form's command object:Otherwise you need a full path: