[英]Why is the information from my database (mysql) not showing on my html template? This is a springboot application using thymeleaf
This is the class that creates the entity in the mysql database:这是在 mysql 数据库中创建实体的 class:
package io.rhd.itdepartment.models;
import java.util.Base64;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* @author J
*
*/
@Entity
@Table(name="admin_user")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class AdminUser {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="user_image")
private byte[] userImage;
@Column(name="user_name")
private String name;
private String requestID;
private String deviceType;
private Date requestDate;
private String issueDetails;
private String status;
}
this is the Service class package io.rhd.itdepartment.services;这是服务 class package io.rhd.itdepartment.services;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.rhd.itdepartment.models.AdminUser;
import io.rhd.itdepartment.repositories.AdminUserRepository;导入 io.rhd.itdepartment.repositories.AdminUserRepository;
@Service
public class AdminUserService {
@Autowired
private AdminUserRepository adminUserRepository;
//return list of adUsers
public List<AdminUser> getEntries(){
//return adminUserRepository.findAll();
return (List<AdminUser>) adminUserRepository.findAll();
}
//save new adUser
public void save(AdminUser adUser) {
adminUserRepository.save(adUser);
}
//return adUser item by id
public Optional<AdminUser> findById(int id){
return adminUserRepository.findById(id);
}
public void delete(int id) {
// TODO Auto-generated method stub
adminUserRepository.deleteById(id);
}
} }
The controller class is as follows: controller class如下:
package io.rhd.itdepartment.controllers;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.rhd.itdepartment.models.AdminUser;
import io.rhd.itdepartment.services.AdminUserService;
@Controller
public class AdminUserController {
@Autowired
private AdminUserService adminUserService;
@GetMapping("/index")
public String goToEntriesPage(Model model) {
List<AdminUser> returnEntries = adminUserService.getEntries();
model.addAttribute("entries", returnEntries);
return "index"; //the html page to return
}
@PostMapping("/index/addNew")
public String addNew(AdminUser adUser) {
adminUserService.save(adUser);
return "redirect:/index";
}
@RequestMapping("/index/findById")
@ResponseBody//return on the modal popup page
public Optional<AdminUser> findById(int id) {
return adminUserService.findById(id);
}
//parse submitted modal form update to database
@RequestMapping(value="/index/update", method = {RequestMethod.PUT, RequestMethod.GET})
public String update(AdminUser country) {
adminUserService.save(country);
return "redirect:/index";
}
//delete country item
@RequestMapping(value="/index/delete", method = {RequestMethod.DELETE, RequestMethod.GET})
public String delete(Integer id) {
adminUserService.delete(id);
return "redirect:/index";
}
}
And finally, this is how is it parsed in the html template:最后,这是它在 html 模板中的解析方式:
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<div class="d-sm-flex align-items-center mb-4">
<h4 class="card-title mb-sm-0">Request Summary</h4>
<a href="#" class="text-dark ml-auto mb-3 mb-sm-0"> View all Requests</a>
</div>
<div class="table-responsive border rounded p-1">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addModal" data-whatever="@mdo">Add Entry</button>
<table class="table">
<thead>
<tr>
<th class="font-weight-bold">ID</th>
<th class="font-weight-bold">Personnel</th>
<th class="font-weight-bold">Request ID</th>
<th class="font-weight-bold">Device Type</th>
<th class="font-weight-bold">Request Date</th>
<th class="font-weight-bold">Issue Details</th>
<th class="font-weight-bold">Status</th>
<th class="font-weight-bold">Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="adminuser:${entries}">
<td th:text="${user.id}"></td>
<td th:text="${adminuser.userImage}"></td>
<td th:text="${adminuser.name}"></td>
<td th:text="${adminuser.requestID}"></td>
<td th:text="${adminuser.deviceType}"></td>
<td th:text="${adminuser.requestDate}"></td>
<td th:text="${adminuser.issueDetails}"></td>
<td th:text="${adminuser.status}"></td>
</tr>
</tbody>
</table>
</div><hr>
<div class="d-flex mt-4 flex-wrap">
<p class="text-muted">Showing 1 to 10 of - entries</p>
<nav class="ml-auto">
<ul class="pagination separated pagination-info">
<li class="page-item"><a href="#" class="page-link"><i class="icon-arrow-left"></i></a></li>
<li class="page-item active"><a href="#" class="page-link">1</a></li>
<li class="page-item"><a href="#" class="page-link">2</a></li>
<li class="page-item"><a href="#" class="page-link">3</a></li>
<li class="page-item"><a href="#" class="page-link">4</a></li>
<li class="page-item"><a href="#" class="page-link"><i class="icon-arrow-right"></i></a></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
</div>
When i run the above, no information displays in the columns on the html template.当我运行上述程序时,html 模板的列中没有显示任何信息。
What am I doing wrong?我究竟做错了什么?
Problem is iteration in your thymeleaf.问题是您的 thymeleaf 中的迭代。 Here down userEntries: ${entries}
attribute value you see means for each
element in the result of evaluating ${entries}
, repeat this fragment of template, using the current element in a variable called userEntries
.在此处向下userEntries: ${entries}
属性值意味着for each
元素${entries}
,重复此模板片段,使用名为userEntries
的变量中的当前元素。 Let's give a name each of the things we see:让我们为我们看到的每一个事物命名:
1. We will call ${entries}
the iterated expression or iterated variable. 1.我们将${entries}
称为迭代表达式或迭代变量。
2. We will call userEntries
the iteration variable or simply iteration variable. 2.我们将userEntries
称为迭代变量或简称迭代变量。
Structure结构
<th:block th:each="iteration_variable: ${variable}">
<span th:text="${iteration_variable.variable_from_entity}"></span>
<span th:text="${iteration_variable.variable_from_entity}"></span>
</th:block>
Here down is code下面是代码
<tr th:each="userEntries : ${entries}">
<td th:text="${userEntries.id}"></td>
<td th:text="${userEntries.userImage}"></td>
<td th:text="${userEntries.name}"></td>
<td th:text="${userEntries.requestID}"></td>
<td th:text="${userEntries.deviceType}"></td>
<td th:text="${userEntries.requestDate}"></td>
<td th:text="${userEntries.issueDetails}"></td>
<td th:text="${userEntries.status}"></td>
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.