spring-mvcthymeleafspring-mvc-initbinders

Spring initBinder StringTrimmerEditor - exclude some fields


In my current application by using init binder-StringTrimmerEditor we are nullifying all the values which are empty from the view/templates. But now I want to remove one field(movielist) from being nullified as this particular field when I edit the form i.e., remove all the values in the movie-list and click save button controller is getting null value instated of empty string. I want it to be as empty String instead of null value.

How do I exclude the movielist from being nullified.

<form action="#" th:object="${CustomerForm}" th:action="@{customer/save}" method="post">
                        <input type="hidden" th:field="*{id}"/>                     
                         <textarea  th:field="*{movieList}"></textarea>
                        <div class="modal-footer">                         
                            <input class="btn-submit" type="submit" value="Save"/>
                        </div>
                    </form>
 

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(emptyAsnull:true));
    }

Solution

  • You can look at this question . You might have to set the allowed fields value for your databinder which will also solve a potential security concern.