客户端发送的请求语法错误-Spring MVC + JDBC Template
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20616319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The request sent by the client was syntactically incorrect.-Spring MVC + JDBC Template
提问by north_head
I am newbie to Spring MVC. I was stuck by an error while running my project Error-The request sent by the client was syntactically incorrect.I have an entity class PatientInfo. My jsp page is demo1. My controller is Patient Controller. The functionality i want to implement is Inserting values into database. But i am not able to call my function(add-update2) in controller.
我是 Spring MVC 的新手。我在运行我的项目时遇到错误 错误 - 客户端发送的请求在语法上不正确。我有一个实体类 PatientInfo。我的jsp页面是demo1。我的控制器是患者控制器。我要实现的功能是将值插入数据库。但是我无法在控制器中调用我的函数(add-update2)。
demo1.jsp
演示1.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2 align="center">Full Registration Form</h2>
<hr />
<table align="center" cellpadding="5" cellspacing="5">
<form:form modelAttribute="patientInfo" method="POST" action="add-update2">
<tr>
<td> First Name</td>
<td><form:input path="firstName"/></td>
</tr>
<tr>
<td>Middle Name</td>
<td><form:input path="middleName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastName"/>
</td>
</tr>
<tr>
<td>Age</td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td>Gender</td>
<td><form:select path="gender">
<form:option value="" label="Select Gender" />
<form:options items="${genderList}" itemLabel="gender" itemValue="gender" />
</form:select></td>
</tr>
<tr>
<td>Marital Status</td>
<td><form:select path="maritalStatus">
<form:option value="" label="Select Marital Status" />
<form:options items="${maritalList}" itemLabel="maritalstatus" itemValue="maritalstatus" />
</form:select></td>
</tr>
<tr>
<td>Nationality</td>
<td><form:select path="nationality">
<form:option value="" label="Select Nationality" />
<form:options items="${nationalityList}" itemLabel="country" itemValue="country" />
</form:select></td>
</tr>
<tr name="tstest">
<td>Date Of Birth</td>
<td><form:input path="dateOfBirth" name="timestamp" value=""/>
<a href="javascript:show_calendar('document.tstest.timestamp', document.tstest.timestamp.value);"><img src="../images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>
</td>
</tr>
<tr>
<td>E-mail</td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td>Blood Group</td>
<td><form:select path="bloodGroup">
<form:option value="" label="Select Blood Group" />
<form:options items="${bloodList}" itemLabel="bloodgroupname" itemValue="bloodgroupname" />
</form:select></td>
</tr>
<tr>
<td><input type="submit" value="submit"/></td>
</tr>
</form:form>
</table>
</body>
</html>
Controller-PatientController.java
控制器-PatientController.java
package com.app.ehr.api;
import com.app.ehr.domain.Bloodgroup;
import com.app.ehr.domain.Gendertype;
import com.app.ehr.entities.Patientinfo;
import com.app.ehr.domain.Maritalstatus;
import com.app.ehr.domain.Nationality;
import com.app.ehr.model.Patient;
import com.app.ehr.service.PatientService;
import org.springframework.stereotype.Controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class PatientController {
public PatientService patientService;
@Autowired
public PatientController(PatientService patientService){
this.patientService = patientService;
}
@RequestMapping(value="/", method= RequestMethod.GET)
public String index(ModelMap map) {
return "index";
}
@RequestMapping(value="/full-reg", method= RequestMethod.GET)
public String fullreg(ModelMap map,Patientinfo patientInfo) {
List<Bloodgroup> bloodList = new ArrayList<Bloodgroup>();
List<Gendertype> genderList = new ArrayList<Gendertype>();
List<Nationality> nationalityList = new ArrayList<Nationality>();
List<Maritalstatus> maritalList = new ArrayList<Maritalstatus>();
bloodList=patientService.getAllBloodgroup();
genderList= patientService.getAllGendertype();
nationalityList=patientService.getAllNationality();
maritalList=patientService.getAllMaritalstatus();
for(int i=0;i<bloodList.size();i++)
{
System.out.println("---------------------Controller"+bloodList.get(i));
}
// map.addAttribute("hello", "Hello Spring from Netbeans!!");
map.addAttribute("patientInfo", patientInfo);
map.addAttribute("bloodList", patientService.getAllBloodgroup());
map.addAttribute("genderList", patientService.getAllGendertype());
map.addAttribute("maritalList", patientService.getAllMaritalstatus());
map.addAttribute("nationalityList", patientService.getAllNationality());
return "demo1";
}
@RequestMapping(value="/add-update2", method= RequestMethod.POST)
public String addUpdate(@ModelAttribute("patientInfo") Patientinfo patientInfo) {
System.out.println("----------------------------------------- From Controller------------------------------------------------");
//patientService.addPatient(patientInfo);
return "redirect:/full-reg";
}
}
Entity Class- PatientInfo.java
实体类- PatientInfo.java
package com.app.ehr.entities;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author HP LAPTOP
*/
@Entity
@Table(name = "patientinfo")
@NamedQueries({
@NamedQuery(name = "Patientinfo.findAll", query = "SELECT p FROM Patientinfo p")})
public class Patientinfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "PatientKey")
private Long patientKey;
@Column(name = "PatientMRNumber")
private String patientMRNumber;
@Column(name = "IntPrimaryPhysicianKey")
private BigInteger intPrimaryPhysicianKey;
@Column(name = "FirstName")
private String firstName;
@Column(name = "MiddleName")
private String middleName;
@Column(name = "LastName")
private String lastName;
@Column(name = "Age")
private Short age;
@Column(name = "Gender")
private String gender;
@Column(name = "Nationality")
private String nationality;
@Column(name = "DateOfBirth")
@Temporal(TemporalType.TIMESTAMP)
private Date dateOfBirth;
@Column(name = "MaritalStatus")
private String maritalStatus;
@Column(name = "Occupation")
private String occupation;
@Column(name = "AnnualIncome")
private String annualIncome;
@Column(name = "BloodGroup")
private String bloodGroup;
@Column(name = "Email")
private String email;
@Column(name = "ModeOfPayment")
private String modeOfPayment;
@Column(name = "ModeOfPaymentAlt")
private String modeOfPaymentAlt;
@Column(name = "ExtPrimaryPhysicianName")
private String extPrimaryPhysicianName;
@Column(name = "ExtPrimaryPhysicianPhoneNumber")
private String extPrimaryPhysicianPhoneNumber;
@Column(name = "IsDeleted")
private Boolean isDeleted;
@Column(name = "Meta_CreatedByUser")
private String metaCreatedByUser;
@Column(name = "Meta_UpdatedDT")
@Temporal(TemporalType.TIMESTAMP)
private Date metaUpdatedDT;
@Column(name = "Meta_CreatedDT")
@Temporal(TemporalType.TIMESTAMP)
private Date metaCreatedDT;
public Patientinfo() {
}
public Patientinfo(Long patientKey) {
this.patientKey = patientKey;
}
public Long getPatientKey() {
return patientKey;
}
public void setPatientKey(Long patientKey) {
this.patientKey = patientKey;
}
public String getPatientMRNumber() {
return patientMRNumber;
}
public void setPatientMRNumber(String patientMRNumber) {
this.patientMRNumber = patientMRNumber;
}
public BigInteger getIntPrimaryPhysicianKey() {
return intPrimaryPhysicianKey;
}
public void setIntPrimaryPhysicianKey(BigInteger intPrimaryPhysicianKey) {
this.intPrimaryPhysicianKey = intPrimaryPhysicianKey;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getAnnualIncome() {
return annualIncome;
}
public void setAnnualIncome(String annualIncome) {
this.annualIncome = annualIncome;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getModeOfPayment() {
return modeOfPayment;
}
public void setModeOfPayment(String modeOfPayment) {
this.modeOfPayment = modeOfPayment;
}
public String getModeOfPaymentAlt() {
return modeOfPaymentAlt;
}
public void setModeOfPaymentAlt(String modeOfPaymentAlt) {
this.modeOfPaymentAlt = modeOfPaymentAlt;
}
public String getExtPrimaryPhysicianName() {
return extPrimaryPhysicianName;
}
public void setExtPrimaryPhysicianName(String extPrimaryPhysicianName) {
this.extPrimaryPhysicianName = extPrimaryPhysicianName;
}
public String getExtPrimaryPhysicianPhoneNumber() {
return extPrimaryPhysicianPhoneNumber;
}
public void setExtPrimaryPhysicianPhoneNumber(String extPrimaryPhysicianPhoneNumber) {
this.extPrimaryPhysicianPhoneNumber = extPrimaryPhysicianPhoneNumber;
}
public Boolean getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(Boolean isDeleted) {
this.isDeleted = isDeleted;
}
public String getMetaCreatedByUser() {
return metaCreatedByUser;
}
public void setMetaCreatedByUser(String metaCreatedByUser) {
this.metaCreatedByUser = metaCreatedByUser;
}
public Date getMetaUpdatedDT() {
return metaUpdatedDT;
}
public void setMetaUpdatedDT(Date metaUpdatedDT) {
this.metaUpdatedDT = metaUpdatedDT;
}
public Date getMetaCreatedDT() {
return metaCreatedDT;
}
public void setMetaCreatedDT(Date metaCreatedDT) {
this.metaCreatedDT = metaCreatedDT;
}
@Override
public int hashCode() {
int hash = 0;
hash += (patientKey != null ? patientKey.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Patientinfo)) {
return false;
}
Patientinfo other = (Patientinfo) object;
if ((this.patientKey == null && other.patientKey != null) || (this.patientKey != null && !this.patientKey.equals(other.patientKey))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.app.ehr.entities.Patientinfo[ patientKey=" + patientKey + " ]";
}
}
回答by Sotirios Delimanolis
I think the issue is that Spring doesn't know how to deserialize the date your browser client sends when submitting the following input
field in
我认为问题在于 Spring 不知道如何反序列化浏览器客户端在提交以下input
字段时发送的日期
<tr name="tstest">
<td>Date Of Birth</td>
<td><form:input path="dateOfBirth" name="timestamp" value=""/>
<a href="javascript:show_calendar('document.tstest.timestamp', document.tstest.timestamp.value);"><img src="../images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>
</td>
</tr>
Spring doesn't know how to take the value that you enter into that field and convert it into a Date
object. You need to register a PropertyEditor
for that. For example, add the following to your @Controller
class
Spring 不知道如何获取您在该字段中输入的值并将其转换为Date
对象。您需要为此注册一个PropertyEditor
。例如,将以下内容添加到您的@Controller
课程中
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
Obviously, change the SimpleDateFormat
to whatever your client is sending.
显然,将 更改SimpleDateFormat
为您的客户端发送的任何内容。
On a related note, you're sending a 302 response by sending a redirect
在相关说明中,您通过发送重定向来发送 302 响应
return "redirect:/full-reg";
Remember that request and model attributes only live for the duration of one request. So when your client send the request to full-reg
, none of the form input parameters you sent originally exist any more. You should re-think how you do this.
请记住,请求和模型属性仅在一个请求的持续时间内有效。因此,当您的客户端向 发送请求时full-reg
,您最初发送的表单输入参数将不再存在。你应该重新考虑如何做到这一点。
回答by Piet
I came across the same error this morning. The problem with my code was that I had declared a variable as an integer in my form binding object but on the actual form I was capturing text. Changing the variable to the correct type worked out for me
今天早上我遇到了同样的错误。我的代码的问题是我在表单绑定对象中将变量声明为整数,但在实际表单中我正在捕获文本。将变量更改为正确的类型对我来说很有效
回答by Javierfdr
This happens when the defined binding does not match to what the user is sending. The most common issues are:
当定义的绑定与用户发送的内容不匹配时,就会发生这种情况。最常见的问题是:
- Missing PathVariable declaration
- Incomplete PathVariable declaration (for example missing value="variableName")
- Wrong data type, such as Sotirios Delimanolis answer above. If the Class of an input parameter cannot be serialized the request is not processable
- 缺少 PathVariable 声明
- 不完整的 PathVariable 声明(例如缺少 value="variableName")
- 错误的数据类型,例如上面的 Sotirios Delimanolis 答案。如果输入参数的类无法序列化,则请求不可处理
So, in general, be sure that:
因此,总的来说,请确保:
- Each PathVariable is declared
- A value is assigned that corresponds to value to be match in the path -
@PathVariable(value="myVariableName", String myVariable)
where the path defines@RequestMapping(value = "/userInfo/myVariableName", method = RequestMethod.GET)
- Each class declared for a PathVariable must be serializable.
- 声明了每个 PathVariable
- 分配的值对应于路径中要匹配的值 -
@PathVariable(value="myVariableName", String myVariable)
路径定义的位置@RequestMapping(value = "/userInfo/myVariableName", method = RequestMethod.GET)
- 为 PathVariable 声明的每个类都必须是可序列化的。
回答by Aekkawit Chanpen
In my case, I try to create an object that has ID and NAME as attributes. ID is int, NAME is String.But my js set values like this ID = '', NAME = 'brabrabra...'
就我而言,我尝试创建一个具有 ID 和 NAME 作为属性的对象。 ID 是整数,NAME 是字符串。但是我的 js 设置了这样的值 ID = '', NAME = 'brabrabra...'
After set ID = 0, problem is fixed.
设置 ID = 0 后,问题解决。
回答by Ocean's Fourteenth
I had a similar issue recently and solved it by annotating my Date field with the @DateTimeFormat
. In your case, you would edit your PatientInfo.java
file to:
我最近遇到了类似的问题,并通过使用@DateTimeFormat
. 在您的情况下,您可以将PatientInfo.java
文件编辑为:
import org.spring.framework.annotation.DateTimeFormat;
@Column(name = "DateOfBirth")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = ${pattern})
private Date dateOfBirth;
Make sure to replace ${pattern}
with a string representing the format that will be received (e.g. "yyyy-MM-dd").
确保${pattern}
用代表将接收的格式的字符串替换(例如“yyyy-MM-dd”)。
回答by storm_buster
try with this (with /add-update2
instead of just add-update2
) and replace modelAttribute
by commandName
试试这个(用/add-update2
而不是只是add-update2
)并替换modelAttribute
为commandName
<form:form commandName="patientInfo" method="POST" action="/add-update2">