在Spring MVC(使用休眠验证器)中提交具有无效数据的表单时,发送语法上不正确的请求 [英] Syntactically incorrect request sent upon submitting form with invalid data in Spring MVC (which uses hibernate Validator)

查看:206
本文介绍了在Spring MVC(使用休眠验证器)中提交具有无效数据的表单时,发送语法上不正确的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录表格:

<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

前几天有人问我,我觉得这值得 提及:在您的Spring MVC @Controller类处理程序方法中, 确保BindingResult参数紧随模型之后,或者 命令参数,例如:@RequestMapping(...)public String handleRequest(@ModelAttribute @Valid YourCustomPojo尝试, BindingResult结果).在此示例中,handleRequest将验证 POJO(YourCustomPojo)-检查POJO是否有JSR303注释 并尝试应用约束,因为对POJO进行了注释 使用@Valid-并在BindingResult中存储任何错误, 如果我们要求的话可以使用.

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值)

这篇关于在Spring MVC(使用休眠验证器)中提交具有无效数据的表单时,发送语法上不正确的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆