2017-07-07 138 views
2

下午好,Spring MVC:创建新实体记录时,客户端发送的请求在语法上不正确

我是Spring MVC的新手。我在运行我的项目时遇到以下错误“客户端发送的请求在语法上不正确。”

我的项目有两个实体,团队和国家有一个ManyToOne关系。这两个实体都映射在mysql数据库中创建的表。

我只用Team实体启动了项目,并成功创建了我的类(DAO,控制器,服务等)和jsp来创建新的团队。

现在,我创建了类Country来关联两个实体,并且在“add-team-form.jsp”中添加了一个下拉列表来选择新团队的国家。此页面正确显示(所有国家都显示在下拉列表中),但是,当我单击“提交”创建新团队时,我收到错误“客户端发送的请求在语法上不正确。”

你能帮我找出我的错误吗?我猜这是在“add-team-form.jsp”中。

1 - 实体团队:

@Entity 
@Table(name="teams") 
public class Team implements Serializable{ 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Integer id; 

@Column(name = "name", length = 40, nullable = false) 
private String name; 

@Column(name = "rating", length = 6, nullable = false) 
private Integer rating; 

@ManyToOne(fetch = FetchType.EAGER) 
@JoinColumn(name = "id_country", nullable = false) 
private Country country; 


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 Integer getRating() { 
    return rating; 
} 
public void setRating(Integer rating) { 
    this.rating = rating; 
} 
public Country getCountry() { 
    return country; 
} 
public void setCountry(Country country) { 
    this.country = country; 
} 
} 

2 - 实体国家:

@Entity 
@Table(name = "countries") 
public class Country implements Serializable{ 

@Id 
@Column(name= "id_country", length = 6) 
private String idCountry; 

@Column(name = "name", length = 255, nullable = false) 
private String name; 

@OneToMany(fetch = FetchType.LAZY, mappedBy = "country") 
private List<Team> teams; 


public String getIdCountry() { 
    return idCountry; 
} 

public void setIdCountry(String idCountry) { 
    this.idCountry = idCountry; 
} 

public String getName() { 
    return name; 
} 

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

我的团队DAO

@Repository 
public class TeamDAOImpl implements TeamDAO { 

@Autowired 
private SessionFactory sessionFactory; 

private Session getCurrentSession() { 
    return sessionFactory.getCurrentSession(); 
} 

@Override 
public void addTeam(Team team) { 

    getCurrentSession().save(team); 
} 
} 

我的团队服务

@Service 
@Transactional 
public class TeamServiceImpl implements TeamService { 

@Autowired 
private TeamDAO teamDAO; 

public void addTeam(Team team) { 
    teamDAO.addTeam(team);  
} 

我的团队控制器

@Controller 
@RequestMapping(value="/team") 
public class TeamController { 

@Autowired 
private TeamService teamService; 

@Autowired 
private FilterService filterService; 

@RequestMapping(value="/add", method=RequestMethod.GET) 
public ModelAndView addTeamPage() { 
    ModelAndView modelAndView = new ModelAndView("add-team-form"); 
    modelAndView.addObject("team", new Team()); 

    return modelAndView; 
} 

@RequestMapping(value="/add", method=RequestMethod.POST) 
public ModelAndView addingTeam(@ModelAttribute Team team) { 

    ModelAndView modelAndView = new ModelAndView("home"); 

    teamService.addTeam(team); 

    String message = "Team was successfully added."; 
    modelAndView.addObject("message", message); 

    return modelAndView; 
} 

@ModelAttribute("countryList") 
public Map<String, String> getCountryList(){ 

    Map<String, String> countryList = filterService.getCountries(); 
    return countryList; 
    } 

... 
} 

我的“附加团队form.jsp”

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<?xml version="1.0" encoding="ISO-8859-1" ?> 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Add team page</title> 
</head> 
<body> 
    <h1>Add team page</h1> 

<form:form method="POST" 
     modelAttribute="team" 
     action="${pageContext.request.contextPath}/team/add.html"> 
<table> 
    <tbody> 
    <tr> 
     <td>Name:</td> 
     <td><form:input path="name" /></td> 
    </tr> 
    <tr> 
     <td>Rating:</td> 
     <td><form:input path="rating" /></td> 
    </tr> 
    <tr> 
    <td><label>Country</label></td> 
    <td> 
     <form:select path="country.idCountry"> 
      <form:options items="${countryList}" /> 
     </form:select>  
    </td> 
    <tr> 
     <td><input type="submit" value="Add" /></td> 
     <td></td> 
    </tr> 

</tbody> 
</table> 
</form:form> 

</body> 
</html> 

有在Eclipse的控制台显示没有错误,但这里是错误IM从浏览器接收:

HTTP Status 400 - 

type Status report 

message 

description The request sent by the client was syntactically incorrect. 
Apache Tomcat/7.0.47 
+0

的更多信息,你能否告诉我们完整的堆栈跟踪? –

+0

@TanmayDelhikar我用浏览器收到的错误更新了问题,在eclipse的控制台中没有显示错误。 – MiguelDuque

+0

打开日志到DEBUG;如果使用Boot,这里是[how](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html)。如果浏览器收到错误,则还会在服务器端打印一个错误。 –

回答

1

我在这里可以看到一些问题 - 您正在发布add/team/add.html,而不是击中您的帖子处理程序。在发布到同一端点时,您不需要action属性;

<form:form method="POST" modelAttribute="team" > 

其次你被注入国家的地图,所以这些都是ID /显示值这对于键/值对和值绑定到一个字符串字段的伟大工程。在这种情况下,Spring试图将你的国家ID(字符串)绑定到team.country(Country)字段,该字段将失败。为了帮助Spring出来,你需要一个数据绑定器;在你的控制器中添加;

@InitBinder 
public void initBinder (WebDataBinder binder) { 
    binder.registerCustomEditor(Country.class, new CountryEditor()); 
} 

并创建属性编辑器类;

public class CountryEditor extends PropertyEditorSupport { 
    @Override 
    public void setValue(Object value) { 
     super.setValue(value); 
    } 

    public String getAsText() { 
     if (getValue() == null) return null; 
     return ((Country) getValue()).getName(); 
    }; 

    public void setAsText(String text) throws IllegalArgumentException { 
     if (text != null) { 
      Country country = // something like filterService.getCountryById(text); 
      setValue(country); 
     } 
    }; 
} 

有一个在Spring documentation

+0

谢谢你的帮助! – MiguelDuque

-1

如果参数丢失或者格式不同,并且无法转换为预期类型,则通常会收到错误。检查值将被传递给Team对象。您可以记录请求和响应,或者将日志级别设置为“DEBUG”,这将在日志中显示确切的错误。

相关问题