如何解决此消息 - 无法将java.lang.String类型的属性值转换为所需类型

时间:2012-09-12 12:49:13

标签: spring java-ee spring-mvc spring-roo

我正在使用spring roo构建一个简单的应用程序。完成某项工作后,我完全删除了roo。

所以这是我用于创建新数据的控制器方法..

@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());
}

我的域名:

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

和观点:

  <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="/>

我在提交表单时遇到错误 enter image description here

任何人都可以帮助我......

先谢谢。

编辑:

TypeOfPatient类:

 public class TypeOfPatient {

@NotNull
private String patientTypeName;

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

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

}

我的转换代码:

  @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));
        }
    };
}

1 个答案:

答案 0 :(得分:2)

您基本上必须提供Converter

修改

根据您的评论,您使用<mvc:annotation-driven conversion-service="applicationConversionService"/>。如果这类似于this class,则代码应该起作用:

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