gpt4 book ai didi

java - Spring MVC 不显示表单错误

转载 作者:行者123 更新时间:2023-12-02 14:23:21 25 4
gpt4 key购买 nike

这是我的 Controller

@RequestMapping(value = "/save",method = RequestMethod.POST)
public ModelAndView saveUserAccount(@ModelAttribute("account") UserAccountForm userAccount, BindingResult result){
AddUserValidator userValidator = new AddUserValidator();
userValidator.validate(userAccount,result);
boolean hasErrors = result.hasErrors();
if(hasErrors){
return render(ADD_USER_VIEW)
.addAttr("ACCOUNT_ROLES", Arrays.asList(AccountRole.values()))
.addAttr("TENANTS",tenantInfoService.getAll())
.addAttr("errors",result)
.addAttr("account",userAccount).toMav();
}
return render(ADD_USER_VIEW)
.addAttr("ACCOUNT_ROLES", Arrays.asList(AccountRole.values()))
.addAttr("TENANTS",tenantInfoService.getAll())
.addAttr("account",new UserAccountForm())
.toMav();
}

这是我创建的渲染库。

public class RenderMavBuilder {

private final ModelAndView mav;

public static RenderMavBuilder render(String viewname){
RenderMavBuilder target = new RenderMavBuilder(viewname);
return target;
}

public RenderMavBuilder addAttr(String attrName, Object value){
mav.addObject(attrName, value);
return this;
}

public RenderMavBuilder addAttr(Object value){
mav.addObject(value);
return this;
}

public RenderMavBuilder addAttrs(Map<String , ?> attrs){
mav.addAllObjects(attrs);
return this;
}

private RenderMavBuilder(String viewName){
this.mav = new ModelAndView(viewName);
}

public ModelAndView toMav(){
return mav;
}
}

这是我的 validator

这是我的表格。

        <div class="col-md-6 centered">
<form:errors path="*" />
<form:form commandName="account" method="post" action="${pageContext.request.contextPath}/user/save">
<!-- Username -->
<div class="col-xs-12 form-group">
<label class="control-label">Username</label>
<form:input path="username" type="text" class="form-control" />
</div>
<!-- Password -->
<div class="col-xs-12 form-group">
<label class="control-label">Password</label>
<form:password path="password" class="form-control"/>
</div>


<!-- Password -->
<div class="col-xs-12 form-group">
<label class="control-label">Password</label>
<form:password path="retypedPassword" class="form-control"/>
</div>

<!-- First Name -->
<div class="col-xs-12 form-group">
<label class="control-label">First Name</label>
<form:input path="firstName" type="text" class="form-control"/>
</div>

<!-- First Name -->
<div class="col-xs-12 form-group">
<label class="control-label">Last Name</label>
<form:input path="lastName" type="text" class="form-control"/>
</div>

<!-- User Role -->
<div class="col-xs-12 form-group">
<label class="control-label">User Role</label>
<form:select path="accountRole" class="form-control">
<form:options items="${ACCOUNT_ROLES}"/>
</form:select>
</div>

<!-- Branch Designation -->
<div class="col-xs-12 form-group">
<label class="control-label">Designated Branch</label>
<select path="tenantId" items="${TENANTS}" class="form-control">
<c:forEach var="branch" items="${TENANTS}">
<option value="${branch.id}">${branch.tenantDescription}</option>
</c:forEach>
</select>
</div>
<!-- Button -->
<div class="col-md-12 form-group">
<button class="form-control btn btn-primary submit-button" type="submit">Save New User <i class="fa fa-check-square-o"></i></button>
</div>
</form:form>

在我的 Controller 上,绑定(bind)结果有错误。但是,错误没有显示在 View 上,我错过了什么?

最佳答案

问题在于您构建 ModelAndView 的方式,您应该使用 Errors 对象来构建它。使用 Errors.getModel() 获取表示模型的底层 Map,并在 View 名称旁边使用它来构造 ModelAndView

在其他模型属性旁边,您只需添加 @ModelAttribute 带注释的方法即可检索这些属性。我还建议添加一个基于 GET 的方法,您可以在成功提交后重定向到该方法。

您的帮助程序类似乎也毫无意义,因为仅在 Controller 内执行它会简化您的代码(恕我直言)。

你的 Controller 可能看起来像这样。

@RequestMapping(value = "/save",method = RequestMethod.POST)
public ModelAndView saveUserAccount(@ModelAttribute("account") UserAccountForm userAccount, BindingResult result){
AddUserValidator userValidator = new AddUserValidator();
userValidator.validate(userAccount,result);
boolean hasErrors = result.hasErrors();
return new ModelAndView(hasErrors ? ADD_USER_VIEW, "redirect:/add", errors.getModel());
}

@RequestMapping(value="/add")
public String addUserAccount(Model model) {
model.addObject("account", new UserAccountForm());
return ADD_USER_VIEW;
}

@ModelAttribute
public void referenceData(Model model) {
model.addObject("ACCOUNT_ROLES", Arrays.asList(AccountRole.values()));
model.addObject("TENANTS",tenantInfoService.getAll());
}

关于java - Spring MVC 不显示表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726869/

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