如何在spring mvc 4上将我的表单值从jsp发布到controller时解决ClassCastException

时间:2017-03-30 12:34:54

标签: java spring jsp spring-mvc

我正在尝试更新实体。但是在将值发布到控制器时会出现ClassCastException。当我试图调试它不进入方法。我将在这里发布我的源代码和POJO类。

Driver Master POJO Class

@Id
@GeneratedValue
@Column(name = "ROW_ID")
private long row_id;

@NotEmpty
@Column(name = "DRIVER_NAME", nullable = false, length=50)
private String driverName;

@NotEmpty
@Column(name = "VEHICLE_NO", nullable = false, length=50)
private String vehicleNo;

@Column(name = "DRIVER_STATUS", nullable = false, length=50)
private int driverStatus;

@ManyToOne
@JoinColumn(name="DELZONE_ID")
private DeliveryZones deliveryZones;


//bi-directional many-to-one association to TaskMaster
@OneToMany(mappedBy="driverMaster")
private List<TaskMaster> taskMasters;

转换器类

public class DeliveryZoneToDriverConverter implements Converter<Object, DeliveryZones>{

static final Logger logger = LoggerFactory.getLogger(DeliveryZoneToDriverConverter.class);

@Autowired
DeliveryZoneService deliveryZoneService;

/**
 * Gets UserProfile by Id
 * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
 */
public DeliveryZones convert(Object element) {
    Integer id = Integer.parseInt((String)element);
    DeliveryZones profile= deliveryZoneService.findById(id);
    logger.info("Profile : {}",profile);
    return profile;
}

控制器

@RequestMapping(value = { "/driver/newdriver" }, method = RequestMethod.POST)
public String saveDriver(@Valid DriverMaster driverMaster, BindingResult result,
        ModelMap model, final HttpServletRequest request) {

    if (result.hasErrors()) {
        return "driver/adddriver";
    }



    driverMaster.setDriverStatus(1);//to add active initially
    driverService.saveDriverMaster(driverMaster);

    model.addAttribute("success", "Driver " + driverMaster.getDriverName()
            + " Created successfully");
    model.addAttribute("loggedinuser", getPrincipal());
    // return "success";
    return "driver/adddriversuccess";
}

得到错误

2017-03-30 18:10:27,052 INFO  [stdout] (default task-33) Hibernate: 
2017-03-30 18:10:27,053 INFO  [stdout] (default task-33)     select
2017-03-30 18:10:27,053 INFO  [stdout] (default task-33)         this_.ROW_ID as ROW_ID1_7_2_,
2017-03-30 18:10:27,054 INFO  [stdout] (default task-33)         this_.CITY_ID as CITY_ID3_7_2_,
2017-03-30 18:10:27,054 INFO  [stdout] (default task-33)         this_.DELZONE_NAME as DELZONE_2_7_2_,
2017-03-30 18:10:27,055 INFO  [stdout] (default task-33)         citymaster2_.ROW_ID as ROW_ID1_3_0_,
2017-03-30 18:10:27,055 INFO  [stdout] (default task-33)         citymaster2_.CITY_NAME as CITY_NAM2_3_0_,
2017-03-30 18:10:27,056 INFO  [stdout] (default task-33)         citymaster2_.state_id as state_id3_3_0_,
2017-03-30 18:10:27,056 INFO  [stdout] (default task-33)         statemaste3_.ROW_ID as ROW_ID1_17_1_,
2017-03-30 18:10:27,056 INFO  [stdout] (default task-33)         statemaste3_.country_id as country_3_17_1_,
2017-03-30 18:10:27,057 INFO  [stdout] (default task-33)         statemaste3_.STATE_NAME as STATE_NA2_17_1_ 
2017-03-30 18:10:27,057 INFO  [stdout] (default task-33)     from
2017-03-30 18:10:27,057 INFO  [stdout] (default task-33)         DELIVERY_ZONES this_ 
2017-03-30 18:10:27,058 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,058 INFO  [stdout] (default task-33)         CITY_MASTER citymaster2_ 
2017-03-30 18:10:27,058 INFO  [stdout] (default task-33)             on this_.CITY_ID=citymaster2_.ROW_ID 
2017-03-30 18:10:27,058 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,059 INFO  [stdout] (default task-33)         STATE_MASTER statemaste3_ 
2017-03-30 18:10:27,059 INFO  [stdout] (default task-33)             on citymaster2_.state_id=statemaste3_.ROW_ID 
2017-03-30 18:10:27,059 INFO  [stdout] (default task-33)     order by
2017-03-30 18:10:27,059 INFO  [stdout] (default task-33)         this_.DELZONE_NAME asc

2017-03-30 18:10:27,064 INFO  [stdout] (default task-33) Hibernate: 
2017-03-30 18:10:27,064 INFO  [stdout] (default task-33)     select
2017-03-30 18:10:27,064 INFO  [stdout] (default task-33)         countrymas0_.ROW_ID as ROW_ID1_4_0_,
2017-03-30 18:10:27,065 INFO  [stdout] (default task-33)         countrymas0_.COUNTRY_NAME as COUNTRY_2_4_0_ 
2017-03-30 18:10:27,065 INFO  [stdout] (default task-33)     from
2017-03-30 18:10:27,065 INFO  [stdout] (default task-33)         COUNTRY_MASTER countrymas0_ 
2017-03-30 18:10:27,065 INFO  [stdout] (default task-33)     where
2017-03-30 18:10:27,065 INFO  [stdout] (default task-33)         countrymas0_.ROW_ID=?

2017-03-30 18:10:27,089 INFO  [com.aaratech.medtrack.dao.DriverDaoImpl] (default task-33) driverId : 5
2017-03-30 18:10:27,091 INFO  [stdout] (default task-33) Hibernate: 
2017-03-30 18:10:27,091 INFO  [stdout] (default task-33)     select
2017-03-30 18:10:27,091 INFO  [stdout] (default task-33)         this_.ROW_ID as ROW_ID1_8_2_,
2017-03-30 18:10:27,092 INFO  [stdout] (default task-33)         this_.CREATED_BY as CREATED_2_8_2_,
2017-03-30 18:10:27,092 INFO  [stdout] (default task-33)         this_.CREATED_DATE as CREATED_3_8_2_,
2017-03-30 18:10:27,092 INFO  [stdout] (default task-33)         this_.DELZONE_ID as DELZONE13_8_2_,
2017-03-30 18:10:27,092 INFO  [stdout] (default task-33)         this_.DRIVER_NAME as DRIVER_N4_8_2_,
2017-03-30 18:10:27,092 INFO  [stdout] (default task-33)         this_.DRIVER_STATUS as DRIVER_S5_8_2_,
2017-03-30 18:10:27,093 INFO  [stdout] (default task-33)         this_.EMAIL_ID as EMAIL_ID6_8_2_,
2017-03-30 18:10:27,093 INFO  [stdout] (default task-33)         this_.IMEI_NO as IMEI_NO7_8_2_,
2017-03-30 18:10:27,093 INFO  [stdout] (default task-33)         this_.IS_ACTIVE as IS_ACTIV8_8_2_,
2017-03-30 18:10:27,093 INFO  [stdout] (default task-33)         this_.MODIFIED_BY as MODIFIED9_8_2_,
2017-03-30 18:10:27,093 INFO  [stdout] (default task-33)         this_.MODIFIED_DATE as MODIFIE10_8_2_,
2017-03-30 18:10:27,094 INFO  [stdout] (default task-33)         this_.PHONE as PHONE11_8_2_,
2017-03-30 18:10:27,094 INFO  [stdout] (default task-33)         this_.VEHICLE_NO as VEHICLE12_8_2_,
2017-03-30 18:10:27,094 INFO  [stdout] (default task-33)         deliveryzo2_.ROW_ID as ROW_ID1_7_0_,
2017-03-30 18:10:27,094 INFO  [stdout] (default task-33)         deliveryzo2_.CITY_ID as CITY_ID3_7_0_,
2017-03-30 18:10:27,094 INFO  [stdout] (default task-33)         deliveryzo2_.DELZONE_NAME as DELZONE_2_7_0_,
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)         citymaster3_.ROW_ID as ROW_ID1_3_1_,
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)         citymaster3_.CITY_NAME as CITY_NAM2_3_1_,
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)         citymaster3_.state_id as state_id3_3_1_ 
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)     from
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)         DRIVER_MASTER this_ 
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)         DELIVERY_ZONES deliveryzo2_ 
2017-03-30 18:10:27,095 INFO  [stdout] (default task-33)             on this_.DELZONE_ID=deliveryzo2_.ROW_ID 
2017-03-30 18:10:27,096 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,096 INFO  [stdout] (default task-33)         CITY_MASTER citymaster3_ 
2017-03-30 18:10:27,096 INFO  [stdout] (default task-33)             on deliveryzo2_.CITY_ID=citymaster3_.ROW_ID 
2017-03-30 18:10:27,096 INFO  [stdout] (default task-33)     where
2017-03-30 18:10:27,096 INFO  [stdout] (default task-33)         this_.ROW_ID=?

2017-03-30 18:10:27,100 INFO  [stdout] (default task-33) Hibernate: 
2017-03-30 18:10:27,101 INFO  [stdout] (default task-33)     select
2017-03-30 18:10:27,101 INFO  [stdout] (default task-33)         statemaste0_.ROW_ID as ROW_ID1_17_0_,
2017-03-30 18:10:27,101 INFO  [stdout] (default task-33)         statemaste0_.country_id as country_3_17_0_,
2017-03-30 18:10:27,102 INFO  [stdout] (default task-33)         statemaste0_.STATE_NAME as STATE_NA2_17_0_,
2017-03-30 18:10:27,102 INFO  [stdout] (default task-33)         countrymas1_.ROW_ID as ROW_ID1_4_1_,
2017-03-30 18:10:27,102 INFO  [stdout] (default task-33)         countrymas1_.COUNTRY_NAME as COUNTRY_2_4_1_ 
2017-03-30 18:10:27,103 INFO  [stdout] (default task-33)     from
2017-03-30 18:10:27,103 INFO  [stdout] (default task-33)         STATE_MASTER statemaste0_ 
2017-03-30 18:10:27,103 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,103 INFO  [stdout] (default task-33)         COUNTRY_MASTER countrymas1_ 
2017-03-30 18:10:27,104 INFO  [stdout] (default task-33)             on statemaste0_.country_id=countrymas1_.ROW_ID 
2017-03-30 18:10:27,104 INFO  [stdout] (default task-33)     where
2017-03-30 18:10:27,104 INFO  [stdout] (default task-33)         statemaste0_.ROW_ID=?

2017-03-30 18:10:27,154 INFO  [stdout] (default task-33) Hibernate: 
2017-03-30 18:10:27,154 INFO  [stdout] (default task-33)     select
2017-03-30 18:10:27,154 INFO  [stdout] (default task-33)         deliveryzo0_.ROW_ID as ROW_ID1_7_0_,
2017-03-30 18:10:27,154 INFO  [stdout] (default task-33)         deliveryzo0_.CITY_ID as CITY_ID3_7_0_,
2017-03-30 18:10:27,154 INFO  [stdout] (default task-33)         deliveryzo0_.DELZONE_NAME as DELZONE_2_7_0_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         citymaster1_.ROW_ID as ROW_ID1_3_1_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         citymaster1_.CITY_NAME as CITY_NAM2_3_1_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         citymaster1_.state_id as state_id3_3_1_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         statemaste2_.ROW_ID as ROW_ID1_17_2_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         statemaste2_.country_id as country_3_17_2_,
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         statemaste2_.STATE_NAME as STATE_NA2_17_2_ 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)     from
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         DELIVERY_ZONES deliveryzo0_ 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         CITY_MASTER citymaster1_ 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)             on deliveryzo0_.CITY_ID=citymaster1_.ROW_ID 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)     left outer join
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         STATE_MASTER statemaste2_ 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)             on citymaster1_.state_id=statemaste2_.ROW_ID 
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)     where
2017-03-30 18:10:27,155 INFO  [stdout] (default task-33)         deliveryzo0_.ROW_ID=?

2017-03-30 18:10:27,162 INFO  [com.aaratech.medtrack.converter.DeliveryZoneToDriverConverter] (default task-33) Profile : null
2017-03-30 18:10:32,655 INFO  [stdout] (default task-47) Hibernate: 
2017-03-30 18:10:32,656 INFO  [stdout] (default task-47)     select
2017-03-30 18:10:32,656 INFO  [stdout] (default task-47)         this_.ROW_ID as ROW_ID1_7_2_,
2017-03-30 18:10:32,656 INFO  [stdout] (default task-47)         this_.CITY_ID as CITY_ID3_7_2_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         this_.DELZONE_NAME as DELZONE_2_7_2_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         citymaster2_.ROW_ID as ROW_ID1_3_0_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         citymaster2_.CITY_NAME as CITY_NAM2_3_0_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         citymaster2_.state_id as state_id3_3_0_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         statemaste3_.ROW_ID as ROW_ID1_17_1_,
2017-03-30 18:10:32,657 INFO  [stdout] (default task-47)         statemaste3_.country_id as country_3_17_1_,
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)         statemaste3_.STATE_NAME as STATE_NA2_17_1_ 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)     from
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)         DELIVERY_ZONES this_ 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)     left outer join
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)         CITY_MASTER citymaster2_ 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)             on this_.CITY_ID=citymaster2_.ROW_ID 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)     left outer join
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)         STATE_MASTER statemaste3_ 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)             on citymaster2_.state_id=statemaste3_.ROW_ID 
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)     order by
2017-03-30 18:10:32,658 INFO  [stdout] (default task-47)         this_.DELZONE_NAME asc

2017-03-30 18:10:32,662 INFO  [stdout] (default task-47) Hibernate: 
2017-03-30 18:10:32,662 INFO  [stdout] (default task-47)     select
2017-03-30 18:10:32,662 INFO  [stdout] (default task-47)         countrymas0_.ROW_ID as ROW_ID1_4_0_,
2017-03-30 18:10:32,662 INFO  [stdout] (default task-47)         countrymas0_.COUNTRY_NAME as COUNTRY_2_4_0_ 
2017-03-30 18:10:32,662 INFO  [stdout] (default task-47)     from
2017-03-30 18:10:32,662 INFO  [stdout] (default task-47)         COUNTRY_MASTER countrymas0_ 
2017-03-30 18:10:32,663 INFO  [stdout] (default task-47)     where
2017-03-30 18:10:32,663 INFO  [stdout] (default task-47)         countrymas0_.ROW_ID=?

2017-03-30 18:10:32,670 WARN  [org.springframework.validation.DataBinder] (default task-47) Skipping URI variable 'row_id' since the request contains a bind value with the same name.
2017-03-30 18:10:32,684 INFO  [stdout] (default task-47) Hibernate: 
2017-03-30 18:10:32,684 INFO  [stdout] (default task-47)     select
2017-03-30 18:10:32,684 INFO  [stdout] (default task-47)         deliveryzo0_.ROW_ID as ROW_ID1_7_0_,
2017-03-30 18:10:32,685 INFO  [stdout] (default task-47)         deliveryzo0_.CITY_ID as CITY_ID3_7_0_,
2017-03-30 18:10:32,685 INFO  [stdout] (default task-47)         deliveryzo0_.DELZONE_NAME as DELZONE_2_7_0_,
2017-03-30 18:10:32,685 INFO  [stdout] (default task-47)         citymaster1_.ROW_ID as ROW_ID1_3_1_,
2017-03-30 18:10:32,685 INFO  [stdout] (default task-47)         citymaster1_.CITY_NAME as CITY_NAM2_3_1_,
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)         citymaster1_.state_id as state_id3_3_1_,
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)         statemaste2_.ROW_ID as ROW_ID1_17_2_,
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)         statemaste2_.country_id as country_3_17_2_,
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)         statemaste2_.STATE_NAME as STATE_NA2_17_2_ 
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)     from
2017-03-30 18:10:32,686 INFO  [stdout] (default task-47)         DELIVERY_ZONES deliveryzo0_ 
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)     left outer join
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)         CITY_MASTER citymaster1_ 
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)             on deliveryzo0_.CITY_ID=citymaster1_.ROW_ID 
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)     left outer join
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)         STATE_MASTER statemaste2_ 
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)             on citymaster1_.state_id=statemaste2_.ROW_ID 
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)     where
2017-03-30 18:10:32,687 INFO  [stdout] (default task-47)         deliveryzo0_.ROW_ID=?

2017-03-30 18:10:32,690 INFO  [stdout] (default task-47) Hibernate: 
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)     select
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)         countrymas0_.ROW_ID as ROW_ID1_4_0_,
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)         countrymas0_.COUNTRY_NAME as COUNTRY_2_4_0_ 
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)     from
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)         COUNTRY_MASTER countrymas0_ 
2017-03-30 18:10:32,690 INFO  [stdout] (default task-47)     where
2017-03-30 18:10:32,691 INFO  [stdout] (default task-47)         countrymas0_.ROW_ID=?

2017-03-30 18:10:32,696 INFO  [com.aaratech.medtrack.converter.DeliveryZoneToDriverConverter] (default task-47) Profile : DeliveryZones [id=2]
2017-03-30 18:10:32,701 WARN  [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-47) Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type [com.aaratech.medtrack.model.DriverMaster] to required type [com.aaratech.medtrack.model.DriverMaster]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [com.aaratech.medtrack.model.DriverMaster] to type [@javax.validation.Valid com.aaratech.medtrack.model.DriverMaster] for value 'DriverMaster [row_id=5]'; nested exception is java.lang.ClassCastException: com.aaratech.medtrack.model.DriverMaster cannot be cast to java.lang.String

1 个答案:

答案 0 :(得分:1)

由于错误是

  

无法将Driver-Master类型的值转换为所需类型   Driver-Master嵌套异常是Conversion-Failed-Exception

这里Spring尝试将对象Driver-Master转换为String,就像在这里完成一样。

public Delivery-Zones convert(Object element) {
    Integer id = Integer.parseInt((String)element);
}

请通过覆盖它来编写自己的转换器方法。