[英]The request sent by the client was syntactically incorrect. in @ManyToOne relation Hibernate, Spring
it's my first post on StackOverflow. 这是我在StackOverflow上的第一篇文章。 Sorry for my english. 对不起我的英语不好。 I have a problem with adding employees in the project Spring MVC and Hibernate. 我在Spring MVC和Hibernate项目中添加员工时遇到问题。 I run it on tomcat 7.047. 我在tomcat 7.047上运行它。 I think the problem lies in the JSP or Hibernate database mapping. 我认为问题出在JSP或Hibernate数据库映射。 When I add a department in the same way it works. 当我以相同的方式添加部门时。 Oddzial and Pracownik relation in database: Oddzial [or Department in English] 1 : n Pracownik[Employee] 数据库中的Oddzial和Pracownik关系:Oddzial [或英语的部门] 1:n Pracownik [Employee]
Error output: 错误输出:
type Status report message description The request sent by the client was syntactically incorrect. 类型状态报告消息描述客户端发送的请求在语法上不正确。
My Employee [Pracownik] class: 我的员工[Pracownik]课程:
@Entity
@Table(name = "pracownicy")
@Inheritance(strategy=InheritanceType.JOINED)
public class Pracownik implements Serializable {
private static final long serialVersionUID = -2556993070168215685L;
@Id
@GeneratedValue
private Integer pracownik_id;
@ManyToOne(cascade = {CascadeType.MERGE})
@JoinColumn(name="oddzial_id", nullable = false)
private Oddzial oddzial_id;
private String imie;
private String nazwisko;
private String adres_zamieszkania;
private String miasto;
@Column(columnDefinition = "enum('Pelny','Pol')")
@Enumerated(EnumType.STRING)
private Etat etat;
private String stanowisko;
private Double pensja;
@ManyToOne(cascade = {CascadeType.MERGE})
@JoinColumn(name="kierownik_id", nullable = true)
private Pracownik kierownik_id;
//getters and setters generated by eclipse
}
My oddzial [Department] class: 我的奇数[部门]班:
@Entity
@Table(name="oddzial")
public class Oddzial implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer oddzial_id;
private String miasto;
private String ulica;
//getters and setters generated by eclipse
}
My EmployeeController 我的EmployeeController
@Controller
@RequestMapping(value="/pracownik")
public class PracownikController {
@Autowired
private PracownikService pracownikService;
@Autowired
private OddzialService oddzialService;
@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addPracownikPage() {
List<Oddzial> oddzialList = oddzialService.getOddzialy();
ModelAndView modelAndView = new ModelAndView("add-pracownik-form");
modelAndView.addObject("pracownik", new Pracownik());
modelAndView.addObject("oddzialy", oddzialList);
return modelAndView;
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView addingPracownik(@ModelAttribute Pracownik pracownik, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("home");
pracownikService.addPracownik(pracownik);
String message = "Pracownik was successfully added.";
System.out.println(message);
modelAndView.addObject("message", message);
return modelAndView;
}
My JSP - adder of employee form: I have used and and still the same. 我的JSP-员工表的加法器:我曾经使用和仍然相同。
<form:form method="POST" commandName="pracownik" action="${pageContext.request.contextPath}/pracownik/add.html">
<table>
<tbody>
<tr>
<td>Oddzial ID:</td>
<td><form:radiobuttons path="oddzial_id" items='${oddzialy}'/></td>
</tr>
<tr>
<td>Imię:</td>
<td><form:input path="imie" /></td>
</tr>
<tr>
<td>Nazwisko:</td>
<td><form:input path="nazwisko" /></td>
</tr>
<tr>
<td>Pensja:</td>
<td><form:input path="pensja" /></td>
</tr>
<tr>
<td>Stanowisko:</td>
<td><form:input path="stanowisko" /></td>
</tr>
<tr>
<td>Adres:</td>
<td><form:input path="adres_zamieszkania" /></td>
</tr>
<tr>
<td><input type="submit" value="Add" /></td>
<td></td>
</tr>
</tbody>
</table>
</form:form>
If you need more information please write. 如果您需要更多信息,请写信。 Thank you in advance for your help. 预先感谢您的帮助。
Getter and setter for Oddzial and adres_Zamieszkania. Oddzial和adres_Zamieszkania的Getter和Setter。
public Oddzial getOddzial_id() {
return oddzial_id;
}
public void setOddzial_id(Oddzial oddzial_id) {
this.oddzial_id = oddzial_id;
}
public String getAdres_zamieszkania() {
return adres_zamieszkania;
}
public void setAdres_zamieszkania(String adres_zamieszkania) {
this.adres_zamieszkania = adres_zamieszkania;
}
Your form has the following element 您的表单具有以下元素
<form:radiobuttons path="oddzial_id" items='${oddzialy}'/>
which gets populated from 从中填充
List<Oddzial> oddzialList = oddzialService.getOddzialy();
ModelAndView modelAndView = new ModelAndView("add-pracownik-form");
modelAndView.addObject("pracownik", new Pracownik());
modelAndView.addObject("oddzialy", oddzialList);
This will create an html <form>
<input>
element for each object in the oddziaList
List
. 这将为oddziaList
List
中的每个对象创建一个html <form>
<input>
元素。 The value
attribute's value will come from the toString()
method of the Oddzial
class. value
属性的值将来自Oddzial
类的toString()
方法。 Regardless of what that value is, unless you have a Converter
to convert from the return value of the toString()
to an object of type Oddzial
, Spring will be unable to make the conversion itself and will therefore give you a 400 Bad Request. 无论该值是多少,除非您有一个Converter
可以将toString()
的返回值Converter
为Oddzial
类型的对象, Oddzial
Spring本身将无法进行转换,因此将给您400 Bad Request。
You have two options: 您有两种选择:
org.springframework.core.convert.converter.Converter
to do the conversion. 实现并注册一个自定义org.springframework.core.convert.converter.Converter
进行转换。 <form:radiobuttons>
to a c:forEach>
containing regular <input type="radio">
elements that contain the id of the Oddzial
objects. 将<form:radiobuttons>
更改为c:forEach>
其中包含常规的<input type="radio">
元素,这些元素包含Oddzial
对象的ID。 In your controller, use that submitted ID to find the appropriate Oddzial
object and set it on the Pracownik
object before submitting it to your service. 在您的控制器中,使用该提交的ID查找适当的Oddzial
对象,然后将其设置在Pracownik
对象上,然后再将其提交给您的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.