1

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

matina.Kapur
  • 43
  • 1
  • 5

1 Answers1

2

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.

craftsman
  • 15,133
  • 17
  • 70
  • 86
  • i add all of them but i give: java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder; – matina.Kapur Aug 12 '11 at 13:59
  • try adding slf4j-log4j*.jar and log4j*.jar as well. – craftsman Aug 12 '11 at 14:33
  • now when i run in tomcat i give:log4j:WARN No appenders could be found for logger (org.apache.commons.digester.Digester.sax). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. and when i run in jboss i give:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute – matina.Kapur Aug 12 '11 at 14:43
  • Although its not a good practice, but you can ignore log4j warning for now. To avoid the later error, you need to prepend @ModalAttribute annotation before your User parameter declaration. – craftsman Aug 12 '11 at 19:51
  • i editet my question and added my controller and jsp.i realy be greatfull if you have a look at them – matina.Kapur Aug 12 '11 at 21:27
  • I think you may need to either specify `@ModelAttribute("myUser") @Valid User myUser` in your controller method or specify `modelAttribute="user"` in your JSP tag (I think Spring will name the model attribute "user" based on its class, "User", unless you specify a different name for the model attribute). – sdouglass Aug 15 '11 at 17:12