th:href Thymeleaf redirect url with path variables and object id's with Spring MVC - spring

So I'm trying to redirect the entire url with a th:href, but it is adding characters that I don't want.
My current url is this
http://localhost:8080/viewCourse/post/5
And I'm trying to backtrack to the course the post was a part of, which is
http://localhost:8080/viewCourse/1
So currently this is what my html looks like
<a th:href="#{'/viewCourse/'(${post.course.id})}"><span th:text="${post.course.name}"></span></a>
And this is the url I get
http://localhost:8080/viewCourse/?1
And the Id is correct, but I'm not sure why the ? is there.
I've also tried this
<a th:href="#{'/viewCourse/'(id=${post.course.id})}"><span th:text="${post.course.name}"></span></a>
Which gives me this
http://localhost:8080/viewCourse/?id=1
If anybody can see how I can fix this and let me know that would be great, thanks in advance.

You can achieve adding id without question mark by String concatenation
<a th:href="#{/viewCourse/} + ${post.course.id}"><span th:text="${post.course.name}"></span></a>
However I would recommend to study this answer https://stackoverflow.com/a/14938399/5900967
As this can fail in some contexts

Apparently your id was added as a parameter.
Your code should be like this:
<a th:href="#{/viewCourse/{id}(id=${post.course.id})}"><span th:text="${post.course.name}"></span></a>
And the output should be like this:
http://localhost:8080/viewCourse/1
To learn more about thymeleaf url syntax, see https://www.thymeleaf.org/doc/articles/standardurlsyntax.html

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.

preg_match title and ID

Our script uses preg_match to determine if the url is correct and if it's not it throws a 404, except we want our urls to look like this:
http://www.my-domain.com/my-post-title-here/my_id_here/
When it originates as this:
http://www.my-domain.com/my-post-title-here-my_id_here.html
So we tried to change the preg_match to the following but its not working,
preg_match('(([a-z]+)/([0-9]+))/?', request_uri(), $matches);
any help would be appreciated!
Well we got the preg_match to work using the following,
preg_match('#([a-z])/(\d+)/$#i', request_uri(), $matches);
But we still cant get it to restrict to just the seo title, for example.
If the url looks like this
http://www.my-domain.com/my-post-title-here/my_id_here/
You can still load the post by replacing the post title with anything you want, which could result in duplicate content issues!
How can we restrict it to just the title of the post as that's how we generate the url??

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.

Codeigniter's redirect is not passing variable data

I have a form to update some profile information. After making changes when the user clicks on the submit button my to be redirected to the profile page.
so after inserting the updated information into database I have the following code to redirect to the profile page:
database query goes here...
redirect('studentprofile/get/$id');
Here the $id is the the profile id and "get" is the function and "studentprofile" is the controller. But this is not working at all. It seems like the "redirect" is not getting the value of $id.
When the user submits data it shows an error saying "The URI you submitted has disallowed characters." and the URL looks like this
http://localhost/sundial/studentprofile/get/$id
would you please kindly tell me what is wrong with my script? Just for your information I am using Codeigniter
Thanks in advance :)
You need the redirect path string to be in double quotes:
redirect("studentprofile/get/$id");
or write it like this:
redirect('studentprofile/get/'.$id);
You are using single quotes use single quotes instead and btw that is not an issue of CodeIgnitor. Read more about strings: http://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.double

how spring mvc tag works?

I am trying to write some kind of raw html to mimic what spring mvc tag produces after page rendering(and I do make them look exactly the same if you open them with a html element inspector). as I want to create dynamic input form using javascript. but it didn't work. it seems I had to use only what it offers: e.g. <form:input path="firstName" />.
to get the data binding working.
I thought the tag lib only help you to produce a html block that spring knows how to handle them in backend (action). from a web http perspective. what else it can send beyond a bunch of form data, and they should send the same thing. so I am really curious to learn what magic thing the tag lib dose beyond producing a html block.
the other thing I would like to know is where the model object is being hold when the form is being submit to a matched action. you know, you can get the model attribute by using #modelAttribute as the input parameter. is it in the original request object? or in the ActionRequest to which the dispatcherServlet build and put it. or even somewhere else?
thanks in advance.
I got it figured out. the raw html just works as spring tag does. as long as you are in the form tag block. you are okay to use raw html such as
<input type="text" id="abc" name="abc"/> just make sure name reflect your bean attribute path.
id is not mandatory and is just helping you to identify the very element. I guess I missed something when I work with the raw html by the time I ask the question. hope this helps for guys working with raw html approach, especially in case of dynamic input creation.

Resources