【发布时间】:2017-05-03 12:18:10
【问题描述】:
我正在使用 Spring mvc 并尝试在一个 jsp 中插入两个表中的数据
我的桌子:
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>
如何为汽车和城市的名称创建/插入数据?
【问题讨论】:
标签: java hibernate jsp spring-mvc