无法在Spring MVC模型对象中添加Java Bean。抛出嵌套异常是java.lang.IllegalStateException:Validator的目标无效

时间:2014-11-10 08:36:06

标签: spring spring-mvc

我有一个保存控制器,它将数据插入到数据库中并返回必须在视图中显示的状态。

我已经填充bean中的所有值以在视图中呈现。

但是,嵌套异常是java.lang.IllegalStateException:Validator异常的目标无效。

出了什么问题。

请帮帮我。

代码

@Controller
@RequestMapping("/company/application")
public class CustomerInfoSaveControllerImpl implements
        CustomerInfoSaveController {

    private static Logger log = Logger
            .getLogger(CustomerInfoSaveControllerImpl.class);

    @Autowired
    private CustomerInfoService customerInfoService;

    @Autowired
    private CountryService countryService;

    @Autowired
    private IDTypeService idTypeService;

    @Autowired
    @Qualifier("customerInfoValidator")
    private Validator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }

    @ModelAttribute("customerInfoForm")
    public CustomerInfoForm createCustomerInfoFormModel() {
        return new CustomerInfoForm();
    }

    @RequestMapping(value = "/customerinfosave", method = RequestMethod.GET)
    public String createCustomerInfo(
            @ModelAttribute("customerInfoForm") @Validated CustomerInfoForm customerInfoForm,
            BindingResult bindingResult, Model model) {

        if (bindingResult.hasErrors()) {

            log.info("Error in Customer Information Form");

            Map<Long, String> countryList = new HashMap<Long, String>();

            Map<Long, String> idTypeList = new HashMap<Long, String>();

            List<CountryDTO> countryDTOs = countryService.getAllCountry();

            List<IDTypeDTO> idTypeDTOs = idTypeService.getAllIDType();

            for (CountryDTO countryDTO : countryDTOs) {
                countryList.put(countryDTO.getId(), countryDTO.getName());
            }

            for (IDTypeDTO idTypeDTO : idTypeDTOs) {
                idTypeList.put(idTypeDTO.getId(), idTypeDTO.getName());
            }

            model.addAttribute("hasCustomerCountryError", bindingResult
                    .getRawFieldValue("customerCountry").toString()
                    .equals("-1") ? true : false);
            model.addAttribute("hasIdCountryError",
                    bindingResult.getRawFieldValue("idCountry").toString()
                            .equals("-1") ? true : false);
            model.addAttribute("hasIdTypeError", bindingResult
                    .getRawFieldValue("idType").toString().equals("-1") ? true
                    : false);

            model.addAttribute("countryList", countryList);
            model.addAttribute("idTypeList", idTypeList);

            return "customerinfo";
        }

        CountryDTO customerCountryDTO = countryService
                .getCountry(customerInfoForm.getCustomerCountry());

        CountryDTO idCountryDTO = countryService.getCountry(customerInfoForm
                .getIdCountry());

        IDTypeDTO idTypeDTO = idTypeService.getIDType(customerInfoForm
                .getIdType());

        CustomerIDDTO customerIDDTO = new CustomerIDDTO();

        customerIDDTO.setIdExpiryDate(customerInfoForm.getIdExpiry());
        customerIDDTO.setIdType(idTypeDTO);
        customerIDDTO.setCountry(idCountryDTO);
        customerIDDTO.setIdPath("-");
        customerIDDTO.setIdNumber(customerInfoForm.getIdNumber());

        List<CustomerIDDTO> customerIDDTOs = new ArrayList<CustomerIDDTO>();
        customerIDDTOs.add(customerIDDTO);

        CustomerInfoDTO customerInfoDTO = new CustomerInfoDTO();
        customerInfoDTO.setContactNumber(customerInfoForm.getContactNumber());
        customerInfoDTO.setCountry(customerCountryDTO);
        customerInfoDTO.setCustomerIDs(customerIDDTOs);
        customerInfoDTO.setDateOfBirth(customerInfoForm.getDateOfBirth());
        customerInfoDTO.setFirstName(customerInfoForm.getFirstName());
        customerInfoDTO.setLastName(customerInfoForm.getLastName());
        customerInfoDTO.setMiddleName(customerInfoForm.getMiddleName());
        customerInfoDTO.setOccupation(customerInfoForm.getOccupation());

        if (customerInfoForm.getCustomerNumber() == null
                || customerInfoForm.getCustomerNumber().equals("")) {
            customerInfoDTO.setCustomerNumber("");
        }

        CustomerInfoDTO savedCustomerInfoDTO = customerInfoService
                .createCustomerInfo(customerInfoDTO);

        Country country = new Country();
        country.setCode("NOT");
        country.setName("NOT");
        country.setId(1l);

        model.addAttribute("customerInfoForm", country);

        return "customerinfosummary";
    }
}

Validator.java

public class CustomerInfoValidator implements Validator {

    public CustomerInfoValidator() {
    }

    public boolean supports(Class<?> paramClass) {
        return CustomerInfoForm.class.equals(paramClass);
    }

    public void validate(Object object, Errors errors) {

        CustomerInfoForm customerDetail = (CustomerInfoForm) object;

        Boolean error = false;

        if (customerDetail.getFirstName() == null
                || customerDetail.getFirstName().equals("")) {
            errors.reject("firstName", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("firstName", "ENTER ATLEAST ONE VALUE");

            error = true;
        }
        if (customerDetail.getMiddleName() == null
                || customerDetail.getMiddleName().equals("")) {
            errors.reject("middleName", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("middleName", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getLastName() == null
                || customerDetail.getLastName().equals("")) {
            errors.reject("lastName", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("lastName", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getContactNumber() == null
                || customerDetail.getContactNumber().equals("")) {
            errors.reject("contactNumber", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("contactNumber", "ENTER ATLEAST ONE VALUE",
                    "Please Enter All Mandatory Fields");
            error = true;
        }
        if (customerDetail.getCustomerCountry() == null
                || customerDetail.getCustomerCountry().equals("")
                || customerDetail.getCustomerCountry().equals(-1L)) {
            errors.reject("customerCountry", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("customerCountry", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getIdNumber() == null
                || customerDetail.getIdNumber().equals("")) {
            errors.reject("idNumber", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("idNumber", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getOccupation() == null
                || customerDetail.getOccupation().equals("")) {
            errors.reject("occupation", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("occupation", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getIdType() == null
                || customerDetail.getIdType().equals("")
                || customerDetail.getIdType().equals(-1L)) {
            errors.reject("idType", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("idType", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getDateOfBirth() == null
                || customerDetail.getDateOfBirth().equals("")) {
            errors.reject("dateOfBirth", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("dateOfBirth", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getIdExpiry() == null
                || customerDetail.getIdExpiry().equals("")) {
            errors.reject("idExpiry", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("idExpiry", "ENTER ATLEAST ONE VALUE");
            error = true;
        }
        if (customerDetail.getIdCountry() == null
                || customerDetail.getIdCountry().equals("")
                || customerDetail.getIdCountry().equals(-1L)) {
            errors.reject("idCountry", "ENTER ATLEAST ONE VALUE");
            errors.rejectValue("idCountry", "ENTER ATLEAST ONE VALUE");
            error = true;
        }

        if (error) {
            errors.rejectValue("errorMessage", "ENTER ATLEAST ONE VALUE",
                    "Please Enter All Mandatory Fields");
        }
    }
}

例外:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Invalid target for Validator [com.validator.CustomerInfoValidator@182c2aa]: NOTNOT1
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

0 个答案:

没有答案