Fórumok

Nyitólap » Liferay Portal » English » 3. Development

Kombinált nézet Egyszerű nézet Fa-nézet
Szálak [ Előző | Következő ]
marc henry
JSTL forEach with arrayList
2010. július 26. 0:52
Válasz

marc henry

Rangsorolás: New Member

Hozzászólások: 20

Csatlakozás dátuma: 2010. május 24.

Legújabb hozzászólások

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. július 26. 2:19
Válasz

Tina Agrawal

Rangsorolás: Regular Member

Hozzászólások: 190

Csatlakozás dátuma: 2008. január 2.

Legújabb hozzászólások

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. július 26. 2:42
Válasz

Gaurang Sheladia

Rangsorolás: Regular Member

Hozzászólások: 158

Csatlakozás dátuma: 2009. február 15.

Legújabb hozzászólások

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. augusztus 30. 8:58
Válasz

Felipe Sere

Rangsorolás: Junior Member

Hozzászólások: 87

Csatlakozás dátuma: 2010. január 27.

Legújabb hozzászólások

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. augusztus 31. 3:47
Válasz

Tina Agrawal

Rangsorolás: Regular Member

Hozzászólások: 190

Csatlakozás dátuma: 2008. január 2.

Legújabb hozzászólások

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. március 27. 6:50
Válasz

Mohamed Rizwanuzaman

Rangsorolás: New Member

Hozzászólások: 17

Csatlakozás dátuma: 2009. december 16.

Legújabb hozzászólások

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. március 27. 7:11
Válasz

Tina Agrawal

Rangsorolás: Regular Member

Hozzászólások: 190

Csatlakozás dátuma: 2008. január 2.

Legújabb hozzászólások

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