[英]spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id}
[英]frontpage get object's long type id from model.addAttribute lose precision
認為有一個名為“用戶”的類。
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
//...get、set
}
然后,我嘗試通過以下代碼跳至用戶信息編輯頁面:
@GetMapping("edit")
public String edit(Model model, @RequestParam Long id) {
model.addAttribute("entity", userService.findById(id));
return "user/edit";
}
在“用戶/編輯”頁面中,我嘗試獲取正確的id值,但失敗。長型id的值始終失去精度,就像id的值是“ 94105929343041536”一樣,我得到的是“ 94105929343041540”。
無論您的首頁是什么,處理大的long值時Javascript,JSON和Angular都有很多問題。 不幸的是,您無法更改這些值的取整方式。 在任何一種情況下,我都建議使用一個轉換器在檢索/存儲時(使用諸如AttributeConverter之類的)將您的長值轉換為String。
@Converter
public class LongStringConverter implements AttributeConverter<Long, String> {
@Override
public String convertToDatabaseColumn(Long value) {
return Long.toString(value);
}
@Override
public Long convertToEntityAttribute(String dbData) {
return Long.parseLong(dbData);
}
}
然后,您可以將其添加到您的id字段中:
@Convert(converter = LongStringConverter.class)
根據您的確切使用情況,還有其他一些轉換器可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.