I want to create site. I have already configured login "/login" and registration "/registration" pages. The main task of this site is to show schedule for every student.
Now I need to display list of subjects on page "/schedule", using HTML-table, according to fields GROUP and COURSE which where picked by student during registration.
I have 14 different tables with list of subjects(1 table - one group of students).
Do I need to create #Entity class and Repository for every table?
How to show information for students using data from registration?
Controller of page "/schedule":
#Controller
public class ShedulePageController {
#GetMapping("/schedule")
public String schedulePage() {
return "SchedulePage";
}
}
Entity for 1 of 14 tables with list of subjects:
#Entity
#Table(name = "f_fit_c2g1")
public class f_fit_c2g1 {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_f_fit_c2g1;
#Column(length = 60)
private String monday;
#Column(length = 60)
private String tuesday;
#Column(length = 60)
private String wednesday;
#Column(length = 60)
private String thursday;
#Column(length = 60)
private String friday;
#Column(length = 60)
private String monday2;
#Column(length = 60)
private String tuesday2;
#Column(length = 60)
private String wednesday2;
#Column(length = 60)
private String thursday2;
#Column(length = 60)
private String friday2;
public String getMonday2() {
return monday2;
}
public void setMonday2(String monday2) {
this.monday2 = monday2;
}
public String getTuesday2() {
return tuesday2;
}
public void setTuesday2(String tuesday2) {
this.tuesday2 = tuesday2;
}
public String getWednesday2() {
return wednesday2;
}
public void setWednesday2(String wednesday2) {
this.wednesday2 = wednesday2;
}
public String getThursday2() {
return thursday2;
}
public void setThursday2(String thursday2) {
this.thursday2 = thursday2;
}
public String getFriday2() {
return friday2;
}
public void setFriday2(String friday2) {
this.friday2 = friday2;
}
public Long getId_c2g9() {
return id_f_fit_c2g1;
}
public void setId_c2g9(Long id_f_fit_c2g1) {
this.id_f_fit_c2g1 = id_f_fit_c2g1;
}
public String getMonday() {
return monday;
}
public void setMonday(String monday) {
this.monday = monday;
}
public String getTuesday() {
return tuesday;
}
public void setTuesday(String tuesday) {
this.tuesday = tuesday;
}
public String getWednesday() {
return wednesday;
}
public void setWednesday(String wednesday) {
this.wednesday = wednesday;
}
public String getThursday() {
return thursday;
}
public void setThursday(String thursday) {
this.thursday = thursday;
}
public String getFriday() {
return friday;
}
public void setFriday(String friday) {
this.friday = friday;
}
}
Every table with subjects has name like this - "f_fit_c#g#" where # - number.
Rerository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface f_fit_c2g1Repository extends JpaRepository<f_fit_c2g1, Long> {
}
My HTML file of "/schedule" page(with random static data):
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Table 07</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" th:href="#{/css/style.css}" />
</head>
<body>
<header>
<div class="small" id="header-scroll">
<h1>KNUTE</h1>
<nav>
<ul>
<li>Расписание</li>
<li>Домашнее задание</li>
<li>Профиль</li>
<li>Полезные ссылки</li>
</ul>
</nav>
</div>
</header>
<section class="ftco-section">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6 text-center mb-5">
<h2 class="heading-section">Расписание на первую неделю</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-dark table-hover">
<thead>
<tr style="text-align: center; width: 20px">
<th>#</th>
<th>Время</th>
<th>Понедельник</th>
<th>Вторник</th>
<th>Среда</th>
<th>Четверг</th>
<th>Пятница</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>8:20 - 9:40</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">2</th>
<td>10:05 - 11:25</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">3</th>
<td>12:05 - 13:25</td>
<td>the Bird</td>
<td>larrybird#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">4</th>
<td>13:50 - 15:10</td>
<td>Doe</td>
<td>johndoe#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">5</th>
<td>15:25 - 16:45</td>
<td>Bird</td>
<td>garybird#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">6</th>
<td>17:00 - 18:20</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">7</th>
<td>18:45 - 20:05</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-6 text-center mb-5">
<h2 class="heading-section" style="padding-top: 65px">Расписание на вторую неделю</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-dark table-hover">
<thead>
<tr style="text-align: center; width: 20px">
<th>#</th>
<th>Время</th>
<th>Понедельник</th>
<th>Вторник</th>
<th>Среда</th>
<th>Четверг</th>
<th>Пятница</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>8:20 - 9:40</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">2</th>
<td>10:05 - 11:25</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">3</th>
<td>12:05 - 13:25</td>
<td>the Bird</td>
<td>larrybird#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">4</th>
<td>13:50 - 15:10</td>
<td>Doe</td>
<td>johndoe#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">5</th>
<td>15:25 - 16:45</td>
<td>Bird</td>
<td>garybird#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">6</th>
<td>17:00 - 18:20</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
<tr>
<th scope="row">7</th>
<td>18:45 - 20:05</td>
<td>Otto</td>
<td>markotto#email.com</td>
<td>Thornton</td>
<td>jacobthornton#email.com</td>
<td>Thornton</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
</body>
</html>
I found a lot of information on similar topics, but I can't find information about working with multiple tables.
To quickly answer your questions,
1. Do I need to create #Entity class and Repository for every table?
Yes you need to create entity and repository for each table in the db.
2. How to show information for students using data from registration?
You will need to fetch the info from the table storing the student details, build a DTO (Data Transfer Object) with the schedule details and all and send it to thymeleaf.
I think you will have to redesign your database itself. The design feels kinda odd.
A better design would be something like this:
Related
I am new to spring and I need to be able to delete the selected rows by checkbox. How can I delete multiple rows from the database? I am using Thymeleaf as view.
Here is my code:
HTML
<div class="col-md-12">
<table class="table" id="tableImport">
<thead>
<tr>
<th scope="col">Remove</th>
<th scope="col">Debt Age Rule</th>
<th scope="col">Reminder</th>
<th scope="col">Frequency</th>
<th scope="col">Reorder</th>
</tr>
</thead>
<tbody>
<tr th:each="configCampaign:${listConfigCampaigns}">
<td>
<input type="checkbox" name="my-checkbox">
</td>
<td th:text="${configCampaign.debtagerule}"></td>
<td th:text="${configCampaign.remindebttype}"></td>
<td th:text="'Every '+${configCampaign.every} + ' ' + ${configCampaign.unit}"></td>
<td></td>
</tr>
</tbody>
</table>
<div class="col-md-12" style="text-align: center">
<input class="btn btn-primary" type="button" value="- Remove Selected Action(s)"/>
</div>
</div>
This table shows me data from an arrayList in memory, nothing with a database, I need to remove those selected objects from the array. At the moment I have my controller like this
Entity
private int configid;
private String debtagerule;
private String remindebttype;
private int every;
private String unit;
private boolean selected;
//getters and setters
In addition to the above, this is the controller with which I am currently working
Controller
#GetMapping("/deleteConfigureCampaign")
public String deleteConfig(#ModelAttribute ConfigCampaign configCampaign, Model model) {
listConfigCampaigns.remove(configCampaign);
return "redirect:/configureCampaign";
}
In Spring Boot, You have to use JpaRepository<> for delete data from database and need to understand structure of Spring Boot project.
Here is sturcture of Spring Boot project:
Entity -> Repository -> Service -> Controller -> View.
Here down is code:
Entity
#Table(name = "config_master")
public class ConfigCampaign {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer configid;
private String debtagerule;
private String remindebttype;
private Integer every;
private String unit;
private boolean selected;
// Constructor, Getter and Setter
}
Repository
Use of #Modifying annotation: It is used to enhance the #Query annotation so that we can execute not only SELECT queries, but also INSERT, UPDATE, DELETE, and even DDL queries
#Repository
public interface ConfigRepo extends JpaRepository<ConfigCampaign, Integer>{
#Modifying
#Transactional
#Query(nativeQuery = true, value = "DELETE FROM config_master WHERE configid IN(?1)")
void delConfig(List<Integer> configId);
}
Service
#Service
public class PojoServiceImpl{
#Autowired
private ConfigRepo configRepo;
#Override
public void delConfig(Integer[] configId) {
configRepo.delConfig(Arrays.asList(configId));
}
}
Controller
// Show table of Config Campaign
#RequestMapping(value = "/showconfig", method = RequestMethod.GET)
public String getConfig(Model mdl)
{
List<ConfigCampaign> getConfig = pojoService.getAllConfig();
mdl.addAttribute("config", getConfig);
return "showConfigCampaign";
}
// Delete item from Config Campaign
#RequestMapping(value = "/delcampaign", method = RequestMethod.GET)
public String deleteConfig(#RequestParam("cid") Integer[] configId)
{
pojoService.delConfig(configId);
return "redirect:/showconfig";
}
showConfigCampaign
You have to add configData.configid in checkbox.
<form th:action="#{/delcampaign}" th:object="${sconfig}">
<div class="container">
<div class="row">
<div class="col-sm-12">
<table class="table" style="text-align: center">
<thead>
<tr>
<th scope="col">Remove</th>
<th scope="col">Debt Age Rule</th>
<th scope="col">Reminder</th>
<th scope="col">Frequency</th>
</tr>
</thead>
<tbody>
<tr th:each="configData: ${config}">
<td><input type="checkbox" name="cid" th:value="${configData.configid}"/></td>
<td th:text="${configData.debtagerule}"></td>
<td th:text="${configData.remindebttype}"></td>
<td th:text="${configData.every}"></td>
</tr>
</tbody>
</table>
<input type="submit" value="Delete Users" />
</div>
</div>
</div>
</form>
Spring boot using thymeleaf and jpa
I have a reservation application that stores tickets in h2 database using jpa. When someone booked a seat, the checkbox should be disabled for next people, because for now you can reserve the seat over and over again - user should not be able to click the checkbox which is already reservated by other users. How to solve this problem? I can not get through this
reservation-seat.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Movies</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container my-2">
<div class="card">
<div class="card-body">
<div class="container my-5">
<h1 th:text="${movieName}"> Movie Name</h1>
<form th:action="#{'/reservation/save/' + ${repertoireId}}" th:object="${seatInfo}" method="post">
<div class="seatStructure">
<center>
<table id="seatsBlock">
<p id="notification"></p>
<tr>
<td colspan="14">
<div class="screen">SCREEN</div>
</td>
<br/>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td></td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>A</td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A1"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A2"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A3"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A4"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A5"></td>
<td class="seatGap"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A6"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A7"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A8"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A9"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A10"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A11"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A12"></td>
</tr>
</table>
<input type = "hidden" th:value="${movieName}">
<input type = "hidden" th:value="${repertoireId}">
</br>
<button type="submit">Order.</button>
</center>
</div>
</form>
<br/><br/>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
ReservationController.java
#Controller
// #Transactional
public class ReservationController {
TicketRepo ticketRepo;
ReservationRepo reservationRepo;
AppUserRepo appUserRepo;
MovieRepo movieRepo;
RepertoireRepo repertoireRepo;
#Autowired
public ReservationController(TicketRepo ticketRepo, ReservationRepo reservationRepo, AppUserRepo appUserRepo,
MovieRepo movieRepo, RepertoireRepo repertoireRepo) {
this.ticketRepo = ticketRepo;
this.reservationRepo = reservationRepo;
this.appUserRepo = appUserRepo;
this.movieRepo = movieRepo;
this.repertoireRepo = repertoireRepo;
}
#GetMapping("/movies/{movieName}/reservation")
public String reservationPage(Model model, #PathVariable ("movieName") String movieName) {
Movie movie = movieRepo.findByTitle(movieName);
List<Repertoire> repertoires = repertoireRepo.findByMovieId(movie.getId());
// model.addAttribute("seat", new SeatReservation());
// model.addAttribute("movieName", movirepertoireseName);
model.addAttribute("repertoires", repertoires);
return "reservation";
}
#GetMapping("/movies/{movieName}/reservation/{repertoireId}")
public String reservationSeatPage(Model model, #PathVariable("movieName") String movieName,
#PathVariable("repertoireId") Long repertoireId) {
Testing testing1 = new Testing();
testing1.setSeatReservation(new SeatReservation());
model.addAttribute("seatInfo", testing1);
model.addAttribute("movieName", movieName);
model.addAttribute("repertoireId", repertoireId);
return "reservation-seat";
}
#PostMapping("/reservation/save/{repertoireId}")
public String reserve(#ModelAttribute ("seatInfo") Testing testing, Principal principal,
#ModelAttribute("repertoireId") Long repertoireId) {
UUID uuid = UUID.randomUUID();
Ticket ticket = new Ticket();
ticket.setSeat(testing.getSeatReservation().getSeat());
ticket.setPrice(20);
ticket.setUuid(uuid);
ticketRepo.save(ticket);
Reservation reservation = new Reservation();
reservation.setTicket(ticketRepo.findByUuid(uuid).get());
Repertoire repertoire = repertoireRepo.findById(repertoireId).get();
reservation.setMovie(movieRepo.findByTitle(repertoire.getMovie().getTitle()));
reservation.setRepertoire(repertoire);
reservation.setAppUser(appUserRepo.findByUsername(principal.getName()));
reservationRepo.save(reservation);
return "redirect:/movies/list";
}
}
SeatReservation.java
import lombok.Data;
#Data
public class SeatReservation {
private String seat;
private boolean active;
public boolean isActive() {
return active;
}
}
Testing.java
import lombok.Data;
#Data
public class Testing {
private SeatReservation seatReservation;
private Long id;
private String string;
private boolean active;
public boolean isActive() {
return active;
}
}
Ticket.java
import lombok.Data;
import javax.persistence.*;
import java.util.UUID;
#Data
#Entity
public class Ticket {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id")
private Long id;
private UUID uuid;
private String seat;
private Integer price;
public Ticket() {
}
}
You should be using th:checked for checking/unchecking and th:disabled for enabling/disabling in Thymeleaf where you should evaluate a logical expression inside of these attributes. A simple example regarding your case is as follows:
<td><input th:field="*{seatReservation.seat}" type="checkbox" th:checked="${seatReservation.isActive}" th:disabled="${seatReservation.isActive}" class="seats" value="A6"></td>
Regards
I have a reservation application that stores tickets in h2 database using jpa. When someone booked a seat, the checkbox should be inactive/disabled for next people, because for now you can reserve the seat over and over again - user should see that some seats are already reservated.. How to solve this problem? Do I have to add something at in reservation-seat.html?
reservation-seat.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Movies</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container my-2">
<div class="card">
<div class="card-body">
<div class="container my-5">
<h1 th:text="${movieName}"> Movie Name</h1>
<form th:action="#{'/reservation/save/' + ${repertoireId}}" th:object="${seatInfo}" method="post">
<div class="seatStructure">
<center>
<table id="seatsBlock">
<p id="notification"></p>
<tr>
<td colspan="14">
<div class="screen">SCREEN</div>
</td>
<br/>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td></td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>A</td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A1"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A2"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A3"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A4"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A5"></td>
<td class="seatGap"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A6"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A7"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A8"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A9"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A10"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A11"></td>
<td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A12"></td>
</tr>
</table>
<input type = "hidden" th:value="${movieName}">
<input type = "hidden" th:value="${repertoireId}">
</br>
<button type="submit">Order.</button>
</center>
</div>
</form>
<br/><br/>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
ReservationController.java
#Controller
// #Transactional
public class ReservationController {
TicketRepo ticketRepo;
ReservationRepo reservationRepo;
AppUserRepo appUserRepo;
MovieRepo movieRepo;
RepertoireRepo repertoireRepo;
#Autowired
public ReservationController(TicketRepo ticketRepo, ReservationRepo reservationRepo, AppUserRepo appUserRepo,
MovieRepo movieRepo, RepertoireRepo repertoireRepo) {
this.ticketRepo = ticketRepo;
this.reservationRepo = reservationRepo;
this.appUserRepo = appUserRepo;
this.movieRepo = movieRepo;
this.repertoireRepo = repertoireRepo;
}
#GetMapping("/movies/{movieName}/reservation")
public String reservationPage(Model model, #PathVariable ("movieName") String movieName) {
Movie movie = movieRepo.findByTitle(movieName);
List<Repertoire> repertoires = repertoireRepo.findByMovieId(movie.getId());
// model.addAttribute("seat", new SeatReservation());
// model.addAttribute("movieName", movirepertoireseName);
model.addAttribute("repertoires", repertoires);
return "reservation";
}
#GetMapping("/movies/{movieName}/reservation/{repertoireId}")
public String reservationSeatPage(Model model, #PathVariable("movieName") String movieName,
#PathVariable("repertoireId") Long repertoireId) {
Testing testing1 = new Testing();
testing1.setSeatReservation(new SeatReservation());
model.addAttribute("seatInfo", testing1);
model.addAttribute("movieName", movieName);
model.addAttribute("repertoireId", repertoireId);
return "reservation-seat";
}
#PostMapping("/reservation/save/{repertoireId}")
public String reserve(#ModelAttribute ("seatInfo") Testing testing, Principal principal,
#ModelAttribute("repertoireId") Long repertoireId) {
UUID uuid = UUID.randomUUID();
Ticket ticket = new Ticket();
ticket.setSeat(testing.getSeatReservation().getSeat());
ticket.setPrice(20);
ticket.setUuid(uuid);
ticketRepo.save(ticket);
Reservation reservation = new Reservation();
reservation.setTicket(ticketRepo.findByUuid(uuid).get());
Repertoire repertoire = repertoireRepo.findById(repertoireId).get();
reservation.setMovie(movieRepo.findByTitle(repertoire.getMovie().getTitle()));
reservation.setRepertoire(repertoire);
reservation.setAppUser(appUserRepo.findByUsername(principal.getName()));
reservationRepo.save(reservation);
return "redirect:/movies/list";
}
}
SeatReservation.java
import lombok.Data;
#Data
public class SeatReservation {
private String seat;
private boolean active;
public boolean isActive() {
return active;
}
}
Testing.java
import lombok.Data;
#Data
public class Testing {
private SeatReservation seatReservation;
private Long id;
private String string;
private boolean active;
public boolean isActive() {
return active;
}
}
Ticket.java
import lombok.Data;
import javax.persistence.*;
import java.util.UUID;
#Data
#Entity
public class Ticket {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
#Column(name = "id")
private Long id;
private UUID uuid;
private String seat;
private Integer price;
public Ticket() {
}
}
I have worked on something similar to this so will post my approach.
I had used ajax to submit to post the seat booking info and if the response is successful then I will make the clicked checkbox/checkboxes disable.
As you said in comment section that you want to achieve this without javascript then go for below approach.
Every time you submit the form, save the occupied seats map (seat id key, status value) in model attribute and then iterate through the map by checking the seat id and status (active/inactive) and make rendered checkbox disabled/ not disabled based on the flag.
This is my Servlet class
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
#WebServlet("/Register")
public class Register extends HttpServlet {
static int id=0;
private static final long serialVersionUID = 1L;
private static final ArrayList<Employee> EMPLOYEES = new ArrayList<>();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
id++;
int uage=0;
long mob=0;
String uexit = request.getParameter("uexit");
if(uexit.equals("Exit")) {
System.out.println("\n\n\n\n\n\n\n\n");
request.getRequestDispatcher("exit.jsp").forward(request, response);
}
else {
String uname = request.getParameter("uname");
uage = Integer.parseInt(request.getParameter("uage"));
mob = Long.parseLong(request.getParameter("umob"));
if(uname!=null && uage!=0 && mob!=0) {
Employee employee = new Employee(id, uname, uage, mob);
EMPLOYEES.add(employee);
request.getSession().setAttribute("employees" , EMPLOYEES);
RequestDispatcher rd = getServletContext().getRequestDispatcher("register.jsp");
rd.forward(request, response);
}
else {
request.getSession().setAttribute("employees", null);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
for(int i=0; i<EMPLOYEES.size(); i++) {
System.out.print(EMPLOYEES.get(i).id + "\t");
System.out.print(EMPLOYEES.get(i).name + "\t");
System.out.print(EMPLOYEES.get(i).age + "\t");
System.out.print(EMPLOYEES.get(i).mob + "\t");
System.out.println();
}
}
This is my register.jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%#page import="java.util.*"%>
<!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>MyProject</title>
</head>
<h2 align="center">
<font color="#BF360C" size="10" face="Times New Roman"><b><i>
Register</i></b></font>
</h2>
<body style="background-color: #AED6F1">
<form action="Register" method="POST">
<table align="center" cellpadding="20" width="600"
style="background-color: #33C3DD;">
<tr>
<th><font size="5" face="Times New Roman"><b><i>Name:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uname"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Age:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uage"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Mobile
Number:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="umob"></td>
</tr>
<tr>
<th colspan="3" align="center"><input type="submit"
name="uexit" value="Register"><br /> <br /> <input
type="submit" name="uexit" value="Exit"></th>
</tr>
</table>
</form>
<br>
<br>
<table cellpadding="20" cellspacing="5" width="900"
style="background-color: #FDFEFE;" align="center">
<tr style="background-color: #FDFDFD">
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Sr. No.</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Name</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Age</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Mobile Number:</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Edit</i></b></font></th>
</tr>
<c:forEach items="${employees}" var="item">
<tr>
<td style="background-color: #4468D1"><c:out value = "${item.id}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.name}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.age}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.mob}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
This is my Entity class
public class Employee {
public int id;
public String name;
public int age;
public long mob;
public Employee() {}
public Employee(int id, String name, int age, long mob) {
this.name= name;
this.age= age;
this.mob= mob;
this.id=id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=age;
}
public long getMob() {
return mob;
}
public void setMob(int mob) {
this.mob = mob;
}
}
I want to pass list EMPLOYEES from the servlet to jsp page abd want to show each Employee object with <c:forEach> but it keeps on saying exception. so please anyone answer me.
Also tell me which jstl standard jar to download if i m using tomcat 8.5
i have jstl-jar 1.2 but i dont think it's working because this error is keeps on coming no matter how i do it.
Try to use
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
instead of
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
in your jsp code
Seems you are doing something wrong here
<tr>
<td>${item}</td>
</tr>
According to your code you are getting list of object in item . How you are printing a list which containing object ?
Try using
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.age}</td>
<td>${item.mob}</td>
</tr>
I'm trying to put two objects in my JSP view but while getting the value by getter is working fine and while referring the value by property of the object throws error.
JSP Code Snipet is:
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr><h2 class="intro-text text-center">Manage Subject</h2><hr>
</div>
<div class="col-md-12">
<c:url var="addAction" value="/data-entry/subject/add" ></c:url>
<form:form action="${addAction}" commandName="subject">
<table>
<c:if test="${!empty subject.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="description">
<spring:message text="Description"/>
</form:label>
</td>
<td>
<form:input path="description" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty subject.name}">
<input type="submit"
value="<spring:message text="Edit Subject"/>" />
</c:if>
<c:if test="${empty subject.name}">
<input type="submit"
value="<spring:message text="Add Subject"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Subject List</h3>
<c:if test="${!empty subjectlist}">
<table class="tg">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${subjectlist}" var="subject">
<tr>
<td>${subject.getID()}</td>
<td>${subject.getName()}</td>
<td>${subject.getDescription()}</td>
<td><a href="<c:url value='/admin/subject/edit/${subject.getID()}' />" >Edit</a></td>
<td><a href="<c:url value='/admin/subject/remove/${subject.getID()}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</div>
</div>
</div>
Model Class:
package com.spring.schoolmanagement.model;
public class Subject {
private int id;
private String name;
private String description;
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setDescription( String description ) {
this.description = description;
}
public String getDescription() {
return this.description;
}
#Override
public String toString(){
return "{ID=" + id + ",Name=" + name + ",Description=" + description + "}";
}
}
Controller Class:
package com.spring.schoolmangement;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.schoolmanagement.dao.SubjectDAOImpl;
import com.spring.schoolmanagement.model.Subject;
/**
* Handles requests for admin specific pages.
*/
#Controller
public class AdminController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
#Autowired
private SubjectDAOImpl subjectService;
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping(value = {"/admin/", "/admin"}, method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "admin-dash-board";
}
#RequestMapping(value = "/admin/student")
public String student(Model model) {
return "admin-student";
}
#RequestMapping(value = "/admin/subject")
public String displaySubjects(Model model) {
model.addAttribute("subject", new Subject());
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
#RequestMapping(value = "/admin/subject/edit/{id}")
public String course(#PathVariable("id") int id, Model model) {
model.addAttribute("subject", this.subjectService.getById(id));
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
}
Please suggest where I'm making the mistake.
Change your getter/setter method from:
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
To
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
Your Bean is missing naming convention and should follow JavaBean (read pdf section 8.8) naming convention.