客户端发送的请求在语法上是不正确的.-Spring MVC + JDBC Template

 zjy396999 发布于 2023-02-10 10:02

我是Spring MVC的新手.我在运行项目时遇到错误 错误 - 客户端发送的请求在语法上是不正确的. 我有一个实体类PatientInfo.我的jsp页面是demo1.我的控制器是Patient Controller.我想要实现的功能是将值插入数据库.但我无法在控制器中调用我的函数(add-update2).

demo1.jsp

        <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>


Registration Form


Full Registration Form


First Name
Middle Name
Last Name
Age
Gender
Marital Status
Nationality
Date Of Birth Click Here to Pick up the timestamp
E-mail
Blood Group

Controller-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 bloodList = new ArrayList();
        List genderList = new ArrayList();
         List nationalityList = new ArrayList();
          List maritalList = new ArrayList();

        bloodList=patientService.getAllBloodgroup();
        genderList= patientService.getAllGendertype();
        nationalityList=patientService.getAllNationality();
        maritalList=patientService.getAllMaritalstatus();

        for(int i=0;i

实体类 - 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 + " ]";
    }

}

Sotirios Del.. 26

我认为问题是Spring在提交以下input字段时不知道如何反序列化浏览器客户端发送的日期


    Date Of Birth
    
        Click Here to Pick up the timestamp
    

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));
}

显然,将其更改为SimpleDateFormat客户端发送的任何内容.


在相关说明中,您通过发送重定向发送302响应

return "redirect:/full-reg";

请记住,请求和模型属性仅在一个请求的持续时间内存在.因此,当您的客户端发送请求时full-reg,您最初发送的表单输入参数都不再存在.你应该重新思考如何做到这一点.

1 个回答
  • 我认为问题是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"   border="0" alt="Click Here to Pick up the timestamp"></a>
        </td>
    </tr>
    

    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));
    }
    

    显然,将其更改为SimpleDateFormat客户端发送的任何内容.


    在相关说明中,您通过发送重定向发送302响应

    return "redirect:/full-reg";
    

    请记住,请求和模型属性仅在一个请求的持续时间内存在.因此,当您的客户端发送请求时full-reg,您最初发送的表单输入参数都不再存在.你应该重新思考如何做到这一点.

    2023-02-10 10:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有