Import and print arraylist to JSP - java

I have simple jsp page (page.jsp), and a simple java class (classWithArray) with an arraylist (list) in it.
How can I get access to the arraylist from jsp, for example to show it in a table?

Use JSTL's forEach tag to iterate over a collection (an ArrayList in this case in particular) inside a JSP, take a look at this post for more details.

you can iterate over an ArrayList using c:forEach tag from JSTL
Below is the sample code which iterates over an peopleList List.
<c:forEach var="person" items="${people.peopleList}">
<tr>
<td>${person.name}</td>
</tr>
</c:forEach>
use page tag to import a java.util.List in your JSP page use
<%# page import="java.util.List" %>

Related

Sent data from servlet to jsp and dynamically generate dropdown list

In a servlet I have a list which has data. I put that list in a method doget() to access it in the onPage Load event.
List<Integer> list = pse.populateemployee();
request.setAttribute("list", list);
I want to send that list to my JSP page using JSTL.
I tried this:
<select name="selectemployee">
<c:forEach var="employees" items="${list}">
<option> <c:out value="${employees}"/> </option>
</c:forEach>
</select>
I am using MVC
Why isn't this working?
i solved the problem
i'm running jsp page insted of servlet thats why jsp page is not get data from servlet on load
now tell me how to mapp my servlet in XML file to runn servlet first instead of jsp
You need to add the link .. for example you are in jsp page, and if you want to go from jsp to servlet, you need to give the path .. ( ./servlet name in jsp page)
<form action="./servlet name" method=post>
<\form>
You also should close the form.

List Iterate in paragraph form in JSF page

I want to iterate list in paragraph form in JSF page...
for E.g.
I have list and i want to iterate it's value as
like
list<String>={'test1','test2','test3'}
I want JSF output as
test1:..test2:...test3...
how to achieve this ?
Hope this is helpful.
<html xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:repeat var="test" value="#{someClass.methodThatReturnList}">
#{test}:
</ui:repeat>
JSF way is h:dataTable but since you want it just to be displayed in one line you can go with JSTL
<c:forEach items="${yourBean.youeLIst}" var="para">
<c:out value="${para}"/>
</c:forEach>

struts2 jstl iterator tag

I have been looking how to implement a thing in struts2 jstl but it is impossible for me to find the way.
When I load the jsp page from the action, I have a list of String lists.
I want to create as divs as elements have the list, but inside every div, I want to create as links as the third element of the sub-list.
So I use the s:iterator tag to parse the list. But I don't know how to iterate "${item[2]}" times inside the first iterator.
The code would be something like this:
<s:iterator value="functions" var="item" status="stat">
<span class="operation">${item[1]}</span>
<div id="${item[0]}">
<s:for var $i=0;$i<${item[2]};$i++>
Link $i
</s:for>
</div>
</s:iterator>
Where I have put the s:for tag is where I would like to iterate "${item[2]}" times...
Anyone can help me?
Thank you very much in advance,
Aleix
Make sure you've got the JSTL core library in scope in your JSP page:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
And simply use <c:forEach>. Something like this:
<c:forEach var="i" begin="0" end="${item[2] - 1}">
Link ${i}
</c:forEach>
You should use List of Map if appropriate, example :
Action class
// List of raw type Map
private List<Map> functions = Lists.newArrayList(); // with getter
#Override
public String execute() {
// loops {
Map map = Maps.newHashMap();
map.put("id", id);
map.put("operation", operation);
map.put("count", count); // count is int/Integer
functions.add(map);
// }
return SUCCESS;
}
.jsp
<s:iterator value="functions">
<span class="operation">${operation}</span>
<div id="${id}">
<s:iterator begin="0" end="count - 1" var="link">
Link ${link}
</s:iterator>
</div>
</s:iterator>
or with <s:a /> (example)
<s:a action="action_name" id="%{link}" anchor="%{link}">Link ${link}</s:a>
output
<a id="[id]" href="/namespace/action#[anchor]">Link [link]</a>
See also
Struts2 Guides -> Tag Reference -> s:iterator

JSTL 2d Array Iteration

I have a 2d array stored in a java bean and I'm trying to iterate through its contents to print a corresponding table on a JSP page. The array is a bean data member which I'm accessing through the EL code ${board.cells}. I tried to do this with a c:forEach tag, but it's only printing a single cell. I know the array contents are valid, as I can see them when I index them directly by ${board.cells[0][0]}
Here's my loop code, embedded in the JSP.
<c:forEach items="${board.cells}" var="row">
<tr>
<c:forEach items="${row}" var="cell">
<td><img src=${cell} align="" alt="cell"></td>
</c:forEach>
</tr>
</c:forEach>
Any help is much appreciated!
As per the comments, JSTL core tags are simply not been interpreted/parsed. They are been sent plain to the HTML response. You need to declare the JSTL core taglib in top of your JSP to get them to run.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
See also:
Our JSTL wiki page

Converting an ArrayList<someObjects> to an HTML table

I have a couple of ArrayLists with variable length and sometimes null. This ArrayList contains a bunch of objects.
The table should have columns based on (some) attributes of the object. And the table should be displayed on a jsp.
I have two ideas, one is to use a JSTL tag the other is to use JavaScript. And library suggestions are welcome.
JSTL is the standard, preferred way (unless you need to load it via ajax, for example)
<table>
<tr><td>Foo header</td><td>Bar header</td></tr>
<c:forEach items="${yourRequestScopedArrayList}" var="obj">
<tr>
<td>${obj.foo}</td>
<td>${obj.bar}</td>
</tr>
</c:forEach>
</table>
JSTL is better,
Javascript you should avoid as much as possible ,
I am not sure how you are going to render datatable using java script and Collection
How to use jstl with collection that has been demonstrated by Bozho in the same thread.
Javascript doesn't have access to the Java objects that live (I presume) on the server. The server code can make the ArrayLists available to the JSP which can then loop over them with a JSTL forEach tag.
How you make the ArrayLists "available" depends on the framework you're using, but the plain servlet way is just setting an attribute from the doPost method.
request.setAttribute("list1", arrayList1);
The loop would be something like
<table>
<tr><th>Column 1</th> <th>Column 2</th> <th>Column 3</th></tr>
<c:forEach var="row" items="${list1}">
<tr><td>${row.col1data}</td> <td>${row.col2data}</td> <td>${row.col3data}</td></tr>
</c:forEach>
</table>

Categories