#java #spring #spring-mvc
#java #spring #spring-mvc
Вопрос:
я следую этому руководству https://www.codejava.net/frameworks/spring/spring-mvc-spring-data-jpa-hibernate-crud-example . Я успешно сохраняю данные в базу данных с помощью jpa, но это не отображается на index.jsp, индексная страница показывает обычные данные тегов html, но не извлекает данные из базы данных. Пожалуйста, загляните в мой код.
index.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<html>
<head>
</head>
<title>
Student Manager
</title>
<body>
<div align="center">
<h1>Student Manager</h1>
<form method="get" action="search">
<input type="text" name="keyword"/>
<input type="submit" name="search"/>
</form>
<h2><a href="${pageContext.request.contextPath}/new">Add new Student</a></h2>
<table border="1" cellpadding="5">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Qualification</th>
<th>Mobile</th>
<th>Action</th>
</tr>
<c:forEach items="${ModelList}" var="listStudent">
<tr>
<td>${listStudent.id} </td>
<td>${listStudent.name} </td>
<td>${listStudent.age} </td>
<td>${listStudent.qualification} </td>
<td>${listStudent.mobile} </td>
<td>
<a href="/edit?id=${listStudent.id}">Edit</a>
<a href="/delete?id=${listStudent.id}">Delete</a>
</td>
</tr></c:forEach>
</table>
</div>
</body>
</html>
контроллер
package com.uc.student;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentController {
@Autowired
private StudentService studentservice;
@RequestMapping("/")
public ModelAndView home() {
List<Student> list=studentservice.listAll();
ModelAndView model=new ModelAndView();
model.addObject("ModelList",list);
return model;
}
@RequestMapping("/new")
public String newStudentForm(Map<String,Object> model) {
Student student=new Student();
model.put("student",student);
return "new_student";
}
@RequestMapping(value="/save", method=RequestMethod.POST)
public String saveStudent(@ModelAttribute("student") Student student) {
studentservice.save(student);
return "redirect:/";
}
}
new_student.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add new Student</title>
</head>
<body>
<h2>Add New Student</h2>
<br>
<div align="center">
<form:form method="post" action="save" modelAttribute="student">
<table>
<tr><td>Name : <form:input path="name"/></td></tr>
<tr><td>Age : <form:input path="age"/></td></tr>
<tr><td>Qualification : <form:input path="qualification"/></td></tr>
<tr><td>Mobile : <form:input path="mobile"/></td></tr>
<tr><td><input type="submit" value="save"/></td></tr>
</table>
</form:form>
</div>
</body>
</html>
StudentService .java
package com.uc.student;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class StudentService {
@Autowired StudentRepository repo;
public void save(Student student) {
repo.save(student);
}
public List<Student> listAll(){
return (List<Student>)repo.findAll();
}
public Student getById(Long id) {
return repo.findById(id).get();
}
public void delete(Long id) {
repo.deleteById(id);
}
}