I try to do a java web application using:
SpringBoot
Mysql
JDBC
Design pattern: MVC, DAO
And Thymeleaf
I think i understood than:
I must create a class for every Entity and a DAO classe who use the annotation @Repository:
Diabetic.java(Entity):
public class Diabetic {
private int id_diabetic;
private int id_doctor;
private String name;
private String firstname;
private Date birthdate;
private String mail;
private String password;
private String phone;
private String emergencyContact;
private String address;
public Diabetic() {
}
public Diabetic(int id_diabetic, int id_doctor, String name, String firstname, Date birthdate,
String mail, String password, String phone, String emergencyContact, String address) {
this.id_diabetic = id_diabetic;
this.id_doctor = id_doctor;
this.name = name;
this.firstname = firstname;
this.birthdate = birthdate;
this.mail = mail;
this.password = password;
this.phone = phone;
this.emergencyContact = emergencyContact;
this.address = address;
}
public int getId_diabetic() {
return id_diabetic;
}
public void setId_diabetic(int id_diabetic) {
this.id_diabetic = id_diabetic;
}
public int getId_doctor() {
return id_doctor;
}
public void setId_doctor(int id_doctor) {
this.id_doctor = id_doctor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmergencyContact() {
return emergencyContact;
}
public void setEmergencyContact(String emergencyContact) {
this.emergencyContact = emergencyContact;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
DiabeticDAO.java:
@Repository
public class DiabeticDAO {
private JdbcTemplate jdbcTemplate;
private SimpleJdbcInsert simpleJdbcInsert;
@Autowired
public void setDataSource(DataSource dataSource){
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("diabetic")
.usingGeneratedKeyColumns("id_diabetic");
}
public Diabetic getDiabById(int id){
return jdbcTemplate.queryForObject("select * from Diabetic where id_diabetic=?", new DiabeticRowMapper(),id);
}
public List<Diabetic> getAllDiab(){
return jdbcTemplate.query("select * from Diabetic",new DiabeticRowMapper());
}
private final class DiabeticRowMapper implements RowMapper<Diabetic> {
@Override
public Diabetic mapRow(ResultSet rs, int rowNum) throws SQLException{
Diabetic diabetic = new Diabetic();
diabetic.setId_diabetic(rs.getInt("id_diabetic"));
diabetic.setName(rs.getString("name"));
diabetic.setFirstname(rs.getString("firstname"));
diabetic.setBirthdate(rs.getDate("birthdate"));
diabetic.setMail(rs.getString("mail"));
diabetic.setPassword(rs.getString("password"));
diabetic.setPhone(rs.getString("phone"));
diabetic.setEmergencyContact(rs.getString("emergencyContact"));
diabetic.setAddress(rs.getString("address"));
return diabetic;
}
}
}
And create a controller like this where I use the method from the DAO:
@Controller
public class MainController {
@GetMapping("/")
public String firstPage(){
return"firstPage";
}
@GetMapping("/authentification")
public String auth(){
DiabeticDAO diaDao = new DiabeticDAO();
List<Diabetic> listDia = diaDao.getAllDiab();
return"loggin";
}
What i don't understand:
- How to do a link in a view(html) to go to a controller ?
- How to send the data finded in the controller to a view for displaying them ?
Sorry if my questions seems strange but it's the first time i ask for help on here and english is not my first language
CodePudding user response:
You don´t have to link a controller in the view. The connection is handled trough the URL calls of the controller.
I´m taking examples from this introduction: https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
You can use several methods to add data to the view.
1. Option:
@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("messages", messageRepository.findAll());
return "message/list";
}
In this method we put "Model model" as a parameter to the method. You can now fill the model with some data.
2. Option:
@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView messages() {
ModelAndView mav = new ModelAndView("message/list");
mav.addObject("messages", messageRepository.findAll());
return mav;
}
In this method we create a ModelAndView object, setting the url which should be called and fill it with data. Again the data is called "messages". The return value is the created object.
Access data in view:
After you implemented one of the two options, you can access the data like this:
<tr th:each="message : ${messages}">
<td th:text="${message.id}">1</td>
<td><a href="#" th:text="${message.title}">Title ...</a></td>
<td th:text="${message.text}">Text ...</td>
</tr>
Let´s try to implement it into your current code:
@Controller
public class MainController {
@GetMapping("/")
public String firstPage(){
return"firstPage";
}
@GetMapping("/authentification")
public String auth(Model model){
DiabeticDAO diaDao = new DiabeticDAO();
List<Diabetic> listDia = diaDao.getAllDiab();
model.addAttribute("listdia", listDia);
return"loggin";
}
}
Now you can access the data in your view:
<tr th:each="dia: ${listdia}">
<td th:text="${dia.id_diabetic}">1</td>
<td><a href="#" th:text="${dia.name}">Name</a></td>
<td th:text="${dia.firstname}">Firstname</td>
</tr>