how do i create an arraylist inside jsp using jstl - jstl

I want to collect values from a JSP page and pass it dynamically to another JSP page with the help of JSTL. How can I do this?

You use a request scoped HashMap for that.
Example
1) Declare the HashMap in each JSP you want to insert or access the list of values.
<jsp:useBean id="map" class="java.util.HashMap" scope="request"/>
Note: The scope="request" is what makes it accessible in other JSPs.
2) Stuff information into the HashMap
<c:set target="${requestScope.map}" property="city" value="${param.city}"/>
<c:set target="${requestScope.map}" property="state" value="${param.state}"/>
<c:set target="${requestScope.map}" property="phone" value="${param.phone}"/>
3a) You can now pull out the values in a different JSP by simply doing:
<c:out value="${requestScope.map['city']}"/>
-or-
3b) You can also iterate over that HashMap in a different JSP:
<c:forEach items="${requestScope.map}" var="item">
${item.key} = ${item.value}<br/>
</c:forEach>

Related

How do I write an if statement in JSP-JSTL if I need to check whether the user list contains a specific user?

'theGroup' and 'groupCreator' are a model(entity) attributes coming from the controller. 'users' is the array list which is the attribute of theGroup (theGroup.getUsers() basically).
I need to check if the groupCreator exists in theGroup.users list in JSP
I've tried the code below but it didn't work
<c:if test = "${theGroup.users.contains(groupCreator)}">
</c:if>
You can use forEach to iterate through values and then compare values in it with groupCreator. Your code will somewhat look like below :
<c:forEach var="values" items="${theGroup.users}">
<c:if test="${values == groupCreator}">
<!--setting true if value match-->
<c:set var="Matched" value="true" scope="request" />
</c:if>
</c:forEach
<!--Print-->
${Matched}

How to retrieve List content in JSP from Hibernate Query Language

I Developed the program that retrieve the content from Database and store it in the List
List<Student> studentList=session.createQuery("from Student s where s.lastName=:lastName and s.firstName=:firstName")
.setParameter("lastName",lname)
.setParameter("firstName",fname).list();
for(Student studentinfo:studentList)
{
System.out.println(studentinfo);
}
model.addAttribute("result",studentList);
And i have passed the result to the JSP File
${result}
<c:forEach var="item" items="${result}">
${item}
</c:forEach>
I am getting the result like this
"Student{id=6, firstName='arul', lastName='suju', address='s;lda'}"
But i want print the value one by one,Can any one suggest better code in JSP
I got the solution by modifying the jsp code like this
<c:forEach var="item" items="${result}">
${item.id}
${item.firstName}
${item.lastName}
${item.address}
</c:forEach>

jstl, how to fill current request with map

I have a map which contain request parameters and their value, which will be used later on the jsp page. I'm usinng jsp page inclusion later and I don't know what exact params would I use
How I suppose, the filling should be implemented:
<c:forEach var="theParameter" items="${ parametersMap }" >
<c:set var="${theParameter.key}" value="${theParameter.value}" />
</c:forEach>
But I'm getting the error that the 'var' attribute can't use expression
Do you have any ideas about workaround?
EDIT:
Example:
parametersMap = [ 'param1' : 'value1' ; 'param2' : 'value2']
as result I would like something like this:
<c:set var="param1" value="value1" />
<c:set var="param2" value="value2" />
You need to do like this
<c:set target="${RequestScope[aNewMap]}"
property="${theParameter.key}" value="${theParameter.value}"/>
I solved my problem by adding custom tag, which on java part retrieves the Hashmap object and fill the request (which can be retieved from pageContext)

Using object as key in map (treemap) in spring

I've tried to display on JSP page objects' properties from Map in FormBean. Map is defined as
Map<KeyObject, ValueObject> m
KeyObject has two properties
public class KeyObject implements Comparable<KeyObject> {
private Integer a;
private Integer b;
getters/setters/and rest basic methods
}
On JSP I want to obtain something like code below:
<c:forEach items="${formBean.m}" item="itm">
...
<form:input path="m[itm.key].propertyName" />
...
</c:forEach>
I need to:
display elements in proper order
submit objects to map
So is there any simple solution or I should do some "magic"?
Thanks for your time.
Stefan
Some more information. Each object will have other "view" so I try to use c:import
<c:forEach items="${formBean.m}" item="itm">
<c:import url=${itm.value.name}Page.jsp" />
</c:forEach>
and on ...Page.jsp I want to use form's inputs.
<c:forEach items="${formBean.m}" varStatus="itm">
<tr>
<td>${itm.key.propertyName}</td>
<td>${itm.value.propertyName}</td> <!--which is same as below ... -->
<td>${formBean.m[itm.key].propertyName}</td>
</tr>
</c:forEach>
You can iterate through the maps keys and values, like above, and output different fields as required.

Not able to retrieve session/request scope values using JSTL

In my xhtml page am setting values in some myValue variable. Code is as below,
<body>
<c:set var="myValue" value="someValue" scope="request"></c:set>`
</body>
Am hitting a login.jsp (Login page) from this xhtml page and trying to print the value on my login.jsp as below,
myValue ID retrieved from request <c:out value="${param.myValue}" />
This is not printing someValue. I also checked by putting it in session scope like below,
<c:set var="myValue" value="someValue" scope="session"></c:set>
Even this is not working
And in login.jsp,
myValue ID retrieved from session <c:out value="${sessionScope.myValue}" />
You should have just ${myValue}, as param.myValue means to find value from URL params.

Categories

Resources