How to use spring:eval expression inside $.each jQuery - spring

After a ajax call the response received is a JSON String. I am able to list the data but was wondering if is it feasible to use spring:eval inside jQuery $.each? If someone has an example I would really appreciate. The line in question ends with "??"
Response received is JSON:
{"listOfData":[{"id":"XX","someValue":"James Bond"}]}
The rest of the code:
var obj = jQuery.parseJSON(JSON_String)
$.each(obj.listOfData, function (index, data) {
"<tr>" +
"<td style=\"padding: 3px;\">" +
(index + 1) +
"</td>" +
"<td style=\"padding: 3px;\">" +
<spring:eval expression="data.someValue" /> ??
"</td>" +
"</tr>" +
}

Depending on where this code is,
<td style=\"padding: 3px;\"><spring:eval expression=\"data.someValue\" /></td>
might work. If it doesn't, you could do the "eval" part server side and return ready-to-use datas from your JSON code.

If you are here then may be you are not on the right track. Try this solution. Works like a charm ... Solution 1 and similar solution Solution 2.

Related

ASP-Classic Get information from dynamic field

I have an old ASP-page that contains a form but for some reason, a part of the code doens't work anymore.
The code was working before because I write the content of the form in a database and there I have values from the past.
A short description of my page:
I have a [select multiple]-field and when you select one (or more) option I create another input field (for every selected option a new fied). The ID of this field is FSRow_ + an edited value of de selected option (no spaces, commas, ...).
When I submit the form I want to read the content of my new fields so I can write this information to my database.
An example:
Suppose I have two selected options, to make it easy: "OptionA" and "OptionB", then I create two new fields (from the source code):
<input name="FSRow_OptionA" id="FSRow_OptionA" type="text">
<input name="FSRow_OptionB" id="FSRow_OptionB" type="text">
When I submit my form, I have this code:
if request.form()
...
getSelectedValues = split(request("OptionList"), ",")
for each li in getSelectedValues
li = replace(li, "'", "''")
li = replace(li, "||", ",")
fsrow = trim(request("FSRow_" & replace(replace(replace(replace(replace(trim(li), " ", ""), "&", ""), "+", ""), ",", ""), "|", "")))
if fsrow <> "" then
[write to DB with dynamic field]
else
[only write selected item]
end if
next
When I look in my database. I have only the information of the selected items (so, my i'm going in my else-code). Not the content of my dynamic field. So I suppose the variable "fsrow" is always empty, even if my new field contains some text.
My questions:
How can I get the content of my dynamic input field?
Any idea what could be wrong because it was working in july 2017 and didn't change anything.
[Not needed anymore, found the solution] I've tried to set an alert or write to the console to debug but this isn't working. How can I debug when I'm in my "for each"-loop?
Edit/Update 1:
As commented by Ricardo Pontual I can use "Response.End" in combination with "Response.Write" before.
If I do this:
Response.Write(li)
Response.Write("## FSRow_" & li & " ## ")
Response.Write("->" & request("FSRow_" & li))
I get this result:
OptionA## FSRow_OptionA ## ->
But in my form/input field I wrote "test" in the field with id "FSRow_OptionA"...
Edit/Update 2:
With a non-dynamic input field I can perfectly read and write the value in my database.
So it's realy a problem with the dynamic field...
Found the problem. This was very old code, from years ago that wasn't written by me, and the problem was the position of < form> and < /form>
Old code (NOT WORKING)
<table>
<form>
<tr>
...
</tr>
</form>
</table>
Changed the code by this:
<form>
<table>
<tr>
...
</tr>
</table>
</form>
I changed the order of < table> and < form> and it works now!

FREEMARKER: avoid escaping HTML chars

Having a problem with freemarker output...
[#assign optionsHTML = ""]
[#list data as item]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#list]
so, if I do
<select>
${iptionsHTML}
</select>
the output from otions get html entities instead of actual html.... so
<option value=&quot .....
even if I do
[#assign optionsHTML = ""]
[#list data as item]
[#noescape]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#noescape]
[/#list]
tried even
<select>
${iptionsHTML?html}
</select>
but's even worse :(
Putting #noescape around #assign has no effect. Automatic escaping only applies to ${...}-s that are embedded directly into the static text (the HTML). So there's no escaping to disable inside that #assign.
?html is used to escape a string "manually". Like in your example you could write optionsHTML = optionsHTML + '<option value="${item.value?html}>${item.label?html}</option>', because you know that the value will be output non-auto-escaped later, and the ${...}-s inside the string literal aren't escaped automatically.
However, the best would be if you can organize your code so that things that generate HTML don't construct the HTML inside variables and then print the variable, but print the HTML directly into the output. That's what FTL is designed for.
So after trying stuff, I don't know what I've done wrong before, but clean, this way is working
[#assign optionsHTML = ""]
[#list data as item]
[#assign optionsHTML = optionsHTML + '<option value="' + item.value +'>'+ item.label + '</option>' /]
[/#list]
<select>
[#noescape]
${optionsHTML}
[/#noescape]
</select>
Like ddekany said, write something like this:
<select>
[#list data as item]
<option value="${item.value}">${item.label}</option>
[/#list]
</select>
I faced same problem in string with special chars.
In this example I have checknumber = "6547&6548"
which caused problem before using this #escape
the best and simple way to handle this as following code
<#escape x as x?html>${deposit.checkNumber}</#escape>

JSTL Concatenate String to be used in html

how would I concatenate a string so It can be rendered in HTML properly?
<c:set var="filter" value="${view}"/>
For example, I do something like this in JavaScript:
var view;
var sors;
var filterCriteria = view + "<br>";
if (sors != null)
{
filterCriteria = filter + "SORs: " + sors + ", ";
}
Then the answer is
${view} <br> whatever
or
<c:out value="${view}"> <br> whatever
The first one doesn't escape the HTML special chars in view (<, >, &, ', "), whereas the second one does (and transforms them to <, >, &, etc.)
If you want to output <br> as html tag you could use c:out as <c:out escapeXml="false" value="${view} <br>"/>

How do I target this with nokogiri?

How do I target the following...
Every href attribute, inside a li, inside a ul, inside the FIRST div with the class of "listing".
There are many div.listing divs on the page, and inside each there are like 1000 ul>li>a[href="http://whatever.com"]
I want to fetch all the http://whatever.com in only the first div.
I know I can use div[1] and I can use div[#class="listing"] but really I need to find out how to combine them, and I also need to know how you fetch the attribute and not the text
//*[contains(concat( " ", #class, " " ), concat( " ", "listing", " " )) and (((count(preceding-sibling::*) + 1) = 1) and parent::*)]//a/#href
I got this by marking up your question in HTML and using SelectorGadget

How to write an XPath query to match two attributes?

Following Question:
<div id="id-74385" class="guest clearfix" style="z-index: 999;">
Given above,
If I want a XPath expression with checks both id and class, can we do it w/ 'and' condition LIKE:
//div[#id='id-74385'] and div[#class='guest clearfix']
Is this correct way? My execution fails here... Please help!
//div[#id='..' and #class='...]
should do the trick. That's selecting the div operators that have both attributes of the required value.
It's worth using one of the online XPath testbeds to try stuff out.
or //div[#id='id-74385'][#class='guest clearfix']
Adding to Brian Agnew's answer.
You can also do //div[#id='..' or #class='...] and you can have parenthesized expressions inside //div[#id='..' and (#class='a' or #class='b')].
Sample XML:
<X>
<Y ATTRIB1=attrib1_value ATTRIB2=attrib2_value/>
</X>
string xPath="/" + X + "/" + Y +
"[#" + ATTRIB1 + "='" + attrib1_value + "']" +
"[#" + ATTRIB2 + "='" + attrib2_value + "']"
XPath Testbed:
http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

Resources