3

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?

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

2
  • Start by creating an object with fields similar to used in jsp and stub the method with mock objects and you should see the records. Next you can fetch the data from DB and map to this object. Commented Apr 12, 2016 at 14:08
  • But what is the exact problem? Commented Apr 12, 2016 at 15:11

1 Answer 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:

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.