gpt4 book ai didi

java - 获取 spring :bind status. errorMessage 填充时出现问题

转载 作者:行者123 更新时间:2023-12-02 00:15:03 24 4
gpt4 key购买 nike

我有一个使用 Spring 框架的 Groovy 项目及其 validator 对我的表单输入值进行健全性检查。我想让 Spring 通过内置的 ${status.errorMessage} 在我的输入表单字段旁边填充错误消息;但是,我只能让它填充我的模型对象中的“errorMessages”(来自 Controller )。所以让我们看一些代码。

登录.jsp:

<form method="post" action="<c:url value="/login" />">
<spring:bind path="request.username">
<label for="username"><fmt:message key="login.username"/>:
<input type="text" id="username" size="20" maxlength="50" name="username" value="${request.username}"/>
</label>
<%-- This part does NOT display the validation errors. --%>
<c:if test="${status.error}"><span class="error">${status.errorMessage}</span></c:if>
</spring:bind>

<spring:bind path="request.password">
<label for="password"><fmt:message key="login.password"/>:
<input type="password" id="password" size="20" maxlength="30" name="password" />
</label>
<%-- This part does NOT display the validation errors. --%>
<c:if test="${status.error}"><span class="error">${status.errorMessage}</span></c:if>
</spring:bind>

<input id="login" type="submit" value="Login"/>
</form>

<%-- This part does display the validation errors. --%>
<c:if test="${ec > 0}">
<p>
<c:forEach items="${errorCodes}" var="error">
<span class="error"><fmt:message key="${error.defaultMessage}"/></span><br/>
</c:forEach>
</p>
</c:if>

LoginController.groovy:

@RequestMapping(method = RequestMethod.GET, value = '/')
ModelAndView defaultView() {
ModelMap model = new ModelMap()
model.addAttribute('request', new LoginRequest())
new ModelAndView('login', model)
}

@RequestMapping(method = RequestMethod.POST, value = '/login')
ModelAndView login(
LoginRequest loginRequest, HttpServletResponse response,
HttpSession session, BindingResult br, ModelMap model
) {
validator.validate(loginRequest, br)

if (br.hasErrors()) {
model.addAttribute('request', loginRequest)
return returnWithError(br, model, 'login')
}
...
}

private ModelAndView returnWithError(BindingResult br, ModelMap model, String redirectTo) {
br.allErrors.each {error ->
log.error(error.toString())
}

def objectErrors = br.allErrors.findAll {e -> e instanceof ObjectError}

model.addAttribute('ec', br.errorCount)
model.addAttribute('errorCodes', objectErrors)
new ModelAndView(redirectTo, model)
}

LoginRequestValidator.groovy:

@Override
void validate(Object o, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, 'username', 'username.empty', 'username.empty')
ValidationUtils.rejectIfEmpty(errors, 'password', 'password.empty', 'password.empty')
}

我缺少 Spring Magic [TM] 的哪一部分?

最佳答案

我认为您的 BindingResult 对象应该是紧跟在 LoginRequest 对象后面的参数。请参阅http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestmapping特别是示例 15.1。 BindingResult 和 @ModelAttribute 的顺序无效

关于java - 获取 spring :bind status. errorMessage 填充时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com