Spring - 无法将java.lang.String类型的属性值转换为所需类型

时间:2017-01-29 17:08:19

标签: spring spring-mvc

我正在春季制作住房协会的项目。

当我试图将一个对象添加到我的公寓列表中时,我收到的错误是在页面上以某种方式写的: https://s28.postimg.org/vrhy6mbd9/blad.jpg

公寓与多对一建筑有关系。

公寓管制员:

package pl.dmcs.spoldzielnia.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import pl.dmcs.spoldzielnia.domain.Apartment;
import pl.dmcs.spoldzielnia.service.ApartmentService;
import pl.dmcs.spoldzielnia.service.BuildingService;




@Controller
@SessionAttributes
public class ApartmentController {

    @Autowired
    ApartmentService apartmentService;



    @Autowired
    BuildingService buildingService;



    @RequestMapping("admin/apartment")
    public String listApartment(Map<String, Object> map, HttpServletRequest request) {

        int apartmentId = ServletRequestUtils.getIntParameter(request, "apartmentId" , -1);

        if (apartmentId > 0)
        {   
            Apartment apartment = apartmentService.getApartment(apartmentId);

            apartment.setBuilding(buildingService.getBuilding(apartmentService.getApartment(apartmentId).getBuilding().getId()));


            map.put("selectedBuilding", apartmentService.getApartment(apartmentId).getBuilding().getId());
            map.put("apartment", apartment);
        }
        else
           map.put("apartment", new Apartment());   


        map.put("buildingList", buildingService.listBuilding());
        map.put("apartmentList", apartmentService.listApartment());

        return "apartment";
    }

   @RequestMapping(value = "admin/addApartment", method = RequestMethod.POST)
   public String addContact(@ModelAttribute("apartment") Apartment apartment, BindingResult result, 
           HttpServletRequest request, Map<String, Object> map) {



       if (result.getErrorCount()==0)
       {
           if (apartment.getId()==0)
           {

               if (apartment.getBuilding().getId() > 0)
                   apartment.setBuilding(buildingService.getBuilding(apartment.getBuilding().getId()));
               apartmentService.addApartment(apartment);
           }
           else
           {

               apartmentService.editApartment(apartment);
           }


           return "redirect:/admin/apartment.html";
       }


       map.put("buildingList", buildingService.listBuilding());
       map.put("apartmentList", apartmentService.listApartment());
       return "apartment";
   }

   @RequestMapping("admin/delete/apartment/{apartmentId}")
   public String deleteApartment(@PathVariable("apartmentId") Integer apartmentId) {
       apartmentService.removeApartment(apartmentId);

       return "redirect:/admin/apartment.html";
   }
//   @RequestMapping("/apartment")
//   public ModelAndView showContacts() {
//        
//       return new ModelAndView("apartment", "command", new Apartment());
//   }

域:

  package pl.dmcs.spoldzielnia.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="apartment")
public class Apartment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    @Column(name="apartmentNumber", nullable=false)
    private String number;

    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }




    @ManyToOne
    private Building building;

    public Building getBuilding() {
        return building;
    }
    public void setBuilding(Building building) {
        this.building = building;
    }
}


}

构建服务实施:

package pl.dmcs.spoldzielnia.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pl.dmcs.spoldzielnia.dao.BuildingDAO;
import pl.dmcs.spoldzielnia.domain.Building;
import pl.dmcs.spoldzielnia.domain.Building;

@Service
@Transactional
public class BuildingServiceImpl implements BuildingService{

    @Autowired
    BuildingDAO buildingDAO;

    @Transactional
    public void addBuilding(Building building) {


        buildingDAO.addBuilding(building);
    }

    @Transactional
    public List<Building> listBuilding() {

        return buildingDAO.listBuilding();
    }

    @Transactional
    public Building getBuilding(int id) {
        return buildingDAO.getBuilding(id);
    }

    @Transactional
    public void removeBuilding(int id) {
        buildingDAO.removeBuilding(id);
    }

    @Transactional
    public void editBuilding(Building building) {

        buildingDAO.editBuilding(building);
    }
}

你能帮我解决一下我的问题吗?

1 个答案:

答案 0 :(得分:0)

问题是你假设Spring MVC能够从传递的数据中填充你的Apartment对象。从表单看起来,建筑物编号是12,这可能是数据库中建筑物的唯一标识符,但是Spring MVC如何知道如何进入数据库,检索正确的建筑物对象并将其放入公寓对象?

请记住,通过SpringMVC参数映射的对象是常规Java POJO,而不是Hibernate附加实体。因此,当映射发生时,SpringMVC正试图放置&#34; 12&#34;将Building类型的building属性放入POJO(这解释了你得到的错误)。

您有两种选择: 首先,您可以注册一个自定义格式化程序,它将使用传递的id从数据库中检索Building:

@RequestMapping(value = "admin/addApartment", method = RequestMethod.POST)
   public String addContact(@ModelAttribute("apartment") Apartment apartment, @RequestParam("buildingId") String buildingId, BindingResult result, HttpServletRequest request, Map<String, Object> map) {
       if (result.getErrorCount()==0){
           if (apartment.getId()==0){
               apartment.setBuilding(buildingService.getBuilding(buildingId));
               apartmentService.addApartment(apartment);
            }
        }
       else{
           apartmentService.editApartment(apartment);
       }

       return "redirect:/admin/apartment.html";
   }

   map.put("buildingList", buildingService.listBuilding());
   map.put("apartmentList", apartmentService.listApartment());
   return "apartment";
}

并注册:

decimal

其次,通过在单独的参数中发送建筑物ID来手动完成这项工作:

$dict = array();
foreach($occupancies as $o)
    $dict[$o['room_name']] = $o; // if you can have more than one person per room you need to have an array here with $dict[$o['room_name']][] = $o;

并相应地更改HTML以发送buildingId值。