掲示板

JSTL forEach with arrayList

13年前 に marc henry によって更新されました。

JSTL forEach with arrayList

New Member 投稿: 20 参加年月日: 10/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 :


&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
13年前 に Tina Agrawal によって更新されました。

RE: JSTL forEach with arrayList

Expert 投稿: 297 参加年月日: 08/01/03 最新の投稿
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
13年前 に Felipe Sere によって更新されました。

RE: JSTL forEach with arrayList

Junior Member 投稿: 87 参加年月日: 10/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
thumbnail
13年前 に Tina Agrawal によって更新されました。

RE: JSTL forEach with arrayList

Expert 投稿: 297 参加年月日: 08/01/03 最新の投稿
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
12年前 に Mohamed Rizwanuzaman によって更新されました。

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
13年前 に Gaurang Sheladia によって更新されました。

RE: JSTL forEach with arrayList

Regular Member 投稿: 159 参加年月日: 09/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
thumbnail
12年前 に Tina Agrawal によって更新されました。

RE: JSTL forEach with arrayList

Expert 投稿: 297 参加年月日: 08/01/03 最新の投稿
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
6年前 に Sudhanshu Kumar Shukla によって更新されました。

RE: JSTL forEach with arrayList

Junior Member 投稿: 35 参加年月日: 16/08/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