2015-08-14 57 views
0

在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; 
} 
+1

你沒有提到你如何觸發這個錯誤 – Dici

+0

**來自Alexandre Picard-Lemieux的評論**:該名稱的方法名稱是什麼? – Dici

+0

點擊UI中的提交按鈕之後 – Nastasia

回答

0

您正在使用POST,但需要GET。我對@RequestMapping瞭解不多,但這似乎很明顯,那裏存在問題。最後,你的表格沒有action,所以它幾乎沒有機會做任何事情。