问题描述
登录表格:
<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>
loginData类:
the loginData class:
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 methods for showing and submitting the form: (Shows homepage which contains signup form and login form)
@RequestMapping(value = "/", method=RequestMethod.GET)
public String showHome(Model model)
{
loginservice.logout();
model.addAttribute("logindata", new loginData());
model.addAttribute("signupdata", new signupData());
return "home";
}
提交登录表单时调用的方法:
Method called upon submitting login form:
@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 login works fine when the data entered is 'valid' (either correct or wrong). However, when it is invalid, for instance, when the password field is empty or the username is less than four characters long, following error is shown:
The request sent by the client was syntactically incorrect.
您知道如何解决此问题吗?
Any idea how this might be fixed?
推荐答案
您必须修改参数的顺序.始终将BindingResult result
参数始终放在带有@Valid
批注的参数之后.
You have to modify the order of your arguments. Put the BindingResult result
parameter always directly after the parameter with the @Valid
annotation.
@RequestMapping(value = "login", method=RequestMethod.POST)
public String submitloginForm(@Valid loginData logindata, BindingResult result,
SessionStatus state, Model model)
这周甚至在本周春季-2013年3月5日博客条目中提到了这一点
This was even mentioned in this weeks This Week in Spring - March 5th, 2013 blog entry
春天会
- 0)确定处理程序方法
- 1)创建loginData的实例
- 2)填充
- 3)对其进行验证,并将验证结果存储在BindingResult中
- 4)不管绑定结果是否包含错误,都调用方法(具有loginData和BindingResult值)
这篇关于在Spring MVC(使用休眠验证器)中提交具有无效数据的表单时,发送语法上不正确的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!