0

Hibernate Validator 有大问题

收到此错误

java.lang.IllegalStateException:Bean 名称“公司”的 BindingResult 和普通目标对象都不能用作请求属性

这是我的控制器

 import java.util.logging.Logger;

 import javax.validation.Valid;

 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.SessionAttributes;

 import com.muuves.reservosity.model.Business_Register;

 @Controller
 @SessionAttributes( {"company"})
 @RequestMapping("/business")
 public class BusinessController {
private final static Logger LOG = Logger.getLogger(BusinessController.class .getName()); 


@RequestMapping(method = RequestMethod.GET)
public String business(ModelMap model) {
    LOG.info("Entering Business Home");
    model.put("company", new Business_Register());
    return "business";
}
@RequestMapping(value="/register", method = RequestMethod.GET)
public String businessRegister() {
    LOG.info("Register Business Page");
    return "business_register";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBusiness(@Valid Business_Register company, BindingResult result, ModelMap model) {
    LOG.info("Registering Business"+result.getErrorCount());
    if (result.hasErrors()){
        System.out.println("I found errors now now");
        return "business_register";
    }
    //companyService.addCompany(company);
    return "business_add";
}

}

这是jsp

 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

 <div class="row main-content-reservosity">
<div class="twelve columns">
    <h3>Register Your Business</h3>
    <p>Please register your business details below</p>
    <form:form method="post" action="add" commandName="company">
        <form:errors path="Company"></form:errors>
        <form:input type="text" placeholder="Company Name" path="Company"/>
        <div class="row">
            <div class="six columns">
                <form:input type="text" placeholder="Address Line 1" path="Address_Line_1"/>
                <form:errors path="Address_Line_1"></form:errors>
            </div>
            <div class="six columns">
                <form:input type="text" placeholder="Address Line 2" path="Address_Line_2"/>
                <form:errors path="Address_Line_2"></form:errors>
            </div>
        </div>
        <div class="row">
            <div class="four columns">
                <form:input type="text" placeholder="Postcode" path="Postcode"/>
                <form:errors path="Postcode"></form:errors>
            </div>
            <div class="four columns">
                <form:input type="text" placeholder="County" path="County"/>
                <form:errors path="County"></form:errors>
            </div>
            <div class="four columns">
                <form:input type="text" placeholder="Country" path="Country"/>
                <form:errors path="Country"></form:errors>
            </div>
        </div>
        <div class="row">
            <div class="six columns">
                <form:input type="text" placeholder="Mobile telephone" path="Mobile_Number"/>
                <form:errors path="Mobile_Number"></form:errors>
            </div>
            <div class="six columns">
                <form:input type="text" placeholder="Home telephone" path="Telephone_Number"/>
                <form:errors path="Telephone_Number"></form:errors>
            </div>
        </div>
        <form:errors path="Username"></form:errors>
        <form:input type="text" placeholder="Username" path="Username"/>
        <form:errors path="Password"></form:errors>
        <form:input type="password" placeholder="Password" path="Password"/>
        <br/>
        <button class="radius button" type="submit">Register</button>
        <button class="radius button" type="reset">Cancel</button>
    </form:form>
</div>
  </div>

   import org.hibernate.validator.constraints.NotBlank;

   public class Business_Register {

@NotBlank(message="Name field is mandatory.")
private String Company;
@NotBlank(message="Name field is mandatory.")
private String Address_Line_1;
@NotBlank(message="Name field is mandatory.")
private String Address_Line_2;
@NotBlank(message="Name field is mandatory.")
private String Postcode;
@NotBlank(message="Name field is mandatory.")
private String County;
@NotBlank(message="Name field is mandatory.")
private String Country;
@NotBlank(message="Name field is mandatory.")
private String Mobile_Number;
@NotBlank(message="Name field is mandatory.")
private String Telephone_Number;
@NotBlank(message="Name field is mandatory.")
private String Username;
@NotBlank(message="Name field is mandatory.")
private String Password;

public String getCompany() {
    return Company;
}
public void setCompany(String company) {
    Company = company;
}
public String getAddress_Line_1() {
    return Address_Line_1;
}
public void setAddress_Line_1(String address_Line_1) {
    Address_Line_1 = address_Line_1;
}
public String getAddress_Line_2() {
    return Address_Line_2;
}
public void setAddress_Line_2(String address_Line_2) {
    Address_Line_2 = address_Line_2;
}
public String getPostcode() {
    return Postcode;
}
public void setPostcode(String postcode) {
    Postcode = postcode;
}
public String getCounty() {
    return County;
}
public void setCounty(String county) {
    County = county;
}
public String getCountry() {
    return Country;
}
public void setCountry(String country) {
    Country = country;
}
public String getMobile_Number() {
    return Mobile_Number;
}
public void setMobile_Number(String mobile_Number) {
    Mobile_Number = mobile_Number;
}
public String getTelephone_Number() {
    return Telephone_Number;
}
public void setTelephone_Number(String telephone_Number) {
    Telephone_Number = telephone_Number;
}
public String getUsername() {
    return Username;
}
public void setUsername(String username) {
    Username = username;
}
public String getPassword() {
    return Password;
}
public void setPassword(String password) {
    Password = password;
}
  }

我已将此导入我的 pom.xml

            <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.1.Final</version>
    </dependency>
4

1 回答 1

1

需要添加@ModelAttribute("company") http://blog.nigelsim.org/2011/09/07/spring-mvc-validation-bindingresult/

它有效

你需要这样做似乎很荒谬

于 2013-01-17T20:41:13.107 回答