在JSP下拉框中,我有一个国家/地区列表。选定的国家我应该将“组织”对象指定为对象“国家”,但我有错误请求错误。
JSP,已编辑:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@include file='css/styles.css'%>
<html>
<head>
<title>TestWebApp</title>
</head>
<body>
<table align = "center" class="table" cellspacing='0'>
<tr>
<th valign = "top">Add an organization</th>
</tr>
<form method="POST">
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="${name}" /></td>
</tr>
<tr>
<td>Country: </td>
<td>
<%--
<select name="country">
<c:forEach var="country" items="${countries}">
<option value="${country}">${country.name}</option>
</c:forEach>
</select>
--%>
</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" value="${address}" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" value="${phone}" /></td>
</tr>
<tr>
<td>Market Cap:</td>
<td><input type="text" name="marketCap" value="${marketCap}" /></td>
</tr>
</table>
<table align = "center" class="table" cellspacing='0'>
<tr>
<td><input type="reset" /></td>
<td><input type="submit" /></td>
</table>
</form>
</body>
</html>
模特,组织:
@Entity
public class Organization {
@Id
@GeneratedValue
private Integer id;
private String name;
//I should assign "Country" object to this field
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="country")
private Country country;
private String address;
private String phone;
private Long market_cap;
public Organization(Integer id, String name, Country country, String address, String phone, Long market_cap) {
this.id = id;
this.name = name;
this.country = country;
this.address = address;
this.phone = phone;
this.market_cap = market_cap;
}
...// getters and setters
模特,国家:
@Entity
public class Country {
@Id
@GeneratedValue
private Integer id_country;
private String name;
private String isocode;
public Country() {
}
public Country(Integer id_country, String name, String isocode) {
this.id_country = id_country;
this.name = name;
this.isocode = isocode;
} ...//getters and setters
控制器
@RequestMapping(value="add", method=RequestMethod.GET)
public ModelAndView addOrganization() {
ModelAndView modelAndView = new ModelAndView("add");
Organization organization = new Organization();
modelAndView.addObject("organization", organization);
List<Country> countries = countryService.listOfCountries();
modelAndView.addObject("countries", countries);
return modelAndView;
}