进出口使用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>
如何为汽车和城市的名称创建/插入数据?