0

ModelAndView を返した後に一部の ModelAttribute 値が失われるという問題に直面しています。

例:

ここにすべての統計項目が適切に入力されています。デバッグモードでそれぞれの正しい値を確認できます。すべてがうまくいっているようです:

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
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Float.TYPE, "materialOccurences.averageM2", new StrictFloatPropertyEditor(true, true, request.getLocale()));
    }
4

2 に答える 2

0

概要:

現状では、Controller と StrictFloatPropertyEditor の間のどこかでデータが失われます。Spring Tag を使用していないときはデータがバインドされているため、JSP の問題はないと思います。

コントローラ:

オブジェクトをデバッグ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());
    }
于 2013-12-13T09:00:34.180 に答える