How to remove multiple rows via checkbox - java

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>

Related

SpringBoot + Thymeleaf + SQL, display HTML table from database

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:

How to disable checkbox when it is already used in database - Spring boot jpa

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

how to set the checkbox to inactive when it is already used in the database - thymeleaf spring boot

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.

How to use c:foreach to list a list of object inside another class.

One of my Object have a list of another object in it. how can i use to get the list of superperson out. Any Idea or help would be appreciated. Thanks
<table id="contactTable" class="table table-hover" >
<tr>
<th width="10%">Date</th>
<th width="10%">Supers</th>
<th width="50">Location Seen</th>
<th width="20%"></th>
<th width="30%"></th>
</tr>
<tr>
<td>
Date: <c:out value="${sighting.date}"/>
</td>
<td>
Super Person: <c:out value="${sighting.superPerson.name}"/>
</td>
<td>
Location: <c:out value="${sighting.location.locationName}"/>
</td>
</table>
//class
public class Sighting {
private int sightingId;
private String sighting;
private String description;
#DateTimeFormat(iso = ISO.DATE)
private LocalDate date;
private Location location;
private List<SuperPerson> superPerson = new ArrayList<>();
you can use jstl forEach tag
here
https://beginnersbook.com/2013/11/jstl-cforeach-and-cfortokens-core-tags/

CRUD in controller is not deleting row correctly

I was able to create values and store into my db, which displays a view of the list of values stored and additional values that is written into a input will automatically show in the table.
Controller
#Controller
public class AppPortController {
private ApServerService apServerService;
#Autowired
public void setApServerService(ApServerService apServerService) {
this.apServerService = apServerService;
}
#RequestMapping(value = "/editApServer", method = RequestMethod.GET)
public String list(Model model) {
model.addAttribute("apList", apServerService.listAllApServerModels());
return "editApServer";
}
#RequestMapping("editApServer/update/{id}")
public String update(#PathVariable String id, Model model) {
model.addAttribute("apList", apServerService.getApServerModelById(id));
return "editApServer";
}
#RequestMapping("editApServer/new")
public String newServer(Model model){
model.addAttribute("apServer", new ApServerModel());
return "editApServer";
}
#RequestMapping(value = "/addServer", method = RequestMethod.POST)
public String addServer(#ModelAttribute ApServerModel apServerModel) {
apServerService.saveApServerModel(apServerModel);
return "redirect:editApServer";
}
#RequestMapping("editApServer/delete")
public String delete(#PathVariable String host){
apServerService.deleteApServerModel(host);
return "redirect:editApServer";
}
Repository
public interface AppPortRepository extends CrudRepository<ApServerModel, String> {}
POJO
#Document(collection = "apDBServer")
public class ApServerModel {
#Id
private String id;
private String host;
private String port;
//getters and setters
HTML SNIPPET
<table>
<thead>
<tr>
<th> Host Name </th>
<th> Port Name</th>
<th>Id</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="ApServerModel : ${apList}">
<td th:text ="${ApServerModel.host}"></td>
<td th:text ="${ApServerModel.port}"></td>
<td th:text ="${ApServerModel.id}"></td>
<td><a th:href="${'editApServer/update/' + ApServerModel.id}">Edit</a></td>
<td><a th:href="${'editApServer/delete'}">Delete</a></td>
</tr>
</tbody>
</table>
<br />
<h2>Add AppPortServer</h2>
<form action="/addServer" method="POST">
Host <input type="text" id="host" name="host" /><br />
Port <input type="text" id="port" name="port" /><br />
<input type="submit" />
</form>
Problem
In my controller the delete would not execute(it is not doing what I want it to do). but line below it redirects me back to the same page.
What am I doing wrong? I have been savaging through the internet trying to find the crud functions for mongodb using springboot. Logically speaking why wouldn't my delete work if I follow the logic of the create?
I followed Tutorial, for posting to a table. Then I followed
Tutorial 2 That implements my delete and update. But It does not delete the values.

Categories