Spring MVC如何将数据从数据库显示到表中 [英] Spring MVC how to display data from database into a table

查看:125
本文介绍了Spring MVC如何将数据从数据库显示到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SPRING MVC的新用户,所以真的不知道。我想在表视图中显示数据库中的所有字段。我如何做?

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?

在我的控制器中

@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"%>

我的taskRepository中没有任何内容atm

i dont have anything inside my taskRepository atm

推荐答案

对于初学者:

@RequestMapping(value = "task", method = RequestMethod.GET)
public String taskList(Map<String, Object> model) {
        model.put("task", taskRepository.findAll());
        return "/tasks/list";
  }


$ b $ p

您应该返回一些已创建的对象,而不是String值。让我们假设您要将两个字段传输到您的网页,将其命名为 field1 field2 。现在创建您的数据传输对象:

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:

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
  }
}

请注意,我将您的存储库使用情况放入服务。这是它应该做的方式。您应该使用服务为了从您的数据库提取数据,并且您想要返回您的数据使用为该目的对象的数据传输对象。
我把assemblyTask方法的实现留给你。你需要做的就是分配你想要从实体传递的字段通过你的dto对象的视图。通常你会想要一个汇编器类为每个DTO对象,但为了简单起见,我通过使用方法介绍了这个想法。如果您想详细了解DTO,请查看此帖子:
get-value-of-invalid-field-after-methodargumentnotvalidexception

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

如果你是Spring全新的世界,我还推荐一些基础知识网络教程,例如:
gonetoseries

If you are completely new to the Spring world I recommend also find some basics web tutorials, for example here: gonetoseries

这篇关于Spring MVC如何将数据从数据库显示到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆