0

I'm new to Spring MVC and I wonder how can I print a table from database in my jsp page.

I used to do like this using Java servlet :

<div class="alert alert-info" role="alert">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<%
    HashMap<String,Status> statusMap = new HashMap<String,Status>();
    statusMap = AppUtils.getJobStatusLong();

    DatabaseController db_controller = (DatabaseController)application.getAttribute("db_controller");
    db_controller.openConnection(); 
out.print("<span>&nbsp;&nbsp;Today is "+AppUtils.getDate()+". You have " +db_controller.getClientNumber()+ 
    " clients and "+db_controller.getJobNumber()+" jobs. </span>");
%>

</div>

But I've heard that in Spring MVC view and model are strictly devided, so normally I shouldn't use Java scriplet in JSP ? But how to do it without Java scriptlet?

Thanks.

hawarden_
  • 1,904
  • 5
  • 28
  • 48
  • 1
    Normally, you should not use scriplets not only in Spring, but in any MVC based architecture. Use JSTL tags library. – drgPP Sep 07 '15 at 13:30

2 Answers2

2
  1. Extract all the required data from database in controller.
  2. (Optional) Denormalize the data as much as possible. This usually results in a clean view design.
  3. Push the data into the model.
  4. Iterate over the data in the view and print it on the screen.

JSP are not disallowed in Spring or anywhere else. It is just that you shouldn't put any complex logic in JSP. Looping over results and printing them is ok. Querying a database is not.

bezmax
  • 25,562
  • 10
  • 53
  • 84
0

If you are using jsp page then you can use JSTL to iterate through a collection. Here is an example of how to iterate a map using JSTL.

Community
  • 1
  • 1
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102