【发布时间】:2016-03-27 15:37:31
【问题描述】:
我创建了一个 MVC 结构来为患者保存新记录,但我不断收到 404 错误。这是我的代码,
Patient.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;
}
}
道实现类
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());
}
}
调度程序小服务程序
<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>
PatientForm.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 状态错误
【问题讨论】:
-
看来问题不在jsp。试试我提供的方式。
标签: java spring jsp spring-mvc