当前位置: 首页 > 知识库问答 >
问题:

HTTP状态400客户端发送的请求在Spring中语法不正确

闾丘高峰
2023-03-14

我已经创建了一个MVC结构来为患者保存一个新的记录,我不断收到404错误。这是我的密码,

病人JAVA

public class Patient {
private int patient_ID;
private String  name;
private String gender;
private int age;
private Date dob;

public Patient(){

}

public Patient(int patient_ID, String name, String gender, int age, Date dob) {
    this.patient_ID = patient_ID;
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.dob = dob;
}
public int getPatient_ID() {
    return patient_ID;
}
public void setPatient_ID(int patient_ID) {
    this.patient_ID = patient_ID;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public Date getDob() {
    return dob;
}
public void setDob(Date dob) {
    this.dob = dob;
}

}

控制器类

@Controller
public class PatientController {

@RequestMapping(value="/newPatient",method=RequestMethod.GET)
public ModelAndView newPatient(ModelAndView model) {                                                         Patient newpatient  = new Patient();
    model.addObject("patient", newpatient);
    model.setViewName("PatientForm");
    System.out.println("sending to patient form");
    return model;
}

@RequestMapping(value = "/savePatient", method = RequestMethod.POST)
public ModelAndView savePatient(ModelAndView model,@ModelAttribute Patient patient) {

    PatientDaoImplementation patientDaoImpl = new PatientDaoImplementation();
    patientDaoImpl.saveOrUpdate(patient);
    List<Patient> listPatient = patientDaoImpl.patientList();
    model.addObject("listPatient",listPatient);
    model.setViewName("home");
    return model;
}

}

Dao实现类

public class PatientDaoImplementation {

private DataSource dataSource;
private static JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
    System.out.println("datasource"+dataSource);
    this.dataSource = dataSource;
    this.setJdbcTemplate(dataSource);
}
public void setJdbcTemplate(DataSource ds) {
    this.jdbcTemplate= new JdbcTemplate(ds);
}

public PatientDaoImplementation(){

}

//get all records in Patient
        public List<Patient> patientList() {
            String sql ="SELECT * from patient";
        List<Patient> listPatient = jdbcTemplate.query(sql, new RowMapper<Patient>(){
                public Patient mapRow(ResultSet rs, int arg1) throws SQLException {
                    Patient patient = new Patient();
                    patient.setPatient_ID(rs.getInt("Patient_ID"));
                    patient.setName(rs.getString("Name"));
                    patient.setAge(rs.getInt("Age"));
                    patient.setGender(rs.getString("Gender"));
                    patient.setDob(rs.getDate("DOB"));
                    return patient;
                }
            });
            return listPatient;
        }

        public void saveOrUpdate(Patient patient) {
                //insert
            String sql = "INSERT INTO patient (Patient_ID, Name, Gender, Age,DOB)" + " VALUES (?, ?, ?, ?,?)";

                jdbcTemplate.update(sql, patient.getPatient_ID(),patient.getName(),patient.getGender(),patient.getAge(),patient.getDob());
            }
        }

调度servlet

<context:component-scan base-package="com.csc.*" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/hospital" />
    <property name="username" value="root" />
    <property name="password" value="bgowda" />
</bean>

<bean id="patientDaoImpl" class="com.csc.bg.daoimpl.PatientDaoImplementation">
    <property name="dataSource" ref="dataSource" />
</bean>

耐心形成。jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New Patient</title>
</head>
<body>
    <div align="center">
        <h1>New Patient</h1>
         <form:form action="save" method="post" modelAttribute="patient" > 
        <table>
            <form:hidden path="patient_ID"/>
            <tr>
                <td>Name:</td>
                <td><form:input path="name"  /></td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td><form:input path="gender" /></td>
            </tr>
            <tr>
                <td>Age:</td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td>DOB:</td>
                <td><form:input path="dob"  /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Save"></td>
            </tr>
        </table>
        </form:form>
    </div>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/Dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

我可以获取列表,但当我尝试从PatientForm保存患者详细信息时。jsp时,表单未将其提交给控制器,并给出400状态错误

共有3个答案

益英逸
2023-03-14

请尝试将此action=“save”发送到action=“savePatient”。因为在控制器类中没有名为save的操作。

拓拔霄
2023-03-14

嗯,日期字段(dob)是可疑的,您的框架如何知道如何在没有以下内容的情况下进行转换:

@DateTimeFormat(pattern = "yyyy-MM-dd")
荀正谊
2023-03-14

当数据发送到控制器时,所有数据都作为String发送。因此,dob字段也作为String发送。在模型病人中复制数据时,dob字段为日期类型。

默认情况下,Spring最初没有配置为从String类型自动转换为Date类型。Spring不知道这是一个Date,它把它看作一个String。因此,复制到dob字段的数据失败。

您必须在控制器中绑定String to Date转换器,以便在将数据复制到患者时,String dob可以转换为Date。

要做到这一点,在您的控制器中添加以下内容:

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    sdf.setLenient(true);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
 类似资料: