I have written request filter and locale resolver for getting language code from URL. (for example: DOMAIN/en/, DOMAIN/cs/)
However, I don't know how to change programmatically the context path that Thymeleaf uses for its context-relative URLs (@{/css/main.css}).
For example, if on page with address "DOMAIN/en/test/" is following code
<a th:href="@{/test2/}">TEST 2</a>
it points at
DOMAIN/test2/
instead of
DOMAIN/en/test2/
I thought it would be good to create some filter that edits the URL before it goes to Thymeleaf templates, but I don't have any idea how.
Do you have any ideas how to solve it?
I've found solution that suits my expectations.
I just wanted to insert language code after context path (example.com/CONTEXT_PATH/CONTROLLER -> example.com/CONTEXT_PATH/LANGUAGE_CODE/CONTROLLER) for Thymeleaf templates, so I can still use Thymeleaf's url expression @{/controller}.
I have url filter that removes language code and adds it to request's attributes, so I have just edited the response's encodeURL method and it works as I wanted:
getServletContext().getRequestDispatcher(newUrl).forward(request, new HttpServletResponseWrapper(response) {
@Override
public String encodeURL(String url) {
String contextPath = getServletContext().getContextPath();
if (url.startsWith(contextPath))
url = new StringBuilder(url).insert(contextPath.length(), "/" + getLocale().getLanguage()).toString();
return super.encodeURL(url);
}
});
Anyway, thanks for your answers! :)