2015-08-14 124 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,所以它几乎没有机会做任何事情。