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

前端之家收集整理的这篇文章主要介绍了java – 在Spring MVC中使用无效数据提交表单时发送的语法错误请求(使用hibernate Validator)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

登录表格:

loginData类:

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;
    }
}

显示和提交表单的控制器方法:(显示包含注册表单和登录表单的主页)

@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,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)无论何时绑定Result包含错误,都会调用方法(使用loginData和BindingResult值)

猜你在找的Spring相关文章