Thymeleaf - th:ref convet & to & - spring-boot

I'm using Thymeleaf.
I've a link build with th:ref
<a th:href="#{http://www.pippo.com/a=3&y=4&u=10}">
That is converted into
http://www.pippo.com/a=3&y=4&u=10
So & is converted into &
How can i convert & to & ?
Thanks

th:href is meant to be used for relative urls or urls that come (partly) from a model variable.
This Means that in your case with a static url you should be using regular href making
A secondary issue might be using & as part of the url, this character is normally reserved for url paramaters and might not behave like you expect it to if you use it as an url part.
Looking at your url I would expect it to look like http://www.pippo.com?a=3&y=4&u=10 in thymeleaf this could be #{http://www.pippo.com(a=3,y=4,u=10)}

Related

Get full URL with parameters in Thymeleaf?

I've got a paginated list of cars, on a Spring Boot server, with the parameters sort, range, desc, page, etc to filter and sort by and am generating the URL in Thymeleaf that looks like:
example.com/cars?page=5&sort=mileage
I am wanting to be able to add more parameters to a URL with a few of these already but I'm quite new to this and don't really know how to get the current URL with all the parameters to add more params without losing the previous ones look like
example.com/cars?page=5&sort=mileage&desc=true
I've found an answer to do something like this on Spring but would ideally want to do it on the Thymeleaf template, is this possible?
Get full current url thymeleaf with all parameters
I found that you can get hold of specific parameters in Thymeleaf using ${param.sort} to get hold of the param sort, could something similar to this get hold of all the params currently?
Thanks
If someone is still looking for a thymeleaf template only solution, you could use ${#request.getRequestURI()} with ${#request.getQueryString()} and add your additional parameters via concatenation:
<a th:href="#{${url}}" th:with="url=${#request.getRequestURI()+'?'+#request.getQueryString()+'&foo=bar'}">Link</a>
If you need to escape query parameters, you can use #uris.escapeQueryParam():
<a th:href="#{${url}}" th:with="url=${#request.getRequestURI()+'?'+#request.getQueryString()+'&foo='+#uris.escapeQueryParam('b a r')}">Link</a>
Some further details:
You have to use th:with, otherwise the parser will throw TemplateProcessingException: Access to request parameters is forbidden in this context. in newer thymeleaf versions.
It also works when the current query is empty, the url generator will create a valid url, in my example including one ? and no & in the query part.

Is there a DNN URL decode method as it is changing a URL Encode

We are using Server.URLEncode to change an SKU with a forward slash from BDF5555/45 to BD5555%2F45 in our Href on a button.
When a person clicks on the button the page navigates to another module which has Request.QueryString but DNN is changing the URL.
How can I get the variable decodeprodCode to include the &45 as BDF5555/45?
Perhaps DNN is rewriting the URL?
There is a NavigateURL class in DotNetNuke.Common.Globals that will generate a correct url based on a TabID and a lot of overloads.
DotNetNuke.Common.Globals.NavigateURL(TabId)
You can also use querystring parameters
DotNetNuke.Common.Globals.NavigateURL(TabId, ControlKey,"key=value"))
DNN by default will re-write querystring values into /key/value format in the URL assuming that it is properly encoded. For example if you have querystring values of sku=1 and productid = 2 the resultant URL will be
https://yoursite.com/Your-Page/sku/1/productid/2
This is done via the FriendlyUrlProvider, but it should not have any impact to your ability to process via Request.Querystring as this is a very, very common practice for passing values into DNN.
Your code to retrieve the value is correct.
I ended up using the following code instead of a Request.Querystring.
string RawurlFromRequest = Request.RawUrl;
var cleanSKU = RawurlFromRequest.Split(new[] {"sku/"}, StringSplitOptions.None)[1];
var CleanSKUNoOtherQueryStrings = cleanSKU.Split(new[] {"&"}, StringSplitOptions.None)[0];
The Request.RawURL brings back the URL with the special characters as it is without encoding. As Mitchel Sellers mentioned above, DNN uses the FriendlyURLProvider which rewrites the URL.
For example www.mysite.com/ProductFilter/SKU/BDF5555/45 and not
www.mysite.com/ProductFilter/SKU/BDF5555%2F45
The CleanSKU variable will look for SKU/ and split everything on the left as it is set to [1].
After everything has been split on the left, we look for other QueryStrings which we usually add with a & sign. We will split everything on the right by setting it to [0].
This will bring back BDF5555/45 in the backend with a forward slash which we can use to retrieve product information from our ERP system which URLdecode couldn't do.

Unable to get HTML tags in RequestParam (MVC)

I am trying to send a get request through an MVC form but when I get the value in the controller using RequestParam somehow the HTML tags like <li> etc are getting filtered, any suggestion so as where these tags are getting filtered and any workarounds for them.
Thanks.
You can try using some URL Encoding like this %3Cli%3E%3C%2Fli%3E . If this is not working, you can try a hardcode encoding when sending and decoding when receiving. For example replace "<" with "|OPEN_BRACKET|" and ">" with "|CLOSE_BRACKET|" . Keep in mind, that this is a bad solution and use it as a last resort. Also make some checks for XSS attacks.

Tuckey URL Rewriting => Issue with ? and & chars

I'm currently trying to use Tuckey and Tomcat to rewrite URLs.
At the moment I'm trying to translate
myapp.com/track/2340ddkef?dkfkeif&3434
to
myapp.com?req=track&id=2340ddkef?dkfkeif&3434
But after the first ? or & the parameter is cut off and only
myapp.com?req=track&id=2340ddkef
is send to the servlet. How can I change that behavior? Is that even possible with Tuckey or do I need to apply some filter which to ensure the characters are not lost?
Embedding ? and & in URLs like that really isn't valid. You should URL encode those strings. For example, in Java, use
URLEncoder.encode(id, "UTF-8")

User Agent String - provide as variable

I need to find the user agent string, which it looks like UserAgentSTring.com does for me but I need to store that so I can pass it to the server side. I am not sure how to accomplish this?
The API docs say this:
You can send a ua string as post or get request (form field or in the query string).
Use 'uas' as parameter name:
?uas=Opera/9.70%20(Linux%20i686%20;%20U;%20en-us)%20Presto/2.2.0
this will automatically parse the string. To get some data you have to add one more parameter:
But if I put that line in my source code, in the header of my page how will the server side recognize it? Any assistance would be great.
If you are using PHP you can just use $_SERVER['HTTP_USER_AGENT'] but if that is not what you are looking for, look into HTML forms. Putting it in the header of your source code will do nothing for the server if it is not in PHP/ASP
Server-side approach with PHP:
<html>
<body>
Hi! You're using:
<?php
$jsonText = file_get_contents("http://useragentstring.com/?getJSON=all&uas=" + urlencode($_SERVER['HTTP_USER_AGENT']));
$obj = json_decode($jsonText, true);
echo $obj['agent_name'] . " " . $obj['agent_version'] . " on " . $obj['os_name'];
?>
Lol.
</body>
</html>
A client-side approach with Javascript is a bit more complex and you are better off not using useragentstring.com for this but using the browser's built-in mechanisms, like here.

Resources