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:
These are pictures in mainPage.jsp
:
Mykhailo Moskura
's suggestion doesn't work too:
java spring-mvc
add a comment |
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:
These are pictures in mainPage.jsp
:
Mykhailo Moskura
's suggestion doesn't work too:
java spring-mvc
You are getting a list ofSubject
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
add a comment |
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:
These are pictures in mainPage.jsp
:
Mykhailo Moskura
's suggestion doesn't work too:
java spring-mvc
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:
These are pictures in mainPage.jsp
:
Mykhailo Moskura
's suggestion doesn't work too:
java spring-mvc
java spring-mvc
edited Nov 9 at 19:24
asked Nov 9 at 18:45
A.M
767
767
You are getting a list ofSubject
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
add a comment |
You are getting a list ofSubject
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
add a comment |
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
.
Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38
1
To tell that code ofuserService.getUserSubjects()
is needed
– Ivan
Nov 9 at 19:41
add a comment |
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
.
Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38
1
To tell that code ofuserService.getUserSubjects()
is needed
– Ivan
Nov 9 at 19:41
add a comment |
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
.
Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38
1
To tell that code ofuserService.getUserSubjects()
is needed
– Ivan
Nov 9 at 19:41
add a comment |
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
.
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
.
answered Nov 9 at 19:37
Ivan
4,4791720
4,4791720
Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38
1
To tell that code ofuserService.getUserSubjects()
is needed
– Ivan
Nov 9 at 19:41
add a comment |
Yes, it works, but why is it so?
– A.M
Nov 9 at 19:38
1
To tell that code ofuserService.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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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