Data Exchange between Controller and View in Spring Boot

Student.Java Model

package com.example.sunil.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    
    private Integer stdId;
    private String stdName;
    private Double stdFee;

}

Registration.java Controller

package com.example.sunil.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.sunil.model.Student;

@Controller
@RequestMapping("/reg")
public class Registration {
    
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public String show(Model model)
    {
        model.addAttribute("stdId", 500);
        model.addAttribute("stdName", "AJAY");
        model.addAttribute("stdFee", 300.2);
        return "index";
    }
    @GetMapping("/show1")
    public String showobj(Model model)
    {
        Student s=new Student();
        s.setStdId(500);
        s.setStdName("Sunil");
        s.setStdFee(500.00);
        model.addAttribute("stdobj", s);
        return "index";
    }
    
    @GetMapping("/show2")
    public String showobjlist(Model model)
    {
        
        
        List<Student> list = Arrays.asList(
                new Student(101, "A", 200.0),
                new Student(102, "B", 300.0),
                new Student(103, "C", 400.0),
                new Student(104, "D", 500.0)
            );
    
        model.addAttribute("list", list);
        return "index";
    }

}

Registrationform.java Controller

package com.example.sunil.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.sunil.model.Student;

@Controller
@RequestMapping("/regform")
public class Registrationform {
    
    @GetMapping("/register")
    public String showReg() {
        return "StudentRegister";
    }
    
    
    //2. Read Form Data
    @PostMapping("/save")
    public String saveData(
            @ModelAttribute Student student, //To Read Form Data exists in spring container to Container
            Model model                         //To read data from controller to View
            )
    {
        System.out.println(student);
        model.addAttribute("sob", student);
        return "StudentData";
    }

}

index.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Data is  with Hard Coded value: ${stdId}, ${stdName}, ${stdFee} <br/>

Data is  with Object: ${stdobj} <br/>${stdobj.stdId}, ${stdobj.stdName}, ${stdobj.stdFee}<br/>

Data is  with ObjectList : ${list} 

<table class="table table-hover">
    <tr class="bg-primary text-white">
        <th>ID</th>
        <th>NAME</th>
        <th>FEE</th>
        <th>OPERATIONS</th>
    </tr>
    <c:forEach items="${list}" var="ob">
        <tr>
            <td>${ob.stdId}</td>
            <td>${ob.stdName}</td>
            <td>${ob.stdFee}</td>
            <td>
                <a href="#" class="btn btn-danger">DELETE</a> |
                <a href="#" class="btn btn-success">EDIT</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

 

registra

 

<html>
<body>
<h2>Student Register Form</h2>
<form action="save" method="POST">
<pre>
ID   : <input type="text" name="stdId"/>
NAME : <input type="text" name="stdName"/>
FEE  : <input type="text" name="stdFee"/>
    <input type="submit" value="Add Student"/>
</pre>
</form>
</body>
</html>

 

<html>
<body>
DATA PAGE ${sob}
</body>
</html>