JSTL String concatenation - How can we achieve this? - jstl

In my JSP.. I am now accessing a session scoped variable using ${sessionScope.var1}, $sessionScope.var2}, but i want this 1,2 .. so on, in the var1, to be appended from jstl like ${page} which gives me 0,1,2.. so on.. I tried ${sessionScope.var{page}}, also ${sessionScope['var${page}'] and other things.. but still i am not able to get it.. So, can someone please help me out in this isuue..

try this ;)
<c:set var="myVar" value="var${page}" />
${sessionScope[myVar]}

Related

Is it possible to single out a json object and set it as another object only using JSTL?

I'm new to Jstl. Hope this question is not a duplicate.
Say, if there's a json object like below,
jsonObj = {"name":"John", "age":30, "car":null}
what I would like to do is take "John" from name and set it as value for userName.
So what I tried is <c:set var="userName" value="${jsonObj}.name" scope="page" />.
If I print userName with command <script>console.log(${userName})</script>, I can see John on the console panel.
However, if I look through the Sources in developer tools, I find that whole jsonObj value showing on every objects that I created from jsonObj.
Am I asking for something that's not available?
If not, any advice would be grateful no matter how small they are.
Thank you in advance.
<c:set var="userName" value="${jsonObj}.name" /> is equivalent to something like:
userName = jsonObj.toString() + ".name";
Assuming jsonObj is an org.json.JsonObject, you can access the content of your Json with:
<c:set var="userName" value="${jsonObj.getString('name')}"/>

ColdFusion Proxy Server

I'm trying to create a basic proxy server so I can keep track of what my kids are doing web wise - I know there are products out there but I thought it would be an interesting exercise to write one myself.
I have the following code that kind of works but doesn't pull any images or css through - I guess because it makes another call to the remote server and gets confused
<cfhttp url="https://www.bbc.co.uk">
<cfhttpparam type="header" name="Proxy-Connection" value="keep-alive" >
<cfhttpparam type="header" name="Accept" value="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5">
<cfhttpparam type="header" name="Accept-Language" value="en-US,en;q=0.8">
<cfhttpparam type="header" name="Accept-Charset" value="ISO-8859-1,utf-8;q=0.7,*;q=0.3">
</cfhttp>
<cfset html = cfhttp.FileContent />
<cfoutput>#html#</cfoutput>`
What am I missing?
What you want to use is the resolveurl parameter of - set it to yes/true. This defaults to no.
What this parameter does is resolves relative paths to absolute paths automatically for you.
Now, if you want to change those paths as well, you would change them to also route through your http proxy script, but there won't be much use as you won't know much about the content regardless.
resolveurl should hook you up with what you are looking for. cheers.
https://cfdocs.org/cfhttp (look for resolveurl tag attribute)

How to escape '-' in SpEL in thymeleaf

In my Spring Controller I set following to my model attribute:
model.addAttribute("abc-def", "Hello World");
In my thymeleaf html I want to read the value of abc-def.
<th:block th:text="${abc-def}"></th:block>
But I get the error:
The operator 'SUBTRACT' is not supported between objects of type 'null' and 'null'
Its clear because - is an arithmetic operator. Is there a way to escape - for reading out the model value?
My advice would be: don't use variables names with dashes in them. (Would you try to define a variable int abc-def = 5; in java?)
In any case, this seems to work if you have to use it:
<th:block th:text="${#request.getAttribute('abc-def')}" />
Thymeleaf 2
Per the Expression Basic Objects section of the documentation (with more details in Appendix A), the context variables are in a #vars object. So, you can access variables with something like this:
<th:block th:text="${#vars.get('abc-def')}" />
Thymeleaf 3
As Metroids commented this all changes in Thymeleaf 3. It combines the #ctx and #vars objects, so you need to use the Context's getVariable method:
<th:block th:text="${#ctx.getVariable('abc-def')}" />
But this isn't the best plan
While certainly these will "work", having variables with punctuation in them is a bit unusual, and may confuse the next programmer to see your code. I wouldn't do it unless I had a really good reason to use that name.

ImageScaleToFit - how to implement?

I'm new to Railo, moving away from ColdFusion 8 where my site used to used to use cfx_imagecr3.
I believe Railo has ImageScaleToFit but I'm not sure if I'm using it correctly or if I need to add some kind of class/component? I've added it between a cfscript but i get 'invalid variable declaration' which doest make any sense to me. Any help appreciated.
I'm running railo-4.0.0.013 on ubuntu 12.04
<cfscript>
imageScaletoFit('/home/images/testimage.jpg','90','114','highestPerformance');
</cfscript>
You need to first load the image into a variable, then use that variable name inside ImageScaletoFit, like so:
<cfimage name="ImgVariable" source="/home/images/testimage.jpg" />
<cfset imageScaletoFit(ImgVariable,'90','114','highestPerformance') />
(This isn't specific to Railo - the function exists and works the same way in CF8+)

G:textfield remote function advice needed

i need advice on groovy. My remote function's not working. here is my codes. please advice.
<g:textField name="name"
onchange="${remoteFunction(action:'validateName',update:'errorName', params:'\'name=\' + this.value' )}"/>
The remote constraints plugin does most of the work for you.
And, if you want to use only the ajax function you have to use the "remoteConstraints" tag:
<g:remoteConstraints
beanName="Test"
fieldName="name"
template="/shared/error"
var="errorBean"
updateElement="errorsDiv"/>

Resources