How to change Map value dynamically in JSTL - java

One of our application built using Spring MVC + JSP.
Please refer below JSP.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page import="java.util.ArrayList"%>
<%# page import="java.util.HashMap"%>
<%
ArrayList<HashMap<String, String>> listOfMap = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < 3; i++) {
map = new HashMap<String, String>();
map.put("key1", "value1" + i);
map.put("key2", "value2" + i);
listOfMap.add(map);
}
request.setAttribute("listOfMap", listOfMap);
%>
<html>
<body>
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>
</body>
</html>
As you can see in this jsp that i am trying to iterate map and display key and value on page.
But what should be done in case if i want to change the value of mapItem.key and mapItem.value while iterating inside the loop.
So it would like below.
<c:forEach items="${listOfMap}" var="maps">
<c:forEach items="${maps}" var="mapItem">
<!--Basically i would write a code (in scriplet) to prevent cross site scripting here -->
${mapItem.key} ${mapItem.value} <br />
</c:forEach>
</c:forEach>

Related

Passing Java array into JSP file suing HashMap

I'm trying to pass an arry into a new HashMap and then pass it into a jsp file. Problem is that it doesn't seem to be working. There is no output of my array on the jsp display. This is someof the code for the HashMap declaration.
Map<String, Object> params = new HashMap<String, Object>();
for (int x = 0; x < nl3.getLength(); x++){
Node currentItem3 = nl3.item(x);
String key3 = currentItem3.getAttributes().getNamedItem("name").getNodeValue();
if (requirement.equals(key3)) {
matchingID[x] = build;
params.put("message", matchingID[x]);}}
My jsp file looks like this
<%# include file="/include.jsp" %>
<bs:page>
<p>Please Input an Agent-Requirement</p>
<form method="get" action="/QueueStat.html">
Agent Requirement: <input type ="text" id='agentPram' name="requirement" value="${requirement}"/> <br/>
<input type="submit" value="Submit" />
List of Matching builds:
<c:forEach items="${mathcingId}" var="mathcingId">
<br><span class="mono mono-12px"><c:out value="${message}"/></span>
</c:forEach>
</jsp:attribute>
</bs:page>
Any ideas on what I'm doing wrong? I suspect that the prams.put might be incorrect. Or maybe the jsp file?

how to change the scriplet to JSTL display string array

how to change the scriplet to JSTL
<%String sentenceArray[] = (String[])request.getAttribute("displaySentenceArray"); %>
<% for (int i=0; i< sentenceArray.length;i++){ %>
<p> The Result IS : <%=sentenceArray[i] %> </p>
<%} %>
I am new to JSTL
Your JSTL should look like this
<c:forEach var="sentence" items="${requestScope.displaySentenceArray}" >
<p> The Result IS :<c:out value="${sentence}"></c:out></p>
</c:forEach>
Here requestScope.displaySentenceArray will get the whole array from request.'sentence'will be a single element of array, <c:forEach> tag will iterate the array and <c:out> will print the element to JSP.

how to use jstl to iterate through four arraylists and create cells with each content

I am using the following code to create a table:
<c:forEach var="surveyName" items="${surveyList}" varStatus="theCount" >
<tr>
<td align="left" style="border: 1px solid #DDDDDD;padding-left: 10px" ><c:out value="${surveyName}"/> </td>
</tr>
</c:forEach>
surveyList is the array I am passing to jsp. In the same way I have three more arraylist. I want each row to have four cells and each cells have content from the four arryalists i.e. first cell from first list second cell from second list and so on.
Here is demonstration code.
<%# page import="java.util.*" %>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%
ArrayList<String> listA = new ArrayList<String>();
listA.add("a1");
listA.add("a2");
listA.add("a3");
listA.add("a4");
listA.add("a5");
pageContext.setAttribute("listA", listA);
ArrayList<String> listB = new ArrayList<String>();
listB.add("b1");
listB.add("b2");
listB.add("b3");
listB.add("b4");
listB.add("b5");
pageContext.setAttribute("listB", listB);
ArrayList<String> listC = new ArrayList<String>();
listC.add("c1");
listC.add("c2");
listC.add("c3");
listC.add("c4");
listC.add("c5");
pageContext.setAttribute("listC", listC);
ArrayList<String> listD = new ArrayList<String>();
listD.add("d1");
listD.add("d2");
listD.add("d3");
listD.add("d4");
listD.add("d5");
pageContext.setAttribute("listD", listD);
%>
<c:set value="${fn:length(listA)}" var="listSize" />
<html>
<body>
<table>
<c:forEach begin="0" end="${listSize - 1}" var="current">
<tr>
<td>${listA[current]}</td>
<td>${listB[current]}</td>
<td>${listC[current]}</td>
<td>${listD[current]}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

Checking if a value is contained in an array in JSP

I have an if statement like so:
<c:if test="${id == '1' || id == '2' || id == '3' || id == '4'}">
and I'm wondering if there is a way that I can simplify this by checking if id is contained within an array/list/set, sort of like
<c:if test="${id isContainedIn {'1','2','3','4'}}">
Obviously this isn't correct, but it demonstrates what I am looking for. Is there anything similar to this? Thanks!
If you can create a String that contains your list, then you could use something like the following code.
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%
pageContext.setAttribute("listString", "1234");
%>
<c:set var="id" value="3"/>
<c:if test='${fn:contains(listString, id)}'>
Yes id is contained
</c:if>
You can do something like this:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
ArrayList list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
%>
<c:set var="list" value="<%=list%>" />
<html>
<body>
My list is ${list}<br/>
<c:if test='${fn:contains(list, "two")}'>
My list contains two <br/>
</c:if>
<c:if test='${fn:contains(list, ",")}'>
My list contains ,
</c:if>
</body>
</html>
The output for the code above is
My list is [one, two, three]
My list contains two
My list contains ,
I hope it helps someone.

Reused of JSP code

I have lots of JSPs containing code which have statements which could be reused like this select statements, inputs, etc.
Here is a sample of a JSP
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
(needed includes)
<%
ArrayList<Student> studentList = (from database)
%>
<html>
<head>
<title>Students</title>
</head>
<body>
<form>
(other inputs)
...
<select class="combobox">
<%for (Student studentObj:studentList) { %>
<option value="<%=studentObj.getId()">
<%=studentObj.getLastName() %>, <%=studentObj.getFirstName() %>
</option>
<%} %>
</select>
...
(other inputs)
</form>
</body>
</html>
What I did do is make a function as follows. This allows me to be able to pass an object parameter and get html code back.
public static getStudentSelect(ArrayList<Student> studentList) {
String htmlCode = "<select class=\"combobox\">";
for (Student studentObj:studentList) {
htmlCode += "<option value=\"" + studentObj.getId() + "\">" +
studentObj.getLastName() + ", " + studentObj.getFirstName() +
"</option>";
}
htmlCode += "</select>"
return htmlCode;
}
Is there a better way of doing this? Because escaping quotes can get messy.
I can't send objects through jsp includes.
I was thinking of using Gagawa
Please don't use scriptlets in JSP. You should use tag files, JSTL and EL to make your own tag library. This way you can easily pass variables as parameters into the reusable components, unlike with JSP fragments and these are much simpler than writing custom tags, when you're dealing with simple logic like looping or creating a table.
Below is an example based on your sample JSP code in the question.
/WEB-INF/tags/student/select.tag:
<%# taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%# attribute name="studentList" required="true" type="java.util.List" %>
<select class="combobox">
<c:forEach var="student" items="${studentList}">
<option value="${student.id}">
<c:out value="${student.lastName}" />,
<c:out value="${student.firstName}" />
</option>
</c:forEach>
</select>
sample.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix=”student” tagdir=”/WEB-INF/tags/student” %>
<!DOCTYPE html>
<html>
<head>
<title>Students</title>
</head>
<body>
<form>
(other inputs)
...
<student:select studentList="${sessionScope.studentList}" />
...
(other inputs)
</form>
</body>
</html>
To avoid escaping issues.
Better try using plain javascript to create html like
var el = document.createElement('select');
el.className = "combobox";
for (Student studentObj:studentList) {
var optel = document.createElement('option');
optel.value = studentObj.getId();
optel.text = studentObj.getLastName() + ", " + studentObj.getFirstName();
el.append(optel);
}
return el;

Categories

Resources