留言板

JSTL forEach with arrayList

marc henry,修改在13 年前。

JSTL forEach with arrayList

New Member 帖子: 20 加入日期: 10-5-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 :


&lt;%@page import="java.sql.ResultSetMetaData"%&gt;
&lt;%@page import="java.sql.ResultSet"%&gt;
&lt;%@page import="java.util.*" %&gt;
&lt;%@page contentType="text/html"%&gt;
&lt;%@page pageEncoding="UTF-8"%&gt;


&lt;%@ page import="javax.portlet.*"%&gt;
&lt;%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%&gt;

<portlet:defineobjects />
&lt;%PortletPreferences prefs = renderRequest.getPreferences();%&gt;

&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%&gt;


<b>
    DBPagePortlet - view MODE 
</b>

&lt;%

	ArrayList<integer> myList =  new ArrayList<integer>();
	int x=10,j=20,k=30;
	myList.add(x);
	myList.add(j);
	myList.add(k);
	
	
%&gt;

<c:foreach var="myValue" items="${myList}">
${myValue} 

</c:foreach>

</integer></integer>


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
thumbnail
Tina Agrawal,修改在13 年前。

RE: JSTL forEach with arrayList

Expert 帖子: 297 加入日期: 08-1-3 最近的帖子
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
thumbnail
Felipe Sere,修改在13 年前。

RE: JSTL forEach with arrayList

Junior Member 帖子: 87 加入日期: 10-1-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
thumbnail
Tina Agrawal,修改在13 年前。

RE: JSTL forEach with arrayList

Expert 帖子: 297 加入日期: 08-1-3 最近的帖子
Hi Felipe,

Your web.xml shall have the taglib defined.
Example -

<taglib>
		<taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri>
		<taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
</taglib>


Regards,
Tina
Mohamed Rizwanuzaman,修改在12 年前。

RE: JSTL forEach with arrayList

New Member 帖子: 20 加入日期: 09-12-17 最近的帖子
Hi Tina,
I had tried to use the jstl tag for looping a list.
this is my code


List<employee> employeeList = DepartmentLocalServiceUtil.getDepartmentEmployees(departmentId);
<c:foreach var="myValue" items="${employeeList}">
<c:out value="${myValue.employeeName}"></c:out>
</c:foreach>
</employee>

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


13:13:57,455 ERROR [render_portlet_jsp:154] org.apache.jasper.JasperException: /view.jsp (line: 42, column: 0) According to TLD or attribute directive
in tag file, attribute items does not accept any expressions
        at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
        at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
        at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
        at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1235)
        at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:870)
        at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1539)
        at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
        at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
        at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
        at org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
        at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
        at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1790)
        at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
        at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)


Its highly appreciable if anyone help to solve this issue.

Regards,
Rizwan
thumbnail
Gaurang Sheladia,修改在13 年前。

RE: JSTL forEach with arrayList

Regular Member 帖子: 159 加入日期: 09-2-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
thumbnail
Tina Agrawal,修改在12 年前。

RE: JSTL forEach with arrayList

Expert 帖子: 297 加入日期: 08-1-3 最近的帖子
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
Sudhanshu Kumar Shukla,修改在6 年前。

RE: JSTL forEach with arrayList

Junior Member 帖子: 35 加入日期: 16-8-29 最近的帖子
Thank you Tina
It's Work for me.
I have just added java objects into request object then it is working fine.


Again Thank you So much!!!!!!!!!!!!!emoticonemoticonemoticonemoticonemoticonemoticonemoticonemoticonemoticon