gpt4 book ai didi

java - 在 Spring MVC(使用 hibernate Validator)中提交带有无效数据的表单时发送语法错误的请求

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:03 25 4
gpt4 key购买 nike

登录表单:

<f:form class="form-horizontal" method="post" action="/login"
commandName="logindata">
<fieldset>
<legend class="text-info">Login</legend>
<div class="control-group">
<f:label path="uname" class="control-label" for="uname">Username</f:label>
<div class="controls">
<f:input type="text" path="uname" name="uname" id="uname"
placeholder="Username" />
</div>
</div>
<div class="control-group">
<f:label path="pwd" class="control-label" for="pwd">Password</f:label>
<div class="controls">
<f:input type="password" path="pwd" name="pwd" id="pwd"
placeholder="Password" />
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn" id="login">
Login <i class="icon-chevron-right"></i>
</button>
</div>
</div>
<div id="errormsg" class="alert alert-error">${message}</div>
</fieldset>
</f:form>

登录数据类:

package com.demo.forms;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class loginData {
@Length(min=4)
private String uname;

@NotEmpty
private String pwd;

public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}

用于显示和提交表单的 Controller 方法:(显示包含注册表单和登录表单的主页)

@RequestMapping(value = "/", method=RequestMethod.GET)
public String showHome(Model model)
{
loginservice.logout();
model.addAttribute("logindata", new loginData());
model.addAttribute("signupdata", new signupData());
return "home";
}

提交登录表单时调用的方法:

@RequestMapping(value = "login", method=RequestMethod.POST)
public String submitloginForm(@Valid loginData logindata, SessionStatus state, Model model, BindingResult result)
{
if((loginservice.loggedin()) || (result.hasErrors()))
{
return showHome(model);
}
else
{
String uname = logindata.getUname();
String pwd = logindata.getPwd();
if(loginservice.login(uname, pwd))
{
model.addAttribute("user",uname);
return "redirect:profile";
}
else
{
model.addAttribute("message","Invalid Username/Password");
return showHome(model);
}
}
}


当输入的数据“有效”(正确或错误)时,登录工作正常。但是,当它无效时,例如,当密码字段为空或用户名长度少于四个字符时,会显示以下错误:

The request sent by the client was syntactically incorrect.

知道如何解决这个问题吗?

最佳答案

您必须修改参数的顺序。始终将 BindingResult 结果 参数直接放在带有 @Valid 注释的参数之后。

@RequestMapping(value = "login", method=RequestMethod.POST)
public String submitloginForm(@Valid loginData logindata, BindingResult result,
SessionStatus state, Model model)

这甚至在这几周被提及 This Week in Spring - March 5th, 2013博客条目

Someone asked me this the other day and I felt like it was worthy of a mention: in your Spring MVC @Controller class handler methods, make sure that the BindingResult argument is immediately after the model or command argument, like this: @RequestMapping(...) public String handleRequest( @ModelAttribute @Valid YourCustomPojo attempt, BindingResult result). In this example, handleRequest will validate the POJO (YourCustomPojo) - checking the POJO for JSR303-annotations and attempting to apply the constraints because the POJO is annotated with @Valid - and stash any errors in the BindingResult, which it makes available if we ask for it.

春将

  • 0) 确定处理程序方法
  • 1) 创建一个 loginData 实例
  • 2) 填充它
  • 3) 对其进行验证,并将验证结果存储在BindingResult中
  • 4) 调用方法(使用 loginData 和 BindingResult 值),无论绑定(bind)结果是否包含错误

关于java - 在 Spring MVC(使用 hibernate Validator)中提交带有无效数据的表单时发送语法错误的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336143/

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