I am very new with SPRING MVC so really I dont know much about it as of the moment. I want to display all the fields in the database in a table view how do I do this?
我对SPRING MVC很新,所以我现在还不太了解它。我想在表格视图中显示数据库中的所有字段我该怎么做?
in my controller
在我的控制器中
@RequestMapping(value = "task", method = RequestMethod.GET)
public String taskList(Map<String, Object> model) {
model.put("task", taskRepository.findAll());
return "/tasks/list";
}
my jsp:
<%@include file="/WEB-INF/views/includes/header.jsp"%>
<h4 class="form-header">${title}</h4>
<div class="forms col-md-12 bounceInDown mainContent" data-wow-delay="0.2s">
<table class="table table-striped">
<thead>
<tr>
<th>Task ID</th>
<th>Task Name</th>
<th>Task Description</th>
</tr>
</thead>
<tbody>
<c:if test="${empty task}">
<tr>
<td colspan="8">No task to Display</td>
</tr>
</c:if>
<c:if test="${not empty task}">
<c:forEach items="${tasks}" var="task">
<tr class="">
<td>${task.taskid}</td>
<td>${task.taskName}</td>
<td>${task.taskDescription}</td>
<td>
<fmt:message key="task.list.status.text.${task.status}" />
</td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</div>
<%@include file="/WEB-INF/views/includes/footer.jsp"%>
i dont have anything inside my taskRepository atm
我的taskRepository atm里面没有任何东西
1 个解决方案
#1
3
For the starters:
对于初学者:
@RequestMapping(value = "task", method = RequestMethod.GET)
public String taskList(Map<String, Object> model) {
model.put("task", taskRepository.findAll());
return "/tasks/list";
}
You should return some object you have created instead of String value. Let's asume you want to transfer two fields to you page lets name them field1 and field2. Now create your Data Transfer Object:
您应该返回一些您创建的对象而不是String值。假设您要将两个字段传输到您的页面,请将它们命名为field1和field2。现在创建您的数据传输对象:
public class MyEntityDto{
private String filed1;
private String field2;
//Getter and setter method
.
.
.
}
Now your controller should look something like this:
现在你的控制器应该是这样的:
@Autowired
SomeSevice someService;
@RequestMapping(value = "task", method = RequestMethod.GET)
@ResponseBody
public List<MyEntityDto> taskList(Map<String, Object> model) {
List<MyEntityDto> dtoList = someService.findALl();
return dtoList;
}
Your service from the other hand should look something like this:
另一方面,您的服务应该如下所示:
@Service
public class SomeService(){
@Autowired
TaskRepository taskRepository;
public List<MyEntityDto> findAll(){
return assemblyTasks(taskRepository.findAll());//TODO implement method assemblyTasks
}
}
Notice that I put your repository usage into the service.This is the way it supposed to be done. You should use services in order to fetch data from your database and than you want to return your data using specificlly design for that purpose object - Data Transfer Object. I leave the implementation of assemblyTask method to you. What you need to do there is to assign fields you want to pass from entity to view through your dto object. Generally you would want to have an assembler class for every DTO object but for the sake of simplicity I introduced the idea by using method. If you want to read more about DTO, view this post: getting-value-of-invalid-field-after-methodargumentnotvalidexception
请注意,我将您的存储库用法放入服务中。这是它应该完成的方式。您应该使用服务来从数据库中获取数据,而不是使用特定设计为此目的对象 - 数据传输对象返回数据。我将assemblyTask方法的实现留给你。您需要做的是分配您想要从实体传递到通过dto对象查看的字段。通常你会希望每个DTO对象都有一个汇编程序类,但为了简单起见,我通过使用方法介绍了这个想法。如果您想了解有关DTO的更多信息,请查看以下文章:getting-value-of-invalid-field-after-methodargumentnotvalidexception
If you are completely new to the Spring world I recommend also find some basics web tutorials, for example here: gonetoseries
如果您对Spring世界完全陌生,我建议您也找一些基础Web教程,例如:gonetoseries
#1
3
For the starters:
对于初学者:
@RequestMapping(value = "task", method = RequestMethod.GET)
public String taskList(Map<String, Object> model) {
model.put("task", taskRepository.findAll());
return "/tasks/list";
}
You should return some object you have created instead of String value. Let's asume you want to transfer two fields to you page lets name them field1 and field2. Now create your Data Transfer Object:
您应该返回一些您创建的对象而不是String值。假设您要将两个字段传输到您的页面,请将它们命名为field1和field2。现在创建您的数据传输对象:
public class MyEntityDto{
private String filed1;
private String field2;
//Getter and setter method
.
.
.
}
Now your controller should look something like this:
现在你的控制器应该是这样的:
@Autowired
SomeSevice someService;
@RequestMapping(value = "task", method = RequestMethod.GET)
@ResponseBody
public List<MyEntityDto> taskList(Map<String, Object> model) {
List<MyEntityDto> dtoList = someService.findALl();
return dtoList;
}
Your service from the other hand should look something like this:
另一方面,您的服务应该如下所示:
@Service
public class SomeService(){
@Autowired
TaskRepository taskRepository;
public List<MyEntityDto> findAll(){
return assemblyTasks(taskRepository.findAll());//TODO implement method assemblyTasks
}
}
Notice that I put your repository usage into the service.This is the way it supposed to be done. You should use services in order to fetch data from your database and than you want to return your data using specificlly design for that purpose object - Data Transfer Object. I leave the implementation of assemblyTask method to you. What you need to do there is to assign fields you want to pass from entity to view through your dto object. Generally you would want to have an assembler class for every DTO object but for the sake of simplicity I introduced the idea by using method. If you want to read more about DTO, view this post: getting-value-of-invalid-field-after-methodargumentnotvalidexception
请注意,我将您的存储库用法放入服务中。这是它应该完成的方式。您应该使用服务来从数据库中获取数据,而不是使用特定设计为此目的对象 - 数据传输对象返回数据。我将assemblyTask方法的实现留给你。您需要做的是分配您想要从实体传递到通过dto对象查看的字段。通常你会希望每个DTO对象都有一个汇编程序类,但为了简单起见,我通过使用方法介绍了这个想法。如果您想了解有关DTO的更多信息,请查看以下文章:getting-value-of-invalid-field-after-methodargumentnotvalidexception
If you are completely new to the Spring world I recommend also find some basics web tutorials, for example here: gonetoseries
如果您对Spring世界完全陌生,我建议您也找一些基础Web教程,例如:gonetoseries