如何将在控制器中找到的数据发送到视图?
How to send the data finded in the controller to a view?
我尝试做一个 java 网络应用程序使用:
SpringBoot
Mysql
JDBC
设计模式:MVC、DAO
还有百里香
我想我明白了:
我必须为每个实体创建一个 class 和一个使用注解 @Repository:
的 DAO classe
Diabetic.java(实体):
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;
}
}
}
然后创建一个这样的控制器,我使用 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";
}
不明白的地方:
- 如何在视图 (html) 中执行 link 以转到控制器?
- 如何将在控制器中找到的数据发送到视图以显示它们?
对不起,如果我的问题看起来很奇怪,但这是我第一次在这里寻求帮助,英语不是我的母语
您不必 link 视图中的控制器。
通过控制器的 URL 调用处理连接。
我从这篇介绍中举了一些例子:
https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
您可以使用多种方法将数据添加到视图。
1.选项:
@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("messages", messageRepository.findAll());
return "message/list";
}
在这个方法中,我们将“模型模型”作为方法的参数。您现在可以用一些数据填充模型。
2。选项:
@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView messages() {
ModelAndView mav = new ModelAndView("message/list");
mav.addObject("messages", messageRepository.findAll());
return mav;
}
在这个方法中,我们创建了一个 ModelAndView 对象,设置了应该调用的 url 并用数据填充它。数据再次被称为“消息”。 return值为创建的对象。
在视图中访问数据:
实施两个选项之一后,您可以像这样访问数据:
<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>
让我们尝试将其实现到您当前的代码中:
@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";
}
}
现在您可以访问视图中的数据了:
<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>
我尝试做一个 java 网络应用程序使用:
SpringBoot
Mysql
JDBC
设计模式:MVC、DAO
还有百里香
我想我明白了:
我必须为每个实体创建一个 class 和一个使用注解 @Repository:
的 DAO classe
Diabetic.java(实体):
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;
}
}
}
然后创建一个这样的控制器,我使用 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";
}
不明白的地方:
- 如何在视图 (html) 中执行 link 以转到控制器?
- 如何将在控制器中找到的数据发送到视图以显示它们?
对不起,如果我的问题看起来很奇怪,但这是我第一次在这里寻求帮助,英语不是我的母语
您不必 link 视图中的控制器。 通过控制器的 URL 调用处理连接。
我从这篇介绍中举了一些例子: https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
您可以使用多种方法将数据添加到视图。
1.选项:
@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("messages", messageRepository.findAll());
return "message/list";
}
在这个方法中,我们将“模型模型”作为方法的参数。您现在可以用一些数据填充模型。
2。选项:
@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView messages() {
ModelAndView mav = new ModelAndView("message/list");
mav.addObject("messages", messageRepository.findAll());
return mav;
}
在这个方法中,我们创建了一个 ModelAndView 对象,设置了应该调用的 url 并用数据填充它。数据再次被称为“消息”。 return值为创建的对象。
在视图中访问数据:
实施两个选项之一后,您可以像这样访问数据:
<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>
让我们尝试将其实现到您当前的代码中:
@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";
}
}
现在您可以访问视图中的数据了:
<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>