http://localhost:8080/#%7B/author/(id=1)%7D - spring

Please tell me why when I click on the link:
<td> Перейти</td>
I get the address in the url : http://localhost:8080/#%7B/author/(id=1)%7D?
The form that opens by clicking on the link: form action="#{/author/(id=${book.id})}" method="get"

try this :

href="/author/${book.id}"

why when I click on the link
The URL specified in template is #{/author/(id=${book.id})}.
${book.id} is resolved to 1 by Thymeleaf as this notation is consistent with Thymeleaf schema. Now, Spring URL resolver would see the final URL as #{/author/(id=1)}.
Spring follows RFC3986 for encoding and decoding URLs.
URI percent encoding of { is %7B.
URI percent encoding of } is %7D.
Hence, Spring will encode #{/author/(id=1)} to #%7B/author/(id=1)%7D.
This is the reason why #{/author/(id=${book.id})} in your template translates to #%7B/author/(id=1)%7D for you.

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.

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

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

Spring 4 #RequestMapping handling REST URL with special characters

How does Spring 4 handle the REST URL with special character, such as #?
For example : http://localhost:8080/#/members/browse?id=1234
The following code seems not working.
Because Spring always discards everything after (including) #, and just returns the response of http://localhost:8080
Could anyone give us some help?
#RequestMapping(value = "/#/members/browse", method = RequestMethod.GET, produces = "application/json")
public String findMembers(#RequestParam(value="id", required=false) {
// some code
}
This is the expected behavior. It's a fragment identifier and it's not meant to be used for routing requests on the server side.
In your browser, clicking on another anchor link like #/members/browse?id=5678 in the same page looks for an anchor within the page and does not send a request to the server.
I think you're probably using a javascript framework such as Ember or Angular, which provide routing components to route those events on the client side and render views. See the router documentation for Ember.

Spring JSP variable not assigned

I have in my JSP page code like this:
<spring:url value="" var="url"/>
EN
And issue is that parameter url in link is always set to empty String.I would expect that if I type url like localhost:8080/test the url variable will hold this value and it will be replaced in link so it would look like /change_locale?locale=EN&current=test. However it is always generated like /change_locale?locale=EN&current=.What I am doing wrong? Best regards
In
<spring:url value="" var="url"/>
Your value value is the empty String. Because of this, the URL is relative.
Spring uses UrlTag to construct the value from a <url> tag. You'll want to take a look at its createUrl method in the source code if you're curious.
In this case, it will generate a value that is the empty String and store it in a page scope attribute named url. That's what you get when rendering
${url}

Need to `location.href` value in JSTL(or JSP)

All I need is just <script>location.href</script> value in JSTL(or JSP).
Just same as the web browswers display. But it's not that easy.
I'm using Tiles2 and request.getRequestURL() shows tiles base jsp location like /WEB-INF/tiles/base.jsp...
And I found <%=request.getAttribute("javax.servlet.forward.request_uri")%> shows what I want.
https://stackoverflow.com/a/11387378/411615
But still it's not enough. it's not showing protocol and domain.
And I searched again.
http://${pageContext.request.localName}<%=request.getAttribute("javax.servlet.forward.request_uri")%>
It looks dirty and does not contain parameters. I would make it like this...
Map<String, String[]> parameters = request.getParameterMap();
for(String parameter : parameters.keySet()) {
...
}
But it's weird. In javascript it's so simple, but in JSP it's very tough.
Is there any neat way?
My environment isSpring 3.1, Tiles2.
The Request object is all you get on the server side. There's no way to know what was in the original href link because the browser sends the fully assembled URL to the server. Your options are 1) use some javascript trickery on the client side to send the original href as a parameter or header value, or 2) poke around in the Request object to get what you need. 2) is better.

Resources