[英]Spring: ModelAttribute values get lost after returning ModelAndView in <form:input>
我面臨的問題是,某些ModelAttribute值在返回ModelAndView之后會丟失。
例:
這是正確填寫的所有統計項目。 我可以在調試模式下看到每個正確的值。 一切似乎都很好:
ModelAndView mav = new ModelAndView();
mav.addObject("materialStatistic", statisticsService.fillStatistic(statisticHelper));
return mav;
但是在JSP上,數據似乎已經丟失:(僅NULL值)
<c:forEach items="${materialStatistic.materialOccurences}" var="occurence" varStatus="occurenceStatus">
<td>
<form:input path="materialOccurences[${occurenceStatus.index}].averageM2" cssClass="inputFieldShort"/>
</td>
</c:forEach>
同樣非常奇怪的是,如果我打印出如下所示的字段,則會收到數據:(正確的Float值)
${occurence.averageM2}
為什么<form:input>
無法解析我的字段?
更新1:
表格聲明:
<form:form modelAttribute="materialStatistic" action="" id="statistic-material-form" method="POST">
生成的<form:input>
代碼
<input id="materialOccurences20.averageM2" class="inputFieldShort" type="text" value="" name="materialOccurences[20].averageM2">
更新2:
StrictFloatPropertyEditor:
this.getValue()
始終為null
public class StrictFloatPropertyEditor extends PropertyEditorSupport {
private static Log logger = LogFactory.getLog(ProposalService.class);
private Locale locale;
private boolean allowDigits;
private boolean round;
public StrictFloatPropertyEditor(boolean allowDigits, boolean round, Locale locale) {
this.allowDigits = allowDigits;
this.locale = locale;
this.round = round;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Float parsedText = new Float(0);
try {
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);
if (formatter.getDecimalFormatSymbols().getDecimalSeparator() == ',') {
text = text.replaceAll("\\.", ",");
}
parsedText = formatter.parse(text).floatValue();
} catch (ParseException e) {
if (!text.isEmpty()) {
logger.error("Parse Exception occured. Value set to zero: " + e.getMessage());
}
}
super.setValue(parsedText);
}
@Override
public String getAsText() {
if(allowDigits){
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setGroupingUsed(true);
nf.setMinimumFractionDigits(2);
String numberAsText = nf.format(this.getValue());
return numberAsText;
}else if(round){
float number = (Float) this.getValue();
Integer roundedNumber = Math.round(number);
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMinimumFractionDigits(0);
String numberAsText = nf.format(roundedNumber);
return numberAsText;
}else{
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMinimumFractionDigits(0);
String numberAsText = nf.format(this.getValue());
return numberAsText;
}
}
}
InitBinder:
@InitBinder
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Float.TYPE, "materialOccurences.averageM2", new StrictFloatPropertyEditor(true, true, request.getLocale()));
}
您在戰術上不需要使用spring標簽。
JSP
<c:forEach items="${materialStatistic.materialOccurences}" var="occurence" varStatus="occurenceStatus">
<td>
<input class="inputFieldShort" type="text" value="${occurence.averageM2}" name="materialOccurences[${occurenceStatus.index}].averageM2">
</td>
</c:forEach>
摘要:
事實證明,數據在Controller和StrictFloatPropertyEditor之間丟失了。 我猜沒有JSP問題,因為當我不使用Spring Tag時數據是綁定的。
控制器:
我可以調試mav
對象,然后查看mav.model.materialStatistic.materialOccurences
。 所有Float
值均已正確設置...
@RequestMapping(method = RequestMethod.POST)
public ModelAndView loadMaterialStatistic(@ModelAttribute(value="materialStatistic") MaterialStatistic statistic,BindingResult result) {
ModelAndView mav = showStatistic(statistic,"tab_material_statistic");
if (!statistic.getSearchHelper().getMaterialNumber().isEmpty()) {
MaterialStatistic statisticHelper = new MaterialStatistic(statistic.getSearchHelper());
statisticHelper.setProjectMaterialDao(projectMaterialDao);
mav.addObject("materialStatistic", statisticsService.fillStatistic(statisticHelper));
}
return mav;
}
控制器InitBinder
binder.registerCustomEditor(Float.TYPE, "materialOccurences.averageM2", new StrictFloatPropertyEditor(true, false, request.getLocale()));
StrictFloatPropertyEditor :
..但在PropertyEditorSupport
攔截器上, this.getValue()
始終為null
@Override
public String getAsText() {
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setGroupingUsed(true);
nf.setMinimumFractionDigits(2);
String numberAsText = nf.format(this.getValue());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.