1

I'm working on a college project that stores links to various learning materials. There is a list of links to text tutorials in my DB, and the project's files are following:

TextTutorialController.java:

package io.spring.learn.web;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.spring.learn.proc.TextTutorial;
import io.spring.learn.proc.TextTutorialService;

@Controller
@RequestMapping("/textTutorials")
public class TextTutorialController {

    @Inject
    TextTutorialService service;

    @RequestMapping(value = "/listOfTextTutorials", method = RequestMethod.GET)
    public String showList(ModelMap model) {
        List<TextTutorial> listOfTextTutorials = service.findAll();
        model.addAttribute("textTutorialList", listOfTextTutorials);
        return "listOfTextTutorials";
    }

}

listOfTextTutorials.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List of Text Tutorials</title>
</head>
<body>
    <h1>List of Text Tutorials on Spring:</h1>
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>URL</th>
            <th>Alexa Global Rank</th>
            <th>Total Chapters</th>
            <th>% Completed</th>
        </tr>
        <c:forEach var="textTutorial" items="${textTutorialList}">
            <tr>
                <td><c:out value="${textTutorial.id}" /></td>
                <td><c:out value="${textTutorial.name}" /></td>
                <td><c:out value="${textTutorial.url}" /></td>
                <td><c:out value="${textTutorial.alexaGlobalRank}" /></td>
                <td><c:out value="${textTutorial.totalChapters}" /></td>
                <td><c:out value="${textTutorial.percentCompleted}" /></td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

index.jsp:

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

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

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>
        <a href="textTutorials/listOfTextTutorials.html">List of Text Tutorials on Spring</a>
    </body>
</html>

After running the application on Tomcat the console shows that DB connection goes well and even some SELECT statements are queried:

[EL Info]: 2016-06-29 03:29:17.721--ServerSession(2113257970)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2016-06-29 03:29:18.265--ServerSession(2113257970)--file:/Users/AgentDen/Documents/Java/MyProjectSpringMVC/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/LearnSpringMVC/WEB-INF/classes/_LearnSpring login successful
[EL Fine]: sql: 2016-06-29 03:29:18.314--ServerSession(2113257970)--Connection(302754878)--SELECT ID FROM books WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:18.842--ServerSession(2113257970)--Connection(500719466)--SELECT ID FROM CLASSROOM WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:18.886--ServerSession(2113257970)--Connection(1612903660)--SELECT ID FROM general_articles WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:18.905--ServerSession(2113257970)--Connection(250314644)--SELECT ID FROM online_courses WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:18.922--ServerSession(2113257970)--Connection(474813894)--SELECT ID FROM text_tutorials WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:18.936--ServerSession(2113257970)--Connection(1688125903)--SELECT ID FROM youtube_tutorials WHERE ID <> ID
[EL Fine]: sql: 2016-06-29 03:29:19.619--ServerSession(2113257970)--Connection(585968463)--SELECT ID, alexa_global_rank, chapters_studied, date_finished, date_started, days_spent, LANGUAGE, LEVEL, NAME, percent_completed, personal_rating, total_chapters, URL FROM text_tutorials

But everything that is displayed in my jsp is just html title and table header, but no data is displayed from the model:

List of Text Tutorials on Spring:

ID  Name    URL Alexa Global Rank   Total Chapters  % Completed

JSTL is included in pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

I've read through similar questions here but none seems to be specific to my case. I'm going really desperate over this already so any advice would be highly appreciated!

UPD. Here is the implementation of findAll() method of the service:

    package io.spring.learn.proc;

    import java.util.List;

    import javax.persistence.EntityManager;
    import javax.persistence.NoResultException;
    import javax.persistence.PersistenceContext;
    import javax.persistence.TypedQuery;

    import org.springframework.stereotype.Repository;

    @Repository
    public class TextTutorialDaoImpl implements TextTutorialDao {

        @PersistenceContext
        private EntityManager em;

        @Override
        public List<TextTutorial> findAll() {
            TypedQuery<TextTutorial> query = em.createQuery("Select txt from TextTutorial txt", TextTutorial.class);
            return query.getResultList();
        }

    ...

}

and the service implementation:

package io.spring.learn.proc;

import java.util.List;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Named
public class TextTutorialServiceImpl implements TextTutorialService {

    @Inject
    private TextTutorialDao txtDao;

    @Override
    public List<TextTutorial> findAll() {
        return txtDao.findAll();
    }

...

}
4
  • Can you confirm that the query returns values? i.e. at least one item is in the result set - Also, do you know how to use a debugger (there's one in every IDE)? - It will come in handy for situations like this Commented Jun 29, 2016 at 0:52
  • Yes, there are 3 values in the list at the moment which are successfully returned in 'manual' mode (through sql scrapbook or main-console output) Commented Jun 29, 2016 at 0:55
  • At first sight, it all looks good from here - what do you get if you print out the length of textTutorialList on the JSP? i.e.${fn:length(textTutorialList)} Commented Jun 29, 2016 at 0:59
  • Please post service and repository code as well Commented Jun 29, 2016 at 1:46

3 Answers 3

1

Your code looks good. The only thing I can suspect is your service -- I am sure it returns an empty list. Could you print out size of the list before adding it to the model map?

public String showList(ModelMap model) {
    List<TextTutorial> listOfTextTutorials = service.findAll();
    System.out.println(listOfTextTutorials.size()); // <-- This line.
    model.addAttribute("textTutorialList", listOfTextTutorials);
    return "listOfTextTutorials";
}
Sign up to request clarification or add additional context in comments.

6 Comments

Well it sure made me even more confused: the size of the list is actually 0 when printed out that way, but when I use main method in console mode: ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); TextTutorialService tts = ctx.getBean(TextTutorialService.class); List<TextTutorial> ttList = tts.findAll(); for ( TextTutorial tt : ttList ) { System.out.println(tt); } I do get all 3 entries in the list printed to console!
Then you have issue with your query. Could you post the service method that query for the list?
here is the service method implementation: @PersistenceContext private EntityManager em; @Override public List<TextTutorial> findAll() { TypedQuery<TextTutorial> query = em.createQuery("Select txt from TextTutorial txt", TextTutorial.class); return query.getResultList(); } (sorry for format - can't really figure out how to format code in comments)
Then it may be due to the Inject annotation. Did you try calling findAll() the same way you posted in the above comment, or use spring proprietary Autowired instead of Inject?
changing annotation to Autowired did not bring any changes. And what do you mean by calling findAll() the same way?
|
0

How about changing your controller method to this?

@RequestMapping(value = "/listOfTextTutorials", method = RequestMethod.GET)
public ModelAndView showList(ModelMap model) {
    List<TextTutorial> listOfTextTutorials = service.findAll();
    return new ModelAndView("listOfTextTutorials", "textTutorialList", listOfTextTutorials);;
}

And for old JSP 1.2 descriptor, you have to enable EL manually. Add following code in your JSP.

<%@ page isELIgnored="false" %>

Comments

0

Ok so thanks to @Nguyen Tuan Anh's advice to check the size of List returned from service I started digging and realized that Spring's connection to DB is somehow corrupted. Changing db url in application-config.xml from relative to absolute has resolved the problem, although I still can't tell why the relative path returned zero results.

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.