how to change the scriplet to JSTL display string array - java

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.

Related

use for each value in jsp tag

I have a foreach value in an iteration in a jsp page as shown
<c:forEach var="lists" items="${lists}" varStatus="theCount">
String val= ${lists.getVal() } ; //this is giving me error
</c:forEach>
https://www.tutorialspoint.com/jsp/jstl_core_set_tag.htm
<c:forEach var="list" items="${lists}" varStatus="theCount">
<c:set var="val" value="${list.val}"/>
<p>The value is: ${val}.</p>
</c:forEach>

how to add " span " <span> </span> to each words in the arraylist?

I have one arraylist
ArrayList al = new ArrayList();
al.add("tree good has");
al.add("ok go by");
al.add("see good");
al.add("dog");
how to add " span " to each words??? For example:
<li>
<span>tree</span>
<span>good</span>
<span>has</span>
</li>
<li>
<span>ok</span>
<span>go</span>
<span>by</span>
</li>
<li>
<span>see</span>
<span>good</span>
</li>
<li>
<span>dog</span>
</li>
what i tried was:
the result of this statement was wrong. because the result is add " span " to each row , not earch word.
<% for (int i=0; i< al.size();i++){ %>
<li><span><%=al.get(i) %><br></span></li>
<%} %>
how to add " span " to each words???
You need further split each line into words. Find below code -
<% for (int i=0; i< al.size();i++){
%>
<li>
<%
for(int j=0;j<al.get(i).split(" ").length;j++){
%>
<span><%=al.get(j) %><br></span>
<% }%>
</li>
<%}%>
You can use the split method and can add the inner loop to achieve the desired result. it will look like
<% for (int i=0; i< al.size();i++){
String[] splitArray = al.get(i).split();%><li>
<% for (int j=0; j< splitArray.length;j++){ %>
<span><%=splitArray[j] %></span>
<%} %>
</li>
<%} %>
<% for (int i=0; i< HighlightTermList.size();i++){ %>
<li> <%String str=HighlightTermList.get(i); %>
<% String[] splitArray = str.split(" ");
for(int j=0;j<splitArray.length;j++){ %>
<span><%=splitArray[j] %></span>
<%} %>
</li>
<%} %>
I combine two answer together. I worked
You can solve this problem with JSTL. JSTL is better way to using java operations in jsp page. First off all you need to import required libraries to your jsp.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<ul>
<c:forEach items="${al}" var="mWord">
<li>
<c:forEach items="${fn:split(mWord,' ')}" var="word">
<span>${word}</span>
</c:forEach>
</li>
</c:forEach>
</ul>
</body>
</html>
You can learn detailed differences and advantages between JSTL and JSP Scriptlet in this link.

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.

How to dynamically populate values in expression language?

<%for(int i = 1; i < pl.getNoOfSegments(); i++){
%>
<list:column column="segment<c:out value="${i}"/>_value" titleKey="${pickList.pickListSegment.segment1Label}" searchType="input" type="nstring"/>
<%
}
%>
Here in the above scenario I want to populate the value of the column and titleKey but I am unable to use the ${i} EL and it doesn't giving any terminated error. For titleKey the segment1Label ie 1 I want to replace with I value but not able to do so?
I'm new to EL/scriplets as well so this might not be the best way to do it.
What you can do is define a var and increment it manually so it would have the same value as i.
<c:set var="count" value="1" scope="page" /> //define your variable here
<%
for(int i=1;i<pl.getNoOfSegments();i++){
%>
<c:out value="${count} test"/> // ${count} here has the same value as i
<c:set var="count" value="${count + 1}" scope="page" /> //increment count
<%
}
%>
Alternatively, instead of using Scriptlets, you could look into using JSTL tags such as:
<c:forEach begin="1" end="${passedAttr}" varStatus="loop">

How to pass an ArrayList from one jsp to another using session

I'm trying to pass an ArrayList from handle.jsp to main.jsp, but it doesn't let me do it. It keeps saying "Type mismatch: cannot convert from Object to ArrayList".
main.jsp:
<%# page import="java.util.ArrayList" %>
<html>
<body>
<h1>Hobby Manager</h1>
<%
ArrayList<String> hobbies = session.getAttribute("hobbies");
out.println(hobbies.size());
out.println(session.getAttribute("hobbies"));
%>
<h2>Add new hobby!</h2>
<FORM action="handleAddHobby.jsp" method="get">
What new hobby are you wishing to add? <INPUT TYPE=text name=hobbyName /> <br/>
<INPUT TYPE=submit name=addHobby value="Add Hobby" />
</FORM>
</body>
</html>
handle.jsp:
<%# page import="java.util.ArrayList" %>
<html>
<body>
<%
ArrayList<String> hobbies = new ArrayList<String>();
String hobbyName = request.getParameter("hobbyName");
if(hobbyName == null){
out.println("Please enter a hobby before clicking add! Dummy.<br/>");
}
else{
hobbies.add(hobbyName);
for(int index = 0; index < hobbies.size(); index ++){
out.println(hobbies.get(index) + "<br/>");
}
session.setAttribute("hobbies", hobbies);
}
%>
</body>
</html>
I have tried passing it as a string object, and passing it as an object alone but nothing seems to work.
the problem is here ..
ArrayList<String> hobbies = session.getAttribute("hobbies");
Try typecasting it as getAttribute always returns Object.
ArrayList<String> hobbies = (ArrayList<String>)session.getAttribute("hobbies");

Categories

Resources