1

我应该下载什么来使用 spring.know 中的验证在我的课程中是未知的,例如在打击代码中:

public String register2( @Valid User user , BindingResult br)
    {
        if(br.hasErrors())
        {
            return "edit";
        }
        //System.out.println("you registers!");
        return "thanks";
    }

@valid 未知。我应该下载哪个库以在 spring mcv 中使用 jsr-303 标准?我应该在哪里下载?以及我如何在 eclipse 中设置它helious?谢谢

编辑:我的代码附加=>

我的控制器=>

package codes;

import java.util.Map;

import javax.validation.Valid;
import javax.validation.Validator;

import org.apache.jasper.tagplugins.jstl.core.Out;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.BindingResultUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;


@org.springframework.stereotype.Controller

public class Controller {


    @RequestMapping(value="/register/" , method=RequestMethod.GET)
    public String register(Model model)
    {
        model.addAttribute("myUser",new User());
        return "edit";
    }
    @RequestMapping(value="/register/" , method=RequestMethod.POST)
    public String register2( ModelAndView model,@Valid User myUser , BindingResult br)
    {

        try
        {
        if(br.hasErrors())
        {

            return "edit";
        }
        else
        {
            System.out.println(myUser);
            System.out.println(myUser.getName());
            System.out.println(myUser.getFamily());
            System.out.println("salam");
            return "thanks";
        }
        }
        catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
        return "thanks";
    }

}

我的 edit.jsp 页面(表单)=>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
</head>
<body>
<div>

<sf:form method="post" modelAttribute="myUser" >


<label for="USER_NAME">name:</label>
<sf:input path="name" id="USER_NAME"/>
<sf:errors path="name" ></sf:errors>
<br>
<label for="USER_FAMILY">family:</label>
<sf:input path="family" id="USER_FAMILY"/>
<br>
<input type="submit" value="REGISTER" />



</sf:form>




</div>



</body>
</html>

注意:只有当我的用户对象无效时,我才会得到异常,而当它有效时,我不会给出例外

4

1 回答 1

2

您可以使用休眠验证器

要运行它,您需要将这些 jar 添加到您的项目中:

  • 休眠验证器*.jar
  • 验证-api*.jar
  • slf4j-api*.jar

您可以在Hibernate Validator 包中找到所有这些。

于 2011-08-12T06:13:08.450 回答