In this, article we are going to implement search functionality in spring boot, Hibernate, and JPA with JSP, HTML, and MYSQL. For example, we are going to use data from the train list, where will add two inputs fromLocation and toLocation and a search button.
When the user will enter the city names and click on to search button the train list data will be filtered according to the input values.
Steps to implement search in spring boot and JPA with MYSQL
- Create DTO Class to transfer the data from one state to another state.
- Create a form Class to bind the HTML elements.
- Create DAO class.
- Add a search method to the service layer.
- Create a post API into Controller.
- Design search functionality into the JSP page
Create DTO Class
TrainDTO.java is a DTO or entity that contains the definition of a database table, in this example, it’s going use the table name “train” that contains the information of the trains. also, it contains attributes of the Train entity with getters and setters that will help to transfer data from one state to another state.
package com.trainbooking.dto; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; @Entity @Table(name="train") @Getter @Setter public class TrainDTO extends BaseDTO{ @Column(name = "trainName", length = 755) private String trainName; @Column(name = "trainNumber", length = 755) private String trainNumber; @Column(name = "fromLocation", length = 755) private String fromLocation; @Column(name = "toLocation", length = 755) private String toLocation; @Column(name = "ticketPrice", length = 755) private long ticketPrice; @Column(name = "numberOfSeats", length = 755) private long numberOfSeats; }
Base DTO:
package com.trainbooking.dto; import java.sql.Timestamp; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import lombok.Getter; import lombok.Setter; @MappedSuperclass @Getter @Setter public class BaseDTO { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "Id") protected long id; }
Create a form Class
In TrainForm.java, let’s define the required attribute of the Train entity and populate the data into a DTO.
package com.trainbooking.form; import javax.validation.constraints.NotEmpty; import com.trainbooking.dto.BaseDTO; import com.trainbooking.dto.TrainDTO; import com.trainbooking.utility.DataUtility; import lombok.Getter; import lombok.Setter; @Getter @Setter public class TrainForm extends BaseDTO { @NotEmpty(message = "Train name is required") private String trainName; @NotEmpty(message = "Train Number is required") private String trainNumber; @NotEmpty(message = "From Location is required") private String fromLocation; @NotEmpty(message = "To Location is required") private String toLocation; @NotEmpty(message = "Ticket Price is required") private String ticketPrice; @NotEmpty(message = "Number Of seats is required") private String numberOfSeats; public TrainDTO getDTO() { TrainDTO bean=new TrainDTO(); bean.setId(id); bean.setTrainName(trainName); bean.setTrainNumber(trainNumber); bean.setFromLocation(fromLocation); bean.setToLocation(toLocation); bean.setTicketPrice(DataUtility.getLong(ticketPrice)); bean.setNumberOfSeats(DataUtility.getLong(numberOfSeats)); return bean; } public void populate(TrainDTO bean) { id = bean.getId(); trainName=bean.getTrainName(); trainNumber = bean.getTrainNumber(); fromLocation = bean.getFromLocation(); toLocation = bean.getToLocation(); ticketPrice = DataUtility.getStringData(bean.getTicketPrice()); numberOfSeats = DataUtility.getStringData(bean.getNumberOfSeats()); } }
Create DAO class
package com.trainbooking.dao; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.trainbooking.dto.TrainDTO; public interface TrainDAO extends JpaRepository<TrainDTO, Long>{ public TrainDTO findById(long id); public TrainDTO findByTrainNumber(String trainNumber); public List<TrainDTO> findByFromLocationAndToLocation(String fromLocation, String toLocation); }
Define search method in service layer
public List<TrainDTO> searchTrain(TrainDTO dto){ List<TrainDTO> bean = dao.findByFromLocationAndToLocation(dto.getFromLocation(), dto.getToLocation()); return bean; }
Create API into the controller to search Train
@PostMapping("/searchTrain") public String SearchTrain(@ModelAttribute("form")TrainForm form, Model model, @RequestParam("id") long id, HttpServletRequest request ) throws Exception{ String operation = request.getParameter("operation"); List<TrainDTO> list = null; if(operation.equals("Search")) { list = service.searchTrain(form.getDTO()); }else { list = service.list(); } model.addAttribute("list", list); return "trainlist"; }
Design search functionality on JSP
<div> <sf:form method="post" action="${pageContext.request.contextPath}/searchTrain" modelAttribute="form"> <sf:input type="hidden" id="id" class="form-control form-control-lg" path="id" name="id" value="${form.id}"/> <div class="row"> <div class="col-md-8 mb-4 d-flex"> <div class="form-outline"> <s:bind path="fromLocation"> <font color="red" style="font-size: 15px"><sf:errors path="${status.expression}" /></font> <sf:input type="text" id="fromLocation" class="form-control" path="fromLocation" placeholder="From Location" name="fromLocation" style="width: 300px;"/> </s:bind> </div> <div class="form-outline" style="margin-left: 10px"> <s:bind path="toLocation"> <font color="red" style="font-size: 15px"><sf:errors path="${status.expression}" /></font> <sf:input type="text" id="toLocation" class="form-control" path="toLocation" placeholder="To Location" name="toLocation" style="width: 300px;"/> </s:bind> </div> <input class="btn btn-primary" type="submit" value="Search" name="operation" style="margin-left: 10px;"/> <input class="btn btn-primary" type="submit" value="Reset" name="operation" style="margin-left: 10px;"/> </div> </div> </sf:form> </div>
Search in jsp
<sf:form method="post" action="${pageContext.request.contextPath}/searchMovie" modelAttribute="form"> <sf:input type="hidden" id="id" class="form-control form-control-lg" path="id" name="id" value="${form.id}"/> <div class="row"> <div class="col-md-8 mb-4 d-flex"> <div class="form-outline"> <s:bind path="genre"> <font color="red" style="font-size: 15px"><sf:errors path="${status.expression}" /></font> <sf:input type="text" id="genre" class="form-control" path="genre" placeholder="Search by Genre" name="genre" style="width: 300px;"/> </s:bind> </div> <input class="btn btn-primary" type="submit" value="Search" name="operation" style="margin-left: 10px;"/> <input class="btn btn-primary" type="submit" value="Reset" name="operation" style="margin-left: 10px;"/> </div> </div> </sf:form>
Output: