I've a map in which I'm storing an Id as a Key and under that Id I've a List of bean class where in all the bean properties are set and stored. How do I display the contents of the map in my java class? I tried something like this but not getting the list values.
for (Entry<String, List<MyBean>> me : MyForm.getClientId().entrySet())
{
String key = me.getKey();
List<MyBean> valueList = me.getValue();
System.out.println(" Key: " + key);
System.out.print("Values: ");
for (MyBean s : valueList) {
System.out.println(" " + s.getFundId());//This comes as null even though there are values in the list
}
}
Similarly I want to print the contents on the jsp page. How do I do that?
Try this code :
Set your result map in as request attribute in Servlet as show below
request.setAttribute("myMap", map);
RequestDispatcher des = request.getRequestDispatcher("test.jsp");
des.forward(request, response);
You may used different method to pass value to JSP page.
Than after you can use JSTL Tag Library to read that value in JSP page.
You need to include jstl jar file into your class path and add below line to jsp to add tag library in page
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Used this code in JSP when you required to read value
<c:forEach var="beanList" items="${myMap}">
<c:forEach var="bean" items="${beanList['value']}">
<c:out value="${bean.fundId}"></c:out>
</c:forEach>
</c:forEach>
Where fundId is your bean property name.
May this will help you.
Just try this :-
Put this in your servlet/Controller class:-
javax.servlet.http.HttpServletRequest.setAttribute("myFormEntrySet",
And these lines in your jsp page:-
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
MyForm.getClientId().entrySet());
<c:forEach var="myFormEntrySetVar" items="${myFormEntrySet}">
<c:set var="myBeanList" value="${myFormEntrySetVar.value}" />
<c:forEach var="myBeanListVar" items="${myBeanList}" >
${myBeanListVar}
</c:forEach>
</c:forEach>
To use JSTL please download the jar of JSTL
Related
Hi I am trying to print the list of strings stored in ArrayList object using JSTL, here is my jsp page:
<%#page import="java.util.ArrayList"%>
<%#page import="java.util.List"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
List<String> al = new ArrayList<String>();
al.add("testing1");
al.add("testing2");
al.add("testing3");
al.add("testing4");
al.add("testing5");
System.out.println("hello"+ al);
%>
<c:forEach var="alla" items="${al}" >
<c:out value="Hello text"></c:out>
<c:out value="${alla}"></c:out>
</c:forEach>
when i see output it is not showing any thing on the browser, I have printed it using foreach it is showing the result where as in JSTL it is not printing ?
jstl is used to print the objects from the session or request scopes. you did not set your List al either of those .
Try adding it to the session and then access it through jstl
HttpSession session = request.getSession();
session.setAttribute("al", al);
and use c:forEach to print it in your page.
See also
Scope of jsp objects
How do i use jstl
Add values to arraylist use JSTL
how do i create an arraylist inside jsp using jstl
I have to write session values to a a text file in a JSP page. I was able to retrieve the values of the session using following JSTL code.
<c:forEach items="${chapters}" var="name">
<c:out value="Iteration..."/>
<c:out value="${name}"/>
</c:forEach>
After retrieving values , i have to write these values to a text file , and the following sample code works well. But i cannot use JSTL tags inside JSP tags (<% ... %>).
1.Is it possible to use JSTL value inside JSP tags?
2.How to retrieve the session value inside JSP tags ?
It's not recommended to write text file using JSP. It's better to use backend technology so you need to use servlet for that which is made for that, this type of operation must be done by Server Side.
If still you want to do so you can do it using scriplet tag :
<c:set var="name" value="${name}"/>
<%# page import="java.io.*" %>
<%
String str = (String)session.getAttribute("sessionName");
String name = (String)pageContext.getAttribute("name");
String nameOfTextFile = "/resources/data.txt";
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(str);
pw.close();
} catch(IOException e) {
out.println(e.getMessage());
}
%>
Use a servlet for that. JSP is just a view technology.
I am trying to set a variable (named as "o") in jsp in the body of tag - how could I do it without scriplets?
I have wrote this piece of code but it is not working:
<a class="overfl" href="myServlet?action=request.setAttribute('o',i)"> ${values[i]} </a>
Try with JSTL Core c:set Tag to set the attribute in any scope.
Sample code:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="salary" scope="request" value="${2000*2}"/>
ServletRequest#setAttribute() method doesn't return any value.
Get the value back in the same way as you are doing here ${values[i]}
or try with JSTL Core c:out Tag to get the value back.
In your case simply pass the action values as query parameter as shown below:
<a class="overfl" href="myServlet?action=${i}"> ${values[i]} </a>
And get the value back at server side using
String action = servletRequest.getParameter("action");
If the varaible is not already defined in your request's attribute so call <%request.setAttribute('o',i); %> then if you want to write it to the jsp output you have to to write <%request.getAttribute('o') %> in the place where you want to add it's value like this :
<%request.setAttribute('o',i); %>
<a class="overfl" href="myServlet?action=<%=request.getAttribute('o') %>"> ${values[i]} </a>
I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />
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