Can't iterate over list in Jsp











up vote
2
down vote

favorite












I am using hibernate to get the subjects associated with a particular user. In mainPage, I want to see firstName and LastName (this works) of the user and allowed subjects. But, for some reason, I am not able to successfully iterate over the list of subjects put in mainPage.



In my Spring controller I have a method, which looks like this:



@RequestMapping(value = "/signin", method = RequestMethod.POST)
public String SignIn(@ModelAttribute("user") User user, ModelMap modelMap){

String username = user.getUsername();
String password = user.getPassword();

User user1 = userService.getUser(username, password);
if(user1 != null){

List<Subject> subjectList = userService.getUserSubjects(user1.getId());

modelMap.addAttribute("subjects", subjectList);
modelMap.addAttribute("user", user1);
return "mainPage";
}

modelMap.addAttribute("ShowNotAuthenticated", true);
return "authentication";
}


mainPage.jsp looks like this:



<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Current user: ${user.firstName} ${user.lastName}</h2> //this works
<br>
<h3>Allowed subjects:</h3>
<table>
<tr>
<td>
Subject Name
</td>
</tr>
<c:forEach items="${subjects}" var="x"> //doesn't work
<tr>
<td>
${x.name}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>


This is SignIn method during debug:



enter image description here



These are pictures in mainPage.jsp:



enter image description here



enter image description here



Mykhailo Moskura's suggestion doesn't work too:



enter image description here










share|improve this question
























  • You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
    – Alfabravo
    Nov 9 at 19:24












  • @Alfabravo, you are right, ${x[1]} works
    – A.M
    Nov 9 at 19:37















up vote
2
down vote

favorite












I am using hibernate to get the subjects associated with a particular user. In mainPage, I want to see firstName and LastName (this works) of the user and allowed subjects. But, for some reason, I am not able to successfully iterate over the list of subjects put in mainPage.



In my Spring controller I have a method, which looks like this:



@RequestMapping(value = "/signin", method = RequestMethod.POST)
public String SignIn(@ModelAttribute("user") User user, ModelMap modelMap){

String username = user.getUsername();
String password = user.getPassword();

User user1 = userService.getUser(username, password);
if(user1 != null){

List<Subject> subjectList = userService.getUserSubjects(user1.getId());

modelMap.addAttribute("subjects", subjectList);
modelMap.addAttribute("user", user1);
return "mainPage";
}

modelMap.addAttribute("ShowNotAuthenticated", true);
return "authentication";
}


mainPage.jsp looks like this:



<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Current user: ${user.firstName} ${user.lastName}</h2> //this works
<br>
<h3>Allowed subjects:</h3>
<table>
<tr>
<td>
Subject Name
</td>
</tr>
<c:forEach items="${subjects}" var="x"> //doesn't work
<tr>
<td>
${x.name}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>


This is SignIn method during debug:



enter image description here



These are pictures in mainPage.jsp:



enter image description here



enter image description here



Mykhailo Moskura's suggestion doesn't work too:



enter image description here










share|improve this question
























  • You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
    – Alfabravo
    Nov 9 at 19:24












  • @Alfabravo, you are right, ${x[1]} works
    – A.M
    Nov 9 at 19:37













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am using hibernate to get the subjects associated with a particular user. In mainPage, I want to see firstName and LastName (this works) of the user and allowed subjects. But, for some reason, I am not able to successfully iterate over the list of subjects put in mainPage.



In my Spring controller I have a method, which looks like this:



@RequestMapping(value = "/signin", method = RequestMethod.POST)
public String SignIn(@ModelAttribute("user") User user, ModelMap modelMap){

String username = user.getUsername();
String password = user.getPassword();

User user1 = userService.getUser(username, password);
if(user1 != null){

List<Subject> subjectList = userService.getUserSubjects(user1.getId());

modelMap.addAttribute("subjects", subjectList);
modelMap.addAttribute("user", user1);
return "mainPage";
}

modelMap.addAttribute("ShowNotAuthenticated", true);
return "authentication";
}


mainPage.jsp looks like this:



<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Current user: ${user.firstName} ${user.lastName}</h2> //this works
<br>
<h3>Allowed subjects:</h3>
<table>
<tr>
<td>
Subject Name
</td>
</tr>
<c:forEach items="${subjects}" var="x"> //doesn't work
<tr>
<td>
${x.name}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>


This is SignIn method during debug:



enter image description here



These are pictures in mainPage.jsp:



enter image description here



enter image description here



Mykhailo Moskura's suggestion doesn't work too:



enter image description here










share|improve this question















I am using hibernate to get the subjects associated with a particular user. In mainPage, I want to see firstName and LastName (this works) of the user and allowed subjects. But, for some reason, I am not able to successfully iterate over the list of subjects put in mainPage.



In my Spring controller I have a method, which looks like this:



@RequestMapping(value = "/signin", method = RequestMethod.POST)
public String SignIn(@ModelAttribute("user") User user, ModelMap modelMap){

String username = user.getUsername();
String password = user.getPassword();

User user1 = userService.getUser(username, password);
if(user1 != null){

List<Subject> subjectList = userService.getUserSubjects(user1.getId());

modelMap.addAttribute("subjects", subjectList);
modelMap.addAttribute("user", user1);
return "mainPage";
}

modelMap.addAttribute("ShowNotAuthenticated", true);
return "authentication";
}


mainPage.jsp looks like this:



<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>Current user: ${user.firstName} ${user.lastName}</h2> //this works
<br>
<h3>Allowed subjects:</h3>
<table>
<tr>
<td>
Subject Name
</td>
</tr>
<c:forEach items="${subjects}" var="x"> //doesn't work
<tr>
<td>
${x.name}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>


This is SignIn method during debug:



enter image description here



These are pictures in mainPage.jsp:



enter image description here



enter image description here



Mykhailo Moskura's suggestion doesn't work too:



enter image description here







java spring-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:24

























asked Nov 9 at 18:45









A.M

917




917












  • You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
    – Alfabravo
    Nov 9 at 19:24












  • @Alfabravo, you are right, ${x[1]} works
    – A.M
    Nov 9 at 19:37


















  • You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
    – Alfabravo
    Nov 9 at 19:24












  • @Alfabravo, you are right, ${x[1]} works
    – A.M
    Nov 9 at 19:37
















You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
– Alfabravo
Nov 9 at 19:24






You are getting a list of Subject into that variable so, maybe, you are trying to print something you don't have. As you see, the're are 0=> something, 1=> Physics, etc... and the NumberFormatException shows you're trying to process something different from what you expect, Check stackoverflow.com/questions/2148658/…
– Alfabravo
Nov 9 at 19:24














@Alfabravo, you are right, ${x[1]} works
– A.M
Nov 9 at 19:37




@Alfabravo, you are right, ${x[1]} works
– A.M
Nov 9 at 19:37












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










From debugger screenshot I see that your subjectList has elements of type Object. So you either need to change what you store in that list or try to use ${x[1]} instead of ${x.name}.






share|improve this answer





















  • Yes, it works, but why is it so?
    – A.M
    Nov 9 at 19:38








  • 1




    To tell that code of userService.getUserSubjects() is needed
    – Ivan
    Nov 9 at 19:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231651%2fcant-iterate-over-list-in-jsp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










From debugger screenshot I see that your subjectList has elements of type Object. So you either need to change what you store in that list or try to use ${x[1]} instead of ${x.name}.






share|improve this answer





















  • Yes, it works, but why is it so?
    – A.M
    Nov 9 at 19:38








  • 1




    To tell that code of userService.getUserSubjects() is needed
    – Ivan
    Nov 9 at 19:41















up vote
3
down vote



accepted










From debugger screenshot I see that your subjectList has elements of type Object. So you either need to change what you store in that list or try to use ${x[1]} instead of ${x.name}.






share|improve this answer





















  • Yes, it works, but why is it so?
    – A.M
    Nov 9 at 19:38








  • 1




    To tell that code of userService.getUserSubjects() is needed
    – Ivan
    Nov 9 at 19:41













up vote
3
down vote



accepted







up vote
3
down vote



accepted






From debugger screenshot I see that your subjectList has elements of type Object. So you either need to change what you store in that list or try to use ${x[1]} instead of ${x.name}.






share|improve this answer












From debugger screenshot I see that your subjectList has elements of type Object. So you either need to change what you store in that list or try to use ${x[1]} instead of ${x.name}.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:37









Ivan

4,4891720




4,4891720












  • Yes, it works, but why is it so?
    – A.M
    Nov 9 at 19:38








  • 1




    To tell that code of userService.getUserSubjects() is needed
    – Ivan
    Nov 9 at 19:41


















  • Yes, it works, but why is it so?
    – A.M
    Nov 9 at 19:38








  • 1




    To tell that code of userService.getUserSubjects() is needed
    – Ivan
    Nov 9 at 19:41
















Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38






Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38






1




1




To tell that code of userService.getUserSubjects() is needed
– Ivan
Nov 9 at 19:41




To tell that code of userService.getUserSubjects() is needed
– Ivan
Nov 9 at 19:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231651%2fcant-iterate-over-list-in-jsp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff