Java Spring MVC如何将数据库中的数据显示到表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36575698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:27:05  来源:igfitidea点击:

Spring MVC how to display data from database into a table

javadatabasespring-mvc

提问by Ian Santos

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:

我的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 里面没有任何东西

采纳答案by pokemzok

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 field1and field2. Now create your Data Transfer Object:

您应该返回一些您创建的对象而不是 String 值。假设您想将两个字段传输到您的页面,让它们命名为field1field2。现在创建您的数据传输对象:

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 世界完全陌生,我建议您还可以找到一些基础网络教程,例如这里: gotoseries