จะละเว้นฟิลด์เฉพาะในรูปแบบใน thymeleaf ได้อย่างไร

ฉันกำลังสร้างแบบฟอร์มใน thymeleaf ที่มีฟิลด์อัพโหลดไฟล์ที่ไม่ได้เป็นส่วนหนึ่งของโมเดลของฉัน เมื่อฉันโหลดหน้าเว็บ thymeleaf จะบ่นและโยน NotReadablePropertyException สำหรับฟิลด์นั้น

ฉันจะทำให้ thymeleaf เพิกเฉยต่อความจริงที่ว่าไม่มีฟิลด์อยู่ในโมเดลได้อย่างไร

รหัส:

<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="/th#" 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