フォーラム

ホーム » Liferay Portal » English » 3. Development

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
marc henry
JSTL forEach with arrayList
2010/07/26 0:52
答え

marc henry

ランク: New Member

投稿: 20

参加年月日: 2010/05/24

最近の投稿

Hello,

I've got trouble while trying to use <c:forEach> JSTL Tag in my JSP.

Here is a sample source code I made to test the forEach tag :

 1
 2<%@page import="java.sql.ResultSetMetaData"%>
 3<%@page import="java.sql.ResultSet"%>
 4<%@page import="java.util.*" %>
 5<%@page contentType="text/html"%>
 6<%@page pageEncoding="UTF-8"%>
 7
 8
 9<%@ page import="javax.portlet.*"%>
10<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
11
12<portlet:defineObjects />
13<%PortletPreferences prefs = renderRequest.getPreferences();%>
14
15<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
16<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
17
18
19<b>
20    DBPagePortlet - view MODE
21</b>
22
23<%
24
25    ArrayList<Integer> myList =  new ArrayList<Integer>();
26    int x=10,j=20,k=30;
27    myList.add(x);
28    myList.add(j);
29    myList.add(k);
30   
31   
32%>
33
34<c:forEach var="myValue" items="${myList}">
35${myValue}
36
37</c:forEach>


And all I have got is ${myValue} displayed on the screen but not the values I want !

Moreover, ${myValue} is displayed only one time, not three which means that the loop does not run good...

Please help,

Thanks
Tina Agrawal
RE: JSTL forEach with arrayList
2010/07/26 2:19
答え

Tina Agrawal

ランク: Expert

投稿: 255

参加年月日: 2008/01/02

最近の投稿

Hi,

A few things that you need to do -
1) Did you make the entry in web.xml?
2) You need to use c:out tag to display the value.
3) Set the Arraylist in your Java code and then set it in request object.

Regards,
Tina
Gaurang Sheladia
RE: JSTL forEach with arrayList
2010/07/26 2:42
答え

Gaurang Sheladia

ランク: Regular Member

投稿: 160

参加年月日: 2009/02/15

最近の投稿

Hi Marc,

You have used core_rt library for iterating the for loop.

Can you please replace

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


with the following line

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


In JSTL Core library has function to iterate for loop.

Hope it help.


Regards,
Gaurang
Felipe Sere
RE: JSTL forEach with arrayList
2010/08/30 8:58
答え

Felipe Sere

ランク: Junior Member

投稿: 87

参加年月日: 2010/01/27

最近の投稿

Tina Agrawal:
Hi,

A few things that you need to do -
1) Did you make the entry in web.xml?
2) You need to use c:out tag to display the value.
3) Set the Arraylist in your Java code and then set it in request object.

Regards,
Tina


Hi Tina,

I am also fighting with JSTL (and I am quite new to it) and I was wondering what you meant with "making an entry in web.xml". Can you post an example?

Felipe
Tina Agrawal
RE: JSTL forEach with arrayList
2010/08/31 3:47
答え

Tina Agrawal

ランク: Expert

投稿: 255

参加年月日: 2008/01/02

最近の投稿

Hi Felipe,

Your web.xml shall have the taglib defined.
Example -
1
2<taglib>
3        <taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri>
4        <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
5</taglib>


Regards,
Tina
Mohamed Rizwanuzaman
RE: JSTL forEach with arrayList
2012/03/27 6:50
答え

Mohamed Rizwanuzaman

ランク: New Member

投稿: 18

参加年月日: 2009/12/16

最近の投稿

Hi Tina,
I had tried to use the jstl tag for looping a list.
this is my code

1
2List<Employee> employeeList = DepartmentLocalServiceUtil.getDepartmentEmployees(departmentId);
3<c:forEach var="myValue" items="${employeeList}" >
4<c:out value="${myValue.employeeName}"></c:out>
5</c:forEach>

And i have include the c.tld and jstl.jar in liferay-plugin-package.properties but its not working ,
Below is the error i facing

 1
 213:13:57,455 ERROR [render_portlet_jsp:154] org.apache.jasper.JasperException: /view.jsp (line: 42, column: 0) According to TLD or attribute directive
 3in tag file, attribute items does not accept any expressions
 4        at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
 5        at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
 6        at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
 7        at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1235)
 8        at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:870)
 9        at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1539)
10        at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
11        at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
12        at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
13        at org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
14        at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
15        at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1790)
16        at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
17        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
18        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
19        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
20        at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
21        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
22        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
23        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)


Its highly appreciable if anyone help to solve this issue.

Regards,
Rizwan
Tina Agrawal
RE: JSTL forEach with arrayList
2012/03/27 7:11
答え

Tina Agrawal

ランク: Expert

投稿: 255

参加年月日: 2008/01/02

最近の投稿

Hi Mohamed ,

Your code is wrong. You cant use java objects like this in JSTL.
You need to set employeeList in the request object from your portlet and then iterate it.

Tina