Ich mache ein Projekt der Housing Association im Frühjahr.Spring - Fehler beim Konvertieren des Eigenschaftswerts vom Typ java.lang.String in den erforderlichen Typ
Wenn ich versuche, ein Objekt zu meiner Liste der Wohnungen hinzufügen Ich erhalte einen Fehler, der irgendwie auf der Seite geschrieben: https://s28.postimg.org/vrhy6mbd9/blad.jpg
Apartments haben Beziehung Viele zu einem Gebäude.
Apartment Controller:
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());
// }
Domain:
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;
}
}
}
Building Service Implementation:
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);
}
}
Können Sie mir helfen, mein Problem zu lösen?
Hängen Sie das Ausnahmeprotokoll an. –