我正在尝试将数据插入数据库并以Crud格式查看数据。但它显示错误消息HTTP状态404
我已经逐行检查了代码,没有错误。
但无法将数据显示到原始视图中。
Employee.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<h3>Add / Edit Employee!!!</h3>
<form method="post" action="/SpringMVCTutorial/employee.html" commandName="employee">
<div class="table-responsive">
<table class="table table-bordered" style="width: 300px">
<input type="text" name="id" />
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="dept" />
<input class="btn btn-primary btn-sm" type="submit" value="Submit" />
</tr>
</table>
</div>
</form>
<br>
<br>
<h3>List of Employees</h3>
<table class="table table-bordered" style="width: 300px">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Department</th>
<th>Edit/Delete</th>
</tr>
<c:forEach items="$ {employeeList}" var="employee">
<tr>
<td width="60" align="center">${employee.id}</td>
<td width="60" align="center">${employee.name}</td>
<td width="60" align="center">${employee.age}</td>
<td width="60" align="center">${employee.dept}</td>
<td width="60" align="center"><a href="edit/${employee.id}">Edit</a>/<a href="delete/${employee.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Employee.java
package com.javainterviewpoint;
import java.io.Serializable;
public class Employee implements Serializable
{
private static final long serialVersionUID = -1280037900360314186L;
private Integer id;
private String name;
private Integer age;
private String dept;
public Employee()
{
super();
}
public Employee(Integer id, String name, Integer age, String dept)
{
super();
this.id = id;
this.name = name;
this.age = age;
this.dept = dept;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Integer getAge()
{
return age;
}
public void setAge(Integer age)
{
this.age = age;
}
public String getDept()
{
return dept;
}
public void setDept(String dept)
{
this.dept = dept;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age == null)
{
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (dept == null)
{
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (id == null)
{
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null)
{
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString()
{
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + "]";
}
}
EmployeeController.java
package com.javainterviewpoint;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class EmployeeController
{
@Autowired
private EmployeeDAO employeeDAO;
@RequestMapping(value = "/employee",method=RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("employee") Employee employee)
{
try
{
if(employeeDAO.getEmployeeById(employee.getId()) != null);
employeeDAO.updateEmployee(employee);
}
catch(EmptyResultDataAccessException e)
{
System.out.println("inside catch");
employeeDAO.saveEmployee(employee);
}
return new ModelAndView("redirect:/employees");
}
@RequestMapping(value = "/edit/{id}")
public ModelAndView editEmployee(@ModelAttribute("employee") Employee employee,@PathVariable("id") int id)
{
ModelAndView model = new ModelAndView("employees");
employee = employeeDAO.getEmployeeById(id);
List<Employee> employeeList = employeeDAO.getAllEmployees();
model.addObject("employee",employee);
model.addObject("employeeList",employeeList);
return model;
}
@RequestMapping(value = "/delete/{id}")
public ModelAndView deleteEmployee(@ModelAttribute("employee") Employee employee,@PathVariable("id") int id)
{
employeeDAO.deleteEmployee(id);
return new ModelAndView("redirect:/employees");
}
@RequestMapping(value = "/employees")
public ModelAndView listEmployees(@ModelAttribute("employee") Employee employee)
{
ModelAndView model = new ModelAndView("employees");
List<Employee> employeeList = employeeDAO.getAllEmployees();
System.out.println(employeeList);
model.addObject("employeeList", employeeList);
return model;
}
}
EmployeeDao.java
package com.javainterviewpoint;
import java.util.List;
public interface EmployeeDAO
{
public void saveEmployee(Employee employee);
public Employee getEmployeeById(int id);
public void updateEmployee(Employee employee);
public void deleteEmployee(int id);
public List<Employee> getAllEmployees();
}
EmployeeDaoImpl.java
package com.javainterviewpoint;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeDAOImpl implements EmployeeDAO
{
private JdbcTemplate jdbcTemplate;
// JdbcTemplate setter
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
// Saving a new Employee
public void saveEmployee(Employee employee)
{
String sql = "insert into employee1 values(?,?,?,?)";
System.out.println("dao called");
jdbcTemplate.update(sql, new Object[]
{ employee.getId(), employee.getAge(), employee.getDept(), employee.getName() });
}
// Getting a particular Employee
public Employee getEmployeeById(int id)
{
String sql = "select * from employee1 where id=?";
Employee employee = (Employee) jdbcTemplate.queryForObject(sql, new Object[]
{ id }, new RowMapper<Employee>()
{
@Override
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException
{
Employee employee = new Employee();
employee.setId(rs.getInt(1));
employee.setAge(rs.getInt(2));
employee.setDept(rs.getString(3));
employee.setName(rs.getString(4));
return employee;
}
});
return employee;
}
// Getting all the Employees
public List<Employee> getAllEmployees()
{
String sql = "select * from employee1";
List<Employee> employeeList = jdbcTemplate.query(sql, new ResultSetExtractor<List<Employee>>()
{
@Override
public List<Employee> extractData(ResultSet rs) throws SQLException, DataAccessException
{
List<Employee> list = new ArrayList<Employee>();
while (rs.next())
{
Employee employee = new Employee();
employee.setId(rs.getInt(1));
employee.setAge(rs.getInt(2));
employee.setDept(rs.getString(3));
employee.setName(rs.getString(4));
list.add(employee);
}
return list;
}
});
return employeeList;
}
// Updating a particular Employee
public void updateEmployee(Employee employee)
{
String sql = "update employee1 set age =?, dept=?,name=? where id=?";
jdbcTemplate.update(sql, new Object[]
{ employee.getAge(), employee.getDept(), employee.getName(), employee.getId() });
}
// Deletion of a particular Employee
public void deleteEmployee(int id)
{
String sql = "delete employee1 where id=?";
jdbcTemplate.update(sql, new Object[]
{ id });
}
}
SpringMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.javainterviewpoint" />
<bean id="employeeDAOImpl" class="com.javainterviewpoint.EmployeeDAOImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- Database Configurations -->
<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/headway" />
<property name="username" value="root" />
<property name="password" value="toor" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/JSP/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
最佳答案
问题是action
属性的网址错误:
<form method="post" action="/SpringMVCTutorial/employee.html" commandName="employee">
根据控制器:
@RequestMapping(value = "/employee",method=RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("employee") Employee employee)
您的操作网址必须是以下内容:
<form method="post" action="employee" commandName="employee">
关于mysql - HTTP状态404-,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49850290/