Spring: ModelAttribute values get lost after returning ModelAndView in <form:input>

1.8k views Asked by At

I am facing the problem, that some ModelAttribute values get lost after returning ModelAndView.

Example:

Here are all statistic items properly filled. I can see each correct value in debug mode. Everything seem to be fine:

ModelAndView mav = new ModelAndView();
mav.addObject("materialStatistic", statisticsService.fillStatistic(statisticHelper));
return mav;

But on the JSP the data seem to have been lost: (only NULL values)

<c:forEach items="${materialStatistic.materialOccurences}" var="occurence" varStatus="occurenceStatus"> 
    <td>
        <form:input path="materialOccurences[${occurenceStatus.index}].averageM2" cssClass="inputFieldShort"/>
    </td>
</c:forEach>

Also very strange is, if I print out the fields like following, I receive the data: (correct Float values)

${occurence.averageM2}

Why can <form:input> not resolve my fields?


Update 1:

Form declaration:

<form:form modelAttribute="materialStatistic" action="" id="statistic-material-form" method="POST">

Generated code of <form:input>

<input id="materialOccurences20.averageM2" class="inputFieldShort" type="text" value="" name="materialOccurences[20].averageM2">

Update 2:

StrictFloatPropertyEditor:

this.getValue() is always 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()));
    }
2

There are 2 answers

0
Tunguska On

Summary:

As things stand, the data get lost somewhere between Controller and StrictFloatPropertyEditor. I guess no JSP problem, since data is bound when I am not using Spring Tag.

Controller:

I can debug mav object und look into mav.model.materialStatistic.materialOccurences. All Float values are properly set...

    @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;
    }

Controller InitBinder

binder.registerCustomEditor(Float.TYPE, "materialOccurences.averageM2", new StrictFloatPropertyEditor(true, false, request.getLocale()));

StrictFloatPropertyEditor:

.. but at PropertyEditorSupport Interceptor, this.getValue() is always null

@Override
    public String getAsText() {
        NumberFormat nf = NumberFormat.getInstance(locale);
        nf.setGroupingUsed(true);
        nf.setMinimumFractionDigits(2);
        String numberAsText = nf.format(this.getValue());
    }
1
Tomas Santos On

You don't tactically need to use the spring tag.

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>