set value of jquery variable equal to a jstl variable - spring

I have a jstl value,
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/></c:forEach>
I want to set the variable in jquery equal to the var = "c".
How could I do that?

<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
You might need to JS escape it, quote it, etc. depending on what's actually in the variable, which you don't mention. It's also not clear what "a variable in jQuery" is.
The bottom line is that if the JavaScript lives in a JSP, just use the value: the JS isn't evaluated until the response has been emitted, so mix-and-match, but be aware that it's pretty easy to create invalid JavaScript unless you take care to properly escape whatever value types you're emitting.
As an example, consider a Java String that contains a single quote: if you single-quote the Java value in JS you'd have invalid JS because you didn't JS-escape the string value.

the method given in the below code might be correct but the variable whatever would have only one value that also only during the last count of the for each loop would exist
<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
for instance if the loop for the variable detail is of list type that consists of string type objects in it like "A" , "B" ,"C" then in the case the value of the var whatever will be "C", this is because jstl is compile time language.
So the above code won't work in for each loop
the following code might help you out or atleast provide you a idea for same.
<script>
var whatever = new Array;
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
for (var i=0;i<whatever.length;i++)
{
whatever[i]=${c};
}
</c:forEach>
<script>

Related

Concatenate two variables to check null condition using JSTL tags

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...)

how to get the data using XPATH from div with display:none?

I want to extract data from a div element with the attribute 'display:none'.
<div class='test' style='display:none;'>
<div id='test2'>data</div>
</div>
Here is what I tried:
//div[#class = "test"]//div[contains(#style, \'display:none\')';
Please help.
Try several changes:
1) Just put normal quotes around "display:none", like you did for your class attribute and close with ]
2) Then your div with class test and your style attribute is one and the same, so you need to call contains also for the same div:
'//div[#class = "test" and contains(#style, "display:none")]'
or the quotes the other way around, important is, that you are using differnt quotes around the expression than inside the expression
"//div[#class = 'test' and contains(#style, 'display:none')]"
if this still does not work, pls post an error message

scrapy xpath : selector with many <tr> <td>

Hello I want to ask a question
I scrape a website with xpath ,and the result is like this:
[u'<tr>\r\n
<td>address1</td>\r\n
<td>phone1</td>\r\n
<td>map1</td>\r\n
</tr>',
u'<tr>\r\n
<td>address1</td>\r\n
<td>telephone1</td>\r\n
<td>map1</td>\r\n
</tr>'...
u'<tr>\r\n
<td>address100</td>\r\n
<td>telephone100</td>\r\n
<td>map100</td>\r\n
</tr>']
now I need to use xpath to analyze this results again.
I want to save the first to address,the second to telephone,and the last one to map
But I can't get it.
Please guide me.Thank you!
Here is code,it's wrong. it will catch another thing.
store = sel.xpath("")
for s in store:
address = s.xpath("//tr/td[1]/text()").extract()
tel = s.xpath("//tr/td[2]/text()").extract()
map = s.xpath("//tr/td[3]/text()").extract()
As you can see in scrappy documentation to work with relative XPaths you have to use .// notation to extract the elements relative to the previous XPath, if not you're getting again all elements from the whole document. You can see this sample in the scrappy documentation that I referenced above:
For example, suppose you want to extract all <p> elements inside <div> elements. First, you would get all <div> elements:
divs = response.xpath('//div')
At first, you may be tempted to use the following approach, which is wrong, as it actually extracts all <p> elements from the document, not only those inside <div> elements:
for p in divs.xpath('//p'): # this is wrong - gets all <p> from the whole document
This is the proper way to do it (note the dot prefixing the .//p XPath):
for p in divs.xpath('.//p'): # extracts all <p> inside
So I think in your case you code must be something like:
for s in store:
address = s.xpath(".//tr/td[1]/text()").extract()
tel = s.xpath(".//tr/td[2]/text()").extract()
map = s.xpath(".//tr/td[3]/text()").extract()
Hope this helps,

How to split number in a jstl variable

I have this value
<c:set var="string1" value="SS4444"/>
What I am try to achieve is to get the numbers only (4444) from the above variable. I can use substring, but the index value might change, ie it can be 'SS4444', 'S4444', 'SSS444', so cant rely on static index.
Thanks in advance
Split it with regex.. (substringAfter is not proper for this..):
<c:forEach var="string1" items="${fn:split(yourstring, '^\w*')}">
<c:set var="string2" value="${string1}"/>
</c:forEach>
You can try with replace function as:
<c:set var="strng" value="SS4444"/>
<c:set var="strng1" value="${fn:replace(strng,'S','')}"/>

jstl access a second array value with foreach index

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.

Resources