I'm trying to get Thymeleaf to build me a URL where the domain part is a parameter, some fragment is a literal string, and the query parameters are also parameterized.
The documentation offers some examples:
#{${myDomain}/literalUrl}
#{${myDomain}'/literalUrl'}
#{/literalUrl(query=${queryValue})}
#{'/literalUrl'(query=${queryValue})}
#{${myDomain}(query=${queryValue})}
or even
<a th:with="baseUrl=${myDomain}" th:href="#{${baseUrl}}(query=${queryValue})}">
Separately, all of these work well. But if I try to combine them, the domain part suddenly refuses to resolve:
#{${myDomain}/literalUrl(query=${queryValue})} and #{${myDomain}+'/literalUrl'+(query=${queryValue})} each resolve to ${myDomain}/literalUrl?query=queryValue, and
How do I get Thymeleaf to properly generate my url https://example.com/literalUrl?query=queryValue
Don't know if this is a legit solution for your problem, but if you concat the literalUrl with the first parameter, it will work. Down side: you need an additional model parameter.
<a th:href="#{${linkData+path}(q=${queryParam})}">some link</a>
gets
some link
with model params:
mv.addObject("linkData", "https://example.com");
mv.addObject("path", "/literalUrl");
mv.addObject("queryParam", "queryValue");
Related
i have a Thymeleaf template for email, and I am trying to pass two parameters to create an URL.
The part I am talking about looks like this:
<p><span>To accept the protocol click <a th:href="#{http://localhost:8080/accept/(invoiceId=${invoiceId}),(contractorEmail=${email})">HERE</a></span></p>
When I run the function that should create and send an email, I get:
Could not parse as expression: "#{http://localhost:8080/accept/(invoiceId=${invoiceId}),(contractorEmail=${email})" (template: "mailTemplate" - line 7, col 47)
How can I pass both parameters to the URL?
This is all documented in Thymeleaf's Standard URL Syntax.
#{http://localhost:8080/accept/(invoiceId=${invoiceId},contractorEmail=${email})}
I have a strange problem
this is what I have in routes files
GET /path/list controllers.path.getPaths()
GET /path/:id controllers.path.get(id:Int)
when I try to go <domain>/path/list the following error shows up:
For request 'GET /path/list' [Cannot parse parameter id as Int: For
input string: "list"]
I also tried to change the order in routes file
GET /path/:id controllers.path.get(id:Int)
GET /path/list controllers.path.getPaths()
I still get the same error. so my question is
isn't route supposed to match the first path that matches?
what else could be the problem (e.g. java codes)?
From the code you've provided this should work. The routes are not ambiguous because (from Play documentation):
Many routes can match the same request. If there is a conflict, the first route (in declaration order) is used.
if your routes ordering looks like this:
GET /path/list controllers.path.getPaths()
GET /path/:id controllers.path.get(id:Int)
/path/list will match before attempting to extract/transform the id parameter id:Int from the path and throwing.
If you want Play to transform the incoming parameter into a specific Scala type, you can add an explicit type
The only way this would not work is if you attempted to visit a route that did not match list or was not an Int:
For request 'GET /path/lists' [Cannot parse parameter id as Int: For input string: "lists"]
In my web application I'm trying to prevent users from inserting JavaScript in the freeText parameter when they're running a search.
To do this, I've written code in the header Velocity file to check whether the query string contains a parameter called freeText, and if so, use the replace method to replace the characters within the parameter value. However, when you load the page, it still displays the original query string - I'm unsure on how to replace the original query string with my new one which has the replaced characters.
This is my code:
#set($freeTextParameter = "$request.getParameter('freeText')")
freeTextParameter: $freeTextParameter
#if($freeTextParameter)
##Do the replacement:
#set($replacedQueryString = "$freeTextParameter.replace('confirm','replaced')")
replacedQueryString after doing the replace: $replacedQueryString
The query string now: $request.getQueryString()
The freeText parameter now: $request.getParameter('freeText')
#end
In the code above, the replacedQueryString variable has changed as expected (ie the replacement has been carried out as expected), but the $request.getQueryString() and $request.getParameter('freeText') are still the same as before, as if the replacement had never happened.
Seeing as there is a request.getParameter method which works fine for getting the parameters, I assumed there would be a request.setParameter method to do the same thing in reverse, but there isn't.
The Java String is an immutable object, which means that the replace() method will return an altered string, without changing the original one.
Since the parameters map given by the HttpServletRequest object cannot be modified, this approach doesn't work well if your templates rely on $request.getParameter('freeText').
Instead, if you rely on VelocityTools, then you can rather rely on $params.freeText in your templates. Then, you can tune your WEB-INF/tools.xml file to make this parameters map alterable:
<?xml version="1.0">
<tools>
<toolbox scope="request">
<tool key="params" readOnly="false"/>
...
</toolbox>
...
</tools>
(Version 2.0+ of the tools is required).
Then, in your header, you can do:
#set($params.freeText = params.freeText.replace('confirm','replaced'))
I managed to fix the issue myself - it turned out that there was another file (which gets called on every page) in which the $!request.getParameter('freeText')" variable is used. I have updated that file so that it uses the new $!replacedQueryString variable (ie the one with the JavaScript stripped out) instead of the existing "$!request.getParameter('freeText')" variable. This now prevents the JavaScript from being executed on every page.
So, this is the final working code in the header Velocity file:
#set($freeTextParameter = "$!m.request.httpRequest.getParameter('freeText')")
#if($freeTextParameter)
#set($replacedQueryString = "$freeTextParameter.replace('confirm','').replace('<','').replace('>','').replace('(','').replace(')','').replace(';','').replace('/','').replace('\"','').replace('&','').replace('+','').replace('script','').replace('prompt','').replace('*','').replace('.','')")
#end
I'm working on an application, where the SIPSession generated by Mobicents has one of it's attribute coming as null. After digging through the source code, I found out that, the value returned from the SipSession#getId() method is nothing but the SessionKey.
The SessionKey internally uses 4 different parameters to generate the String representation of the key. Out of that, one of the attribute is: fromTag. You can look at the source code of SipSessionKey here. Now, I'm unable to understand, what exactly is that fromTag. When I saw the request which is being sent, there is definitely a vlaue in the From header of the SipRequest. The From header is in the form:
From: <tel:+xxxxxxxxx>
That's it. What is fromTag in there? Why I'm getting it as null?
According to section 8.1.1.3 of RFC 3261, the From header MUST have a tag parameter. This is one of the pieces of data used to identify the dialog. (The others are Call-ID and the tag on the To header, generated by the UAS.) One the examples shown in the RFC is:
From: sip:+12125551212#phone2net.com;tag=887s
When looking at the SIP message received by the Mobicents container, is there a tag parameter on the From header?
I am using a jQuery ajax command, which has the following data:
$.ajax({
type:"POST",
...
data:"e=f_s&es="+JSON.stringify(email)+"&fr="+str
...
})
Where (email) can contain special character, for example it can be a string:
!#$%'&+-/=?^`*{|}~ch!#$%'/=?*^`{|}#mail.com
The reason why I allow such characters, is based on the following question.
The problem is, at some point on the server (Java EE application), it is messing up. The special characters are not showing the boundaries of different request parameters. For example it is considering : '/ as a parameter.
I think I need to escape characters? (if yes how?)
What should I do to be able to send such a string from javascript to java ?
Use encodeURIComponent:
encodeURIComponent("!#$%'&+-/=?^`*{|}~ch!#$%'/=?*^`{|}#mail.com")
returning:
"!%23%24%25'%26%2B-%2F%3D%3F%5E%60*%7B%7C%7D~ch!%23%24%25'%2F%3D%3F*%5E%60%7B%7C%7D%40mail.com"