![](/img/trans.png)
[英]How do I implement the versioning of the data in Spring Boot + Mysql?
[英]How do I get 2 radiobutton list which pass data to mysql db? Spring Boot
這個的主要概念是:我想要有 2 個單選按鈕列表。 一個與培訓師有關,另一個與培訓師的活動有關。 我有兩個問題:
如何獲得 2 個單選按鈕列表,其中包含從 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"; } } } }
歡迎.jsp
<form:form action="nextpage" modelAttribute = "trainers" method="POST"> <input type="radio" name="name" value="${trainers}"/> John Simson
我不知道如何從數據庫中檢索數據到單選按鈕列表。 我有四個教練。
我必須檢查第一個單選按鈕列表中的一個點和第二個單選按鈕列表中的一個點。 在我選擇了 2 個選項后,我將重定向到新的 JSP 頁面(如果選中個人培訓 -> 重定向到 personaltraining.jsp 等)
如何將數據傳遞到 mysql 表,例如我選擇了 John Simson 和 Personal Training,我將填寫 next.jsp 頁面 personaltraining.jsp,當我填寫並發送時,我想將其保存在 db 中。
首先將Spring Thymeleaf
添加到您的項目中。 (我認為Spring JPA
存在。)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
以后在Trainer
實體里有個id會更健康。 你也可以在這里看看。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
把所有教員帶起來的存儲庫和服務可以是這樣的。 (您還沒有進行私人搜索!)
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";
}
}
現在,如果您將以下代碼resources/templates' folder as
,您就可以從數據庫中看到培訓師。
<!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>
結果將如上圖所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.