2017-05-03 36 views
0

进出口使用Spring MVC和两个表中的尝试在一个JSP中插入数据如何从两个表中的一个JSP春季插入数据MVC的

我的表:

1.Cars 
    Id 
    Name 
    CityId(FK) 

2.City 
    Id 
    Name 

我在下面尝试,但它只是在列表中选择,我希望它是将数据插入

我的豆输入文本:

类汽车:

@Entity 
@Table(name="CARS") 
public class Car { 
    @Id 
    @Column(name="ID", nullable=false, unique=true) 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private int id; 

    @ManyToOne 
    @JoinColumn(name="CITY_ID") 
    private City city;  
    // get and set 
} 

级市

@Entity 
@Table(name="CITY") 
public class City { 
    @Id 
    @Column(name="ID", nullable = false, unique = true) 
    @GeneratedValue(strategy=GenerationType.AUTO) 

    private int id; 
    @Column(name = "NAME") 
    private String name; 
    // get and set 
} 

类控制器:

@RequestMapping(value="/CreateCar", method = RequestMethod.POST) 
public ModelAndView setCreateCarPage(HttpServletRequest request, 
            HttpServletResponse response, 
            ModelMap model, @RequestParam("citySelected") Integer citySelected,@ModelAttribute("createCar") Car createCar) { 
    City myCity = cityService.getCityById(citySelected); 
    createCar.setCityId(myCity); 

    carService.createCar(createCar); 
    return new ModelAndView("createCar"); 
} 

JSP:

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/CreateCar" enctype="multipart/form-data"> 
    <fieldset style="width:300px"> 

    <label>Name of Car</label> 
     <form:input path="name" id="name"/> 

    <label>Name of City</label> 
     <select name="citySelected"> 
      <c:forEach items="${cityList}" var="city"> 
       <option value="${city.id}">${city.name}</option> 
      </c:forEach> 
     </select> 
    <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" /> 
    </fieldset> 
</form:form> 

如何为汽车和城市的名称创建/插入数据?

回答

0

您的代码中几乎没有错误。

首先在你的控制器中,你有一个叫做setCityId()的方法,但是它的参数是一个City。你也不需要这里的请求/响应。

在地方CreateCar您的请求名称使用大写,使用createCar

你应该改变这样的:

@RequestMapping(value="/createCar", method = RequestMethod.POST) 
public ModelAndView setCreateCarPage(@ModelAttribute("createCar") Car createCar) { 
    carService.createCar(createCar); 
    return new ModelAndView("createCar"); 
} 

的在你的JSP代码,您需要设置您的汽车的city.id下拉菜单

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/createCar" enctype="multipart/form-data"> 
    <fieldset style="width:300px"> 

    <label>Name of Car</label> 
     <form:input path="name" id="name"/> 

    <label>Name of City</label> 
     <select name="citySelected" path="city.id"> 
      <c:forEach items="${cityList}" var="city"> 
       <option value="${city.id}">${city.name}</option> 
      </c:forEach> 
     </select> 
    <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" /> 
    </fieldset> 
</form:form> 
0

你好,我知道你正在使用列表都你的问题,你可以通过添加前缀作为表名如处理这个问题:

汽车名称:NAME =“Car.name”

城市名称:通过将该你的问题NAME = “Car.City.name”

将得到解决。

0

在JSP更新你的选择是这样的:

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/CreateCar" enctype="multipart/form-data"> 
    <fieldset style="width:300px"> 

    <label>Noi dung</label> 
     <form:input path="name" id="name"/> 
    <label>Tinh/Thanh pho</label> 
     <form:select path="city"> 
     <form:options items="${cityList}" itemLabel="name" itemValue="id"/> 
    </form:select> 
    <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" /> 
    </fieldset> 
</form:form> 

,并在控制器更新此梅索德:

@RequestMapping(value="/CreateCar", method = RequestMethod.POST) 
public ModelAndView setCreateCarPage(HttpServletRequest request, 
            HttpServletResponse response, 
            ModelMap model,@ModelAttribute("createCar") Car createCar) { 
    LOG.info("current city "+createCar.getCity().toString()); 
carService.createCar(createCar); 
    return new ModelAndView("createCar"); 
} 
相关问题