JSP,Spring - 从下拉框中分配给对象选定的值

时间:2015-08-14 13:56:56

标签: java spring jsp

在JSP下拉框中,我有一个国家/地区列表。我应该选择的国家/地区"组织"对象作为对象"国家"。

JSP:

<select name="country">
   <c:forEach var="country" items="${countries}" >
     <option value="${country}">
          <c:out value="${country.name}" />
     </option>
   </c:forEach>
</select>

模特,组织:

@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;
    }

    public Organization() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Long getMarket_cap() {
        return market_cap;
    }

    public void setMarketCap(Long market_cap) {
        this.market_cap = market_cap;
    }

}

模特,国家:

@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;
    }

    public Integer getId_country() {
        return id_country;
    }

    public void setId_country(Integer id_country) {
        this.id_country = id_country;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIsocode() {
        return isocode;
    }

    public void setIsocode(String isocode) {
        this.isocode = isocode;
    }
}

控制器方法:

@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;
}

我的错误请求错误。

0 个答案:

没有答案