Search code examples
javaspring-mvcthymeleaf

"readonly if parameter" is not working in thymeleaf


If the current url have the parameter "action=edit" I want some controls marked as readonly.

I've tried the following:

URL: http://x.x.x.x:8080/department?id=000001&action=edit

<input type="text" th:placeholder="#{department.id}" class="form-control" th:field="*{id}" th:readonly="${param.action == 'edit'}">

This is always false for some reason. I've also tried showing the value:

<input type="text" class="form-control" th:value="${param.action}" th:readonly="${param.action=='edit'}">

This show the value 'edit' in the input but readonly is still false. Then I tried the following: "${param.action != 'edit'}" and this is working. Why? param.action is equal to 'edit' but that IF is taking some different value.


Solution

  • ${param.action} is an array (in order to support multivalued parameters). In order to get the value, you'll probably have to do something like this:

    ${param.action != null AND param.action[0]=='edit'}
    

    (You can skip the null check if the action parameter will always appear, otherwise you'll run into null pointer exceptions.)