Tôi đã làm một số tìm kiếm trên web và trong stackoverflow để xem làm thế nào để xử lý các thông báo sau đây mà tôi nhận được trên một màn hình của tôi:sử dụng mùa xuân của thông điệp typeMismatch
Không thể chuyển đổi giá trị tài sản của loại [java.lang.String] để yêu cầu nhập [java.lang.Long] cho thuộc tính 'qtyToShip'; lồng nhau ngoại lệ là java.lang.IllegalArgumentException: Không thể phân tích số: Unparseable số: "a"
Từ nghiên cứu và từ nhìn vào web Tôi giả định rằng việc thêm dòng sau vào file errors.properties tôi sẽ tạo ra kết quả mong muốn:
typeMismatch.shippingCommand.qtyToShip=1. Invalid value for Qty To Ship, accepts only numbers.
typeMismatch.qtyToShip=2. Invalid value for Qty To Ship, accepts only numbers.
shippingCommand.qtyToShip=3. Invalid value for Qty To Ship, accepts only numbers.
qtyToShip=4. Invalid value for Qty To Ship, accepts only numbers.
typeMismatch.java.lang.NumberFormatException=5. Invalid value for {0}, accepts only numbers.
typeMismatch.java.lang.Integer=Must specify an integer value.
typeMismatch.java.lang.Long=Must specify an integer value.
typeMismatch.java.lang.Float=Must specify a decimal value.
typeMismatch.java.lang.Double=Must specify a decimal value.
typeMismatch.int=Invalid number entered
typeMismatch=Invalid type entered
Tôi thêm giá trị nguyên vào thông báo để xác định giá trị nào sẽ hiển thị.
Bây giờ ở phía trên cùng của JSP của tôi, tôi có như sau:
<center>
<spring:hasBindErrors name="shippingCommand">
<c:if test="${errors.errorCount > 0 }">
<h4>Following errors need to be corrected:</h4>
<font color="red">
<c:forEach items="${errors.allErrors}" var="error">
<spring:message code="${error.code}" arguments="${err.arguments}" text="${error.defaultMessage}"/><br />
</c:forEach>
</font>
</c:if>
</spring:hasBindErrors>
</center>
Trên đây là bên ngoài mẫu của tôi, bên trong mẫu của tôi, tôi đã như sau (thử nghiệm ra lý tưởng)
Vì vậy, kết quả là khi tôi chạy mã của mình để kích hoạt lỗi, tôi thấy rằng bên trong biểu mẫu của tôi, tôi nhận được thông báo sau:
1. Invalid value for Qty To Ship, accepts only numbers.
nào xuất phát từ này: typeMismatch.shippingCommand.qtyToShip
Bên ngoài của displayes dạng:
Invalid type entered
Những gì tôi không hiểu là tại sao tôi có thể hiển thị thông điệp đúng bên trong hình thức của tôi, nhưng không ở bên ngoài?
Trong bộ điều khiển tôi đã thêm như sau:
@Override
protected void initBinder(HttpServletRequest pRequest, ServletRequestDataBinder pBinder) throws Exception
{
NumberFormat numberFormat = NumberFormat.getInstance();
pBinder.registerCustomEditor(Long.class, "qtyToShip", new CustomNumberEditor(Long.class, numberFormat, false));
}
Cảm ơn
Con súng đã làm điều đó. Cảm ơn – boyd4715