Creating View

We have created data access objects for Course and a Controller. Let's see how we can call them from a View. Views in Spring are typically JSPs. Create a JSP (name it courses.jsp) in the src/main/webapp/WEB-INF/views folder. This is the folder that we configured in servlet-context.xml to hold the Spring View files.

Add the JSTL tag library in courses.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

The markup code to display courses is very simple; we make use of the courses variable, which is made available in the Model from the CourseController.getCourses method and displays values using JSTL expressions:

<table> 
  <tr> 
    <th>Id</th> 
    <th>Name</th> 
    <th>Credits</th> 
    <th></th> 
  </tr> 
  <c:forEach items="${courses}" var="course"> 
    <tr> 
      <td>${course.id}</td> 
      <td>${course.name}</td> 
      <td>${course.credits}</td> 
    </tr> 
  </c:forEach> 
</table> 

Recall that courses is a list of objects of CourseDTO type. Members of CourseDTO are accessed in the forEach tag to display the actual values.

Unfortunately, we can't run this page from Eclipse the way we have so far in this book, that is, by right-clicking on the project or page and selecting Run As | Run on Server. If you try to run the project (right-click on the project and select the Run menu), then Eclipse will try to open the http://localhost:8080/course_management/ URL, and because we do not have any start page (index.html or index.jsp), we will get an HTTP 404 error. The reason that we can't run the page by right-clicking and selecting the run option is that Eclipse tries to open http://localhost:8080/course_management/WEB-INF/views/courses.jsp, and this fails because files in WEB-INF are not accessible from outside the server. Another reason, or rather the primary reason, that this URL will not work is that in web.xml, we have mapped all requests to be handled by DispatcherServlet of the Spring Framework and it does not find a suitable mapping for the request URL. To run the application, open the URL http://localhost:8080/course_management/courses in the browser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.20