I want to add selected to HTML tag option using Thymeleaf dynamically by checking the condition of RequestParam:
<option value="en" th:text="${#httpServletRequest.getParameter('lang') == null || #httpServletRequest.getParameter('lang') == 'en'} ? #{lang.eng}"></option>
<option value="ru" th:text="#{lang.ru}" th:attr="${#httpServletRequest.getParameter('lang') == 'ru'} ? 'selected' : null"></option>
But I have an error on the second option.
The reason for doing it because when I switch to another language the selected lang doesn't changing How I can solve this problem? Or I have to use another approach?
With selected
attribute you should refer to Fixed value boolean attributes.
That means instead of
th:attr="${#httpServletRequest.getParameter('lang') == 'ru'} ? 'selected' : null"
you can do:
th:selected="${#httpServletRequest.getParameter('lang') == 'ru'}"