Ad

How To Resolve This Message -Failed To Convert Property Value Of Type Java.lang.String To Required Type

- 1 answer

I was building an simple application using spring roo. After doing certain job, I have removed roo completely.

So here is my controller methods for creating new data..

@RequestMapping(params = "form", produces = "text/html")
public String createForm(Model uiModel) {
    populateEditForm(uiModel, new PatientDetails());
    return "patientdetailses/create";
}

 @RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid PatientDetails patientDetails,
        BindingResult bindingResult, Model uiModel,
        HttpServletRequest httpServletRequest) {

    if (bindingResult.hasErrors()) {
        populateEditForm(uiModel, patientDetails);
        return "patientdetailses/create";
    }
    uiModel.asMap().clear();

    patientDetailsService
            .savePatientDetails(setCurrentUser(patientDetails));
    return "redirect:/patientdetailses/"
            + encodeUrlPathSegment(patientDetails.getId().toString(),
                    httpServletRequest);
}

 void populateEditForm(Model uiModel, PatientDetails patientDetails) {
    uiModel.addAttribute("patientDetails", patientDetails);
    addDateTimeFormatPatterns(uiModel);
    uiModel.addAttribute("sexes", Arrays.asList(Sex.values()));
    uiModel.addAttribute("typeOfPatients",
            typeOfPatientService.findAllTypeOfPatients());
}

my domain:

 public class PatientDetails {

@NotNull
@NotBlank
@NotEmpty
@Size(max = 40)
private String firstName;

@NotNull
@NotBlank
@NotEmpty
@Size(max = 40)
private String lastName;

@Size(max = 40)
private String middleName;

@Size(max = 200)
@NotNull
@NotBlank
@NotEmpty
private String address;

@NotNull
@NotBlank
@NotEmpty
private String city;

@NotNull
private String province;

@NotNull
@Size(max = 5)
private String postalCode;

@NotNull
@Email
@Size(max = 100)
private String email;

@Size(max = 12)
private String homePhone;

@Size(max = 12)
private String workPhone;

@NotNull
private Sex sex;

@Past
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
@NotNull
private Date dateOfBirth;

@NotNull
@Size(max = 10)
private String socialSecurityName;

@NotNull
@Size(max = 100)
private String occupation;

@ManyToOne
private TypeOfPatient typeOfPatient;

@ManyToOne
private User users;
   }

and the views :

  <div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<form:create id="fc_com_simplemed_npc_domain_PatientDetails" modelAttribute="patientDetails" path="/patientdetailses" render="${empty dependencies}" z="8/olU0ivxZlcCzNCDYriOP+hE2U=">
    <field:input field="firstName" id="c_com_simplemed_npc_domain_PatientDetails_firstName" required="true" z="user-managed"/>
    <field:input field="lastName" id="c_com_simplemed_npc_domain_PatientDetails_lastName" required="true" z="user-managed"/>
    <field:input field="middleName" id="c_com_simplemed_npc_domain_PatientDetails_middleName" z="user-managed"/>
    <field:textarea field="address" id="c_com_simplemed_npc_domain_PatientDetails_address" required="true" z="0CYYBU4YKijB7hQ3Npq7QXHb2ys="/>
    <field:input field="city" id="c_com_simplemed_npc_domain_PatientDetails_city" required="true" z="pQoSTWOVKlwKWZV5OlKvsCwQt6M="/>
    <field:input field="province" id="c_com_simplemed_npc_domain_PatientDetails_province" required="true" z="7hzMJ6GrQfV6YMHTSNpUIw2z508="/>
    <field:input field="postalCode" id="c_com_simplemed_npc_domain_PatientDetails_postalCode" max="5" required="true" z="QttQ/Av8Y2LXLPjpc88NRqtu7kI="/>
    <field:input field="email" id="c_com_simplemed_npc_domain_PatientDetails_email" required="true" validationMessageCode="field_invalid_email" z="user-managed"/>
    <field:input field="homePhone" id="c_com_simplemed_npc_domain_PatientDetails_homePhone" max="12" z="pBKxDLFxHZKsvHUjMqozutbbkug="/>
    <field:input field="workPhone" id="c_com_simplemed_npc_domain_PatientDetails_workPhone" max="12" z="LViJ3WNDosJPBWk6AGJmcZrnjfw="/>
    <field:select field="sex" id="c_com_simplemed_npc_domain_PatientDetails_sex" items="${sexes}" path="sexes" required="true" z="OjuzCMOizRZlHrPGgWVrV6wnR0k="/>
    <field:datetime dateTimePattern="${patientDetails_dateofbirth_date_format}" field="dateOfBirth" id="c_com_simplemed_npc_domain_PatientDetails_dateOfBirth" past="true" required="true" z="tthi2TXacQkSlyRj+QX9HsbzaIs="/>
    <field:input field="socialSecurityName" id="c_com_simplemed_npc_domain_PatientDetails_socialSecurityName" max="10" required="true" z="lKuMt6y/W4VI1dJEiW3gk5ZvK3c="/>
    <field:textarea field="occupation" id="c_com_simplemed_npc_domain_PatientDetails_occupation" required="true" z="WfCTmH7Nkt3bMfkJ1HNpkqe+aa8="/>
    <field:select field="typeOfPatient" id="c_com_simplemed_npc_domain_PatientDetails_typeOfPatient" itemValue="id" itemLabel="patientTypeName" required="true" items="${typeOfPatients}" path="/typeofpatients" z="Uj2xfoaPX5/gkwXb9WBxdzAfN/4="/>
</form:create>
<form:dependency dependencies="${dependencies}" id="d_com_simplemed_npc_domain_PatientDetails" render="${not empty dependencies}" z="OEZW6AYtVJ5mEmMbn24lHjWtPp4="/>

I'm getting errors while submitting forms enter image description here

Can anyone please help me on this...

Thanks in Advance.

Edit:

TypeOfPatient Class:

 public class TypeOfPatient {

@NotNull
private String patientTypeName;

@NotNull
@Size(max=1024)
private String decriptions;

@Override
public String toString() {
    return patientTypeName;
}

}

My Conversion Code :

  @Autowired
TypeOfPatientService typeOfPatientService;

public Converter<String, TypeOfPatient> getStringToTypeOfPatientConverter() {
    return new Converter<String, TypeOfPatient>() {
        @Override
        public TypeOfPatient convert(String id) {
            return typeOfPatientService.findTypeOfPatient(Long
                    .parseLong(id));
        }
    };
}
Ad

Answer

You basically have to provide a Converter.

EDIT:

According to your comments you use <mvc:annotation-driven conversion-service="applicationConversionService"/>. If that's something like this class, below code should work:

public class ApplicationConversionServiceFactoryBean
    extends FormattingConversionServiceFactoryBean {

  @Autowired
  TypeOfPatientService typeOfPatientService;

  @Override
  protected void installFormatters(FormatterRegistry registry) {
    registry.addConverter(getStringToTypeOfPatientConverter());
    super.installFormatters(registry);
  }

  public Converter<String, TypeOfPatient> getStringToTypeOfPatientConverter() {
    return new Converter<String, TypeOfPatient>() {
      @Override
      public TypeOfPatient convert(String id) {
        return typeOfPatientService.findTypeOfPatient(Long.parseLong(id));
      }
    };
  }
}
Ad
source: stackoverflow.com
Ad