view不使用mysql-spring-mvc和jpa显示表中的数据

pb3s4cty  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(286)

我正在学习这个教程https://www.codejava.net/frameworks/spring/spring-mvc-spring-data-jpa-hibernate-crud-example . 我使用jpa成功地将数据保存到数据库中,但它没有显示在index.jsp上,索引页显示正常的html标记数据,但没有从数据库检索数据。请检查一下我的密码。
索引.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:/";
    }
}

新建\u 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>

学生服务.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);
}

}
pgky5nke

pgky5nke1#

@RequestMapping("/") 
    public ModelAndView home() {
        List<Student> list=studentservice.listAll();
        ModelAndView model=new ModelAndView("index");  <----- it should point to index.jsp
        model.addObject("ModelList",list);
        return model;
    }

相关问题