Issue
Main concept of this is: I want to have 2 radio button list. One with trainers and other with trainer's activities. I have two problems:
How can I get 2 radio button list with data retrieved from mysql db.
@Entity @Table(name = "trainers") public class Trainer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "name") private String name; @Column(name = "pt_done") private Integer personalTraining; @Column(name = "target") private Integer target; @Column(name = "sale") private Integer sale; @Column(name = "steps") private Integer steps; public Trainer() {} //+getters and setters }
public interface TrainerRepository extends JpaRepository < Trainer, Integer > { @Query(value = "select name from trainers", nativeQuery = true) List < Trainer > findByName() @Service public class TrainerServiceImplementation implements TrainerService { private TrainerRepository trainerRepository; @Override public List < Trainer > findByName() { return trainerRepository.findByName(); } @Controller @RequestMapping("/gym") public class TrainerController { @Autowired private TrainerRepository trainerRepository; @Autowired private TrainerService trainerService; @RequestMapping("/hello") public String viewHomePage(Model theModel) { theModel.addAttribute("trainers", trainerRepository.findByName()); return "welcome"; } } } }
welcome.jsp
<form:form action="nextpage" modelAttribute = "trainers" method="POST"> <input type="radio" name="name" value="${trainers}"/> John Simson
I have no idea how i can retrieve data from db to radiobutton list. I have four trainers.
I have to check one of points from first radio button list and one of points from second radio button list. And after I chose 2 options I will redirect to new JSP Page (If checked personal training -> redirect to personaltraining.jsp, etc.)
How can I pass data to mysql table, e.g. I chose John Simson and Personal Training, I will fill next jsp page personaltraining.jsp and when i fill and send, I want to save it in db.
Solution
First add Spring Thymeleaf
to your project. (I think Spring JPA
exists.)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Later, it would be healthier to have an id in the Trainer
entity. You can also have a look here.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
The repository and service that brings all instructors can be like this. (You haven't done a private search!)
public interface TrainerRepository extends JpaRepository<Trainer, Integer> {}
public interface TrainerService {
List<Trainer> findAll();
}
@Service
public class TrainerServiceImpl implements TrainerService {
private final TrainerRepository trainerRepository;
public TrainerServiceImpl(TrainerRepository trainerRepository) {
this.trainerRepository = trainerRepository;
}
@Override
public List<Trainer> findAll() {
return trainerRepository.findAll();
}
}
@Controller
@RequestMapping("/gym")
public class TrainerController {
private final TrainerService trainerService;
public TrainerController(TrainerService trainerService) {
this.trainerService = trainerService;
}
@GetMapping("/hello")
public String viewHomePage(Model model) {
model.addAttribute("trainers", trainerService.findAll());
return "welcome";
}
}
Now if you save the following code in the resources/templates' folder as
welcome.html`, you can see the trainers from the database.
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<div>
<form method="post">
<div>
<label>Trainers</label>
<div th:each="trainer : ${trainers}">
<div>
<input type="radio" name="trainer" th:value="${trainer.id}" th:text="${trainer.name}">
</div>
</div>
</div>
<button type="submit">Submit Form</button>
</form>
</div>
</body>
</html>
The result will be as in this picture.
Answered By - İsmail Y.