Как игнорировать определенные поля в форме в тимелеафе?

Я создаю форму в тимелеафе, содержащую поле для загрузки файла, которое не является частью моей модели. Когда я загружаю страницу, тимелеаф жалуется и выдает NotReadablePropertyException для этого поля.

Как я могу заставить тимелеаф игнорировать тот факт, что поле не существует в модели?

Код:

<div class="form-group"
     th:classappend="${#fields.hasErrors('uploadFile')}? 'has-error'">
    <label class="col-sm-12 control-label" style="text-align: left; margin-bottom: 7px;">Upload Photo</label>
    <div class="col-sm-12" style="margin-bottom:5px;">
        <div class="fileinput fileinput-new input-group" data-provides="fileinput">
            <div class="form-control" data-trigger="fileinput">
                <i class="glyphicon glyphicon-file fileinput-exists"></i>
                <span class="fileinput-filename"></span>
            </div>
            <span class="input-group-addon btn btn-default btn-file">
                <span class="fileinput-new">Select file</span>
                <span class="fileinput-exists">Change</span>
                <input type="file" name="uploadFile" accept="image/*" required>
            </span>
            <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
        </div>
        <p id="error" style="position:absolute;color:#FF0000;margin-top:-7px"></p>
    </div>
</div>

Ошибка:

org.springframework.beans.NotReadablePropertyException: Invalid property 'uploadFile' of bean class [bean.Library]: Bean property 'uploadFile' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

person Trinopoty    schedule 07.01.2019    source источник


Ответы (1)


ошибка, которую вы получаете, связана с линией

th:classappend="${#fields.hasErrors('uploadFile')}

который ожидает выражение поля в качестве параметра.

Вы можете заменить

th:classappend="${#fields.hasErrors('uploadFile')}

с

th:classappend="${#fields.hasErrors('*')}

где класс has-error появится, если есть ошибка с любым из полей. Или вы даже можете заменить его на

th:classappend="${#fields.hasErrors('global')} 

который не связан с каким-либо конкретным полем в форме.

Или же вы можете добавить поле (uploadFile) в модель в качестве переходного атрибута.

person Periklis Douvitsas    schedule 07.01.2019