I am currently working on a Spring project and I faced an issue with countries list.
The following list is a list of countries, I need to have a country with name emptySpace and id 0. the list is displaying countries successfully but this one country with empty space value is displayed by its id instead of its value, and if I full the empty space with any value, like "good job" it will show good job in the country list.
<c:forEach var="country" items="${country}">
<c:choose>
<c:when test="${country.entityId == companyDetails.countryId}">
<form:option value="${country.entityId}" selected="selected">${country.name}</form:option>
</c:when>
<c:otherwise>
<form:option value="${country.entityId}">${country.name}</form:option>
</c:otherwise>
</c:choose>
</c:forEach>
any suggestions?
You can try to use label attribute instead of tag body for passing label value:
<form:option value="${country.entityId}" label = "${country.name}" />
Since you are not using any feature of the spring form:option tag, try using the html <option> tag instead. I will not do any automatic substitutions.
Related
I am trying to send image of each user separately in ModelMap
model.addAttribute("profileImage"+i, sb); // I have added attribute in loop
Variable 'i' is an index for each image which I am iterating for each user.
Now I am Having profileImage0,profileImage1,profileImage2 and so on.
While Accessing I am putting some condition
<c:choose>
<c:when test="${profileImage!=null}">
<center><img src="${profileImage}" id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar">
</c:when>
<c:otherwise>
<center><img src="${pageContext.request.contextPath}/resources/images/defaultuserprofile.png" id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar"></center>
</c:otherwise>
</c:choose>
So in the Above Code I need to append some indexing to a variable which will first initialize the value of a variable then use it properly instead using like this one.
like profileImage0,profileImage1 and so on....
You can make use of the <c:forEach> tag to iterate over your variables. You may want to pass in the maximum number of values you have in your controller so you know how many values you need to pick up from your request:
<c:forEach var = "i" begin = "1" end = "${max}">
<c:set var = "variableName" scope = "session" value = "profileName + ${i}"/>
<c:set var = "profileImage" scope = "session" value = "${variableName}"/>
</c:forEach>
(or something like that...)
I got a small problem i want to add for example i have some values in my foreach which i recive from my database how can i add a br or a p because now its just in one value
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<p class="vactextl">
${vac.description}
</p>
</c:forEach>
I get now a text from database but i want to add a new line or break.
what i want the text is coming from a database its very long discription text i want to add breaks or paragraph in it but now i cant when i print it out there is no paragraph or anything just text is the are way to do this?
In my database is stored like this http://puu.sh/q73rp/b26d8f7014.png
But when i show it on html i get this http://puu.sh/q73uY/4026d4dc62.png
Is there a way to replace \n \r with br this would be fix the problem
Its obviously wrong way to do it like below but for now you can do it
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<p class="vactextl">
${vac.description}
<%out.println("<br>")%>
</p>
</d>
</c:forEach>
Note: out is jsp's implicit object
You're going to have to split your string if you want to output it with line breaks from the database. You can then try putting the split strings in an arraylist then output them in your forEach loop.
<c:forEach var="vac" items="${loc}">
<div class="featurette">
<c:out value="${vac.description}" /><br/>
</c:forEach>
I have implemented the if / else tag in JSTL like following. But its not working. If condition not checking.
<c:choose>
<core:if ${capital.nextCapital()} eq ${request.capital}>
<p> Yes. The capital of ${capital.nextState()} is ${capital.nextCapital()} </p>
</core:if>
<c:otherwise>
<p>No. The capital of ${capital.nextState()} is ${capital.nextCapital()} </p>
</c:otherwise>
</c:choose>
That is not valid syntax at all.
the standard prefix is c, not core
inside c:choose, you can use c:when and c:otherwise. Not c:if.
the boolean condition must be inside a test attribute:
Attributes must be surrounded by quotes:
<c:when test="...">
The whole boolean EL expression must be inside ${}:
<c:when test="${ ... }">
So the end result should be
<c:when test="${capital.nextCapital() eq request.capital}"> ... </c:when>
I suggest you re-read your book or tutorial about custom tags and the JSTL.
I’m using JBoss 7.1.3.Final and Spring 3.1.1.RELEASE. On my JSP page, how do I get all the ids of an array into a comma separated string? I have tried this:
<c:forEach var="subject" items="${category.subjects}" varStatus="status">
<c:if test="${status.index == 0}">
<c:set var="cateogrySubjects" value="${subject.id}"/>
</c:if>
<c:if test="${status.index > 0}">
<c:set var="categorySubjects" value="${subject.id},${categorySubjects}"/>
</c:if>
</c:forEach>
Unfortunately the last statement printed is always “subj1,subj1” in the array even if the intermediate values are “subj1”, “subj2”, “subj3”, etc. Any help on concatenating things would be great.
i have two arrays (strings delimited with commas) and i made a foreach loop on one of this vars
i need to be able to access to the other string with the foreach index like
<c:set var="name" value="Zara,nuha,roshy" />
<c:set var="name2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${name}" delims="," var="name" varstatus="i">
<c:out value="${name}"/><br>
</c:forEach>
i need to access name2 values, in the name foreach, is it possible without doing another foeach?
The varstatus variable you are using contains a value "index" that you can use.
But, you can't operate on a string like that (not that I know of at least).
First, you need to convert name2 to a proper array or list. Then you can access it inside the for loop:
${name2list[i.index]}
Now, how to convert it to an array? How about the split function?
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="name2list" value="${fn:split(name2, ',')}"/>
This can be done, contrary to some previous comments.
See the following example based on the question:
<c:set var="names" value="Zara,nuha,roshy" />
<c:set var="names2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${names.split(',')}" varStatus="i" var="name" >
${name} : ${names2.split(',')[i.index]}<br/>
</c:forEach>
Essentially we're using a string split function with expression language to get a string array from list comma separated values. Within the loop we the get the second value from the names2 array by using the varStatus index. I believe this accomplishes the task.