开发者

what should and where should i download for work with jsr-303 in spring?

what should i download for working with validation in spring.know annotations are unknown in my classes for example in blow code:

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

@valid is unknown .which library should i download for work with jsr-303 standard in spring mcv?and where should i download? and how i setup that in eclipse helious? thanks

EDIT:MY CODE APPENDED=>

my controller=>

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

}

my edit.jsp page(form)=>

<%@ 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>

NOTE:only when my user object is invalide i get exception and when thatz valid i give not exeption


You can use Hibernate Validator.

To run it, you need to add these jars to your project:

  • hibernate-validator*.jar
  • validation-api*.jar
  • slf4j-api*.jar

You can find all of them in the Hibernate Validator package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜