我正在为房屋协会做一个春天的项目。
当我试图将一个对象添加到我的公寓列表中时,我得到了一个以某种方式写在页面上的错误: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);
}
}你能帮我解决我的问题吗?
发布于 2017-01-30 05:22:15
问题是,您假设Spring MVC将能够从传递的数据填充您的公寓对象。从表单上看,Building number是12,这可能是数据库中Building的唯一标识符,但是Spring MVC如何知道如何访问数据库,检索正确的building对象并将其放入公寓对象?
请记住,通过SpringMVC参数映射的对象是常规的Java POJO,而不是Hibernate附加的实体。因此,当映射发生时,SpringMVC会尝试将"12“放入您的POJO中的类型为building的建筑属性中(这解释了您得到的错误)。
您有两个选择:首先,您可以注册一个自定义格式化程序,它将使用传递的id从数据库中检索建筑物:
import org.springframework.core.convert.converter.Converter;
public class BuildingIdToBuildingConverter implements Converter<String, Building> {
private BuildingService buildingService;
public BuildingIdToBuildingConverter(BuildingService buildingService) {
this.buildingService = buildingService;
}
@Override
public Building convert (String id) {
return buildingService.getBuilding(id);
}
}并注册它:
public class AppConfig extends WebMvcConfigurerAdapter {
...
@Bean
public BuildingService buildingService(){
return new BuildingService();
}
@Override
public void addFormatters (FormatterRegistry registry) {
registry.addConverter(new BuildingIdToBuildingConverter(buildingService()));
}
}其次,通过在单独的参数中发送建筑物id来手动完成此工作:
@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";
}并相应地更改您的HTML值以发送buildingId值。
https://stackoverflow.com/questions/41923601
复制相似问题