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



下午好,

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

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

我只用团队实体开始了这个项目,并成功地创建了我的类(DAO、控制器、服务等(和 jsp 来创建新团队。

现在,我创建了类 Country 来关联这两个实体,并在"添加团队表单.jsp"中添加了一个下拉列表以选择新团队的国家/地区。此页面显示正确(所有国家/地区都显示在下拉列表中(,但是,当我单击"提交"创建新团队时,出现错误"客户端发送的请求语法不正确"。

你能帮我识别我的错误吗?我猜它是在"添加团队形式.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;
}
...
}

我的"添加团队表单.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

我在这里可以看到几个问题 - 您正在发布到 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 文档中有更多信息

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

最新更新