Not able to display List of objects in JSP page based on the given input using Spring MVC JPA using Spring Boot

Viewed 64

I am trying to display List of Student objects based on their Course on JSP page using Spring MVC and JPA using Spring Boot

I am using the JSP page searchByCourse.jsp page as shown below and on the JSP page, the list of student objects based on the given course is not displayed. And also the methods in the repository file StudentRepo.java are not showing any output as well. I have marked them in the comments in the StudentController.java.

I am not able to understand where I made the mistake. Your help would be really appreciated.

Below are the files I am using.

Student.java

package com.springcourse.springmvcboot.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student 
{
    @Id
    private int studID;
    private String studName;
    private String studCourse;

    public String getStudCourse() {
        return studCourse;
    }

    public void setStudCourse(String studCourse) {
        this.studCourse = studCourse;
    }

    public int getStudID() {
        return studID;
    }
    
    public void setStudID(int studID) {
        this.studID = studID;
    }
    
    public String getStudName() {
        return studName;
    }
    
    public void setStudName(String studName) {
        this.studName = studName;
    }

    @Override
    public String toString() {
        return "Student [studID=" + studID + ", studName=" + studName + ", studCourse=" + studCourse + "]";
    }
        
}

StudentController.java

package com.springcourse.springmvcboot;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.springcourse.springmvcboot.dao.StudentRepo;
import com.springcourse.springmvcboot.model.Student;

@Controller
public class StudentController 
{
    @Autowired
    StudentRepo studrepo;
    
    @RequestMapping("/")
    public String home()
    {
        return "home.jsp";
    } 
    
    @RequestMapping("/addStudent")
    public String addStudent(Student stud)
    {
        studrepo.save(stud);
        return "home.jsp";
    }
    
    @RequestMapping("/getStudent")
    public ModelAndView getStudent(@RequestParam int studID)
    {
        ModelAndView mv = new ModelAndView("showStudent.jsp");
        Student student = studrepo.findById(studID).orElse(new Student());
        mv.addObject(student);
        
        return mv;
    }
    
    @RequestMapping("/deleteStudent")
    public ModelAndView deleteStudent(@RequestParam int studID)
    {
        ModelAndView mv = new ModelAndView("deleteStudent.jsp");
        Student student = studrepo.findById(studID).orElse(new Student());
        studrepo.deleteById(studID);
        mv.addObject(student);
        
        return mv;
    }
    
    @RequestMapping("/deleteAllStudent")
    public ModelAndView deleteAllStudent()
    {
        ModelAndView mv = new ModelAndView("deleteAllStudent.jsp");
        studrepo.deleteAll();
        
        return mv;
    }
    
    @RequestMapping("/searchByCourse")
    public ModelAndView searchByCourse(@RequestParam String studCourse)
    {
        ModelAndView mv = new ModelAndView("searchByCourse.jsp");
        List<Student> studList = studrepo.findByStudCourse(studCourse);
        
        mv.addObject("stlist", studList);
        
        System.out.println(studrepo.findByStudCourse("Java"));//Not displaying any output
        System.out.println(studrepo.findByStudIDLessThan(105));
        System.out.println(studrepo.findByStudCourseSorted("Java"));//Not displaying any output
        
        return mv;
    }
}

StudentRepo.java

package com.springcourse.springmvcboot.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.springcourse.springmvcboot.model.Student;

public interface StudentRepo extends CrudRepository<Student, Integer>
{
    List<Student> findByStudCourse(String studCourse);
    
    List<Student> findByStudIDLessThan(int studID);
    
    
    @Query("from Student where studCourse=?1 order by studName")
    List<Student> findByStudCourseSorted(String studCourse);
}

The JSP file used to display

searchByCourse.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 prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html>

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

The List of Students are <br>

<c:forEach items="${stlist}" var="st">
        <li>${st}</li>
</c:forEach>

</body>
</html>

And finally the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcourse</groupId>
    <artifactId>springmvcjpaboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvcjpaboot</name>
    <description>Demo project for Spring MVC with Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.39</version>
</dependency>

<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
0 Answers
Related