spring控制器在两次提交后错误地解释了复选框

scyqe7ek  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(218)

免责声明:我在编码方面不是很有经验,大部分都是我自己学的。可能有一个非常简单的答案。
我有一个由两个复选框(a)和(b)组成的html表单,它提交给spring控制器。控制器验证是否设置了两个复选框。对于每个未设置的复选框,bindingresult都会设置一个错误。如果未设置任何复选框,则会重新加载页面并显示绑定结果错误。
我的问题:以下两个不同的框被选中提交可以检测为“都已被选中”。

再生产

首次提交
只选中两个复选框中的一个复选框(a)
使用chrome中的f12调试编辑器删除其他复选框(b)
提交
结果:浏览器网络分析选项卡显示,只有一个复选框(a)按预期提交。因此,在控制器中,复选框(a)的值为真,缺少的复选框(b)的值为假。到现在为止,一直都还不错。
其次,通过以下步骤提交,我得到了所谓的问题:
选中先前删除的复选框(b)
使用chrome中的f12调试编辑器删除先前选中的复选框(a)
提交
结果:在控制器中,现在复选框(a)和(b)的值都为真,而我只希望(b)为真

简化代码

HTML表单

<form th:action="@{${URL_FEEDBACK_START}}" th:field="${participant}" th:object="${participant}" method="post">
    <input type="hidden" th:field="*{project}" />
    <input type="checkbox" id="priceGameAccepted" th:field="*{priceGameAccepted}" class="form-check-input" required="required" />
    <input type="checkbox" id="dataPrivacyAccepted" th:field="*{dataPrivacyAccepted}" class="form-check-input" required="required" />
    <div class="form-group mb-0">
        <div>
            <button type="submit" id="submit" class="btn btn-primary">
                Zum Feedback!<br /><i class="fas fa-step-forward"></i>
            </button>
        </div>
    </div>
</form>

控制器

@PostMapping(path = "/submit")
public String submit(Model model,
        HttpSession session,
        @ModelAttribute(SessionAttributeHelper.PROJECT) Project project,
        @ModelAttribute(SessionAttributeHelper.PARTICIPANT) @Valid Participant participant,
        BindingResult bindingResult) {

    try {
        validateOptionalPriceGameAccepted(project, participant);
        validateDataPrivacyAccepted(participant);

            [... here would be the following code]

    } catch (DataPrivacyNotAcceptedException e) {
        bindingResult.addError(new FieldError("participant", "dataPrivacyAccepted", e.getMessage()));
        return backToForm(model, project);

    } catch (PriceGameNotAcceptedException e) {
        bindingResult.addError(new FieldError("participant", "priceGameAccepted", e.getMessage()));
        return backToForm(model, project);
    }
}

private String backToForm(Model model, Project project) {
    UiText uiText = uts.getUiText(project, UiTextKey.MSG_FEEDBACK_START);
    mfs.fillUiText(model, uiText); //mfs = ModelFillerService
    mfs.fillGlobal(model);
    return ApplicationPathHelper.RES_FEEDBACK_START;        
}

modelfillerservice公司

public void fillGlobal(Model model) {

    LOG.debug("Preparing model for global area");
    model.addAttribute("URL_HOME", ApplicationPathHelper.URL_HOME);
    model.addAttribute("URL_PROJECTHOME", ApplicationPathHelper.URL_PROJECTHOME); //TODO where necessary?
    model.addAttribute("URL_IMPRESSUM", ApplicationPathHelper.URL_IMPRESSUM);
    model.addAttribute("URL_DATENSCHUTZ", ApplicationPathHelper.URL_DATENSCHUTZ);
    model.addAttribute("URL_PRICEGAME", ApplicationPathHelper.URL_PRICEGAME);

    LOG.debug("Preparing model for feedback start area");
    model.addAttribute("URL_FEEDBACK_START", ApplicationPathHelper.URL_FEEDBACK_START);
    model.addAttribute("URL_FEEDBACK_QUESTION_SUBMIT", ApplicationPathHelper.URL_FEEDBACK_QUESTION_SUBMIT);
    model.addAttribute("URL_FEEDBACK_RESULT_SUBMIT", ApplicationPathHelper.URL_FEEDBACK_RESULT_SUBMIT);

    LOG.debug("Preparing model for anonymous area");
    model.addAttribute("URL_LOGIN_FORM", ApplicationPathHelper.URL_LOGIN_FORM);
    model.addAttribute("URL_LOGIN", ApplicationPathHelper.URL_LOGIN);

    LOG.debug("Preparing model for administration area");
    model.addAttribute("URL_ADMIN_PROJECTDETAILS", ApplicationPathHelper.URL_ADMIN_PROJECTDETAILS);
    model.addAttribute("URL_ADMIN_PROJECTS", ApplicationPathHelper.URL_ADMIN_PROJECTS);
    model.addAttribute("URL_ADMIN_SHOWFEEDBACK", ApplicationPathHelper.URL_ADMIN_SHOWFEEDBACK); //TODO notwendig?
    model.addAttribute("URL_ADMIN_EDITUITEXT", ApplicationPathHelper.URL_ADMIN_EDITUITEXT); //TODO notwendig?
    model.addAttribute("URL_ADMIN_SHOWUSERAGENTS", ApplicationPathHelper.URL_ADMIN_SHOWUSERAGENTS); //TODO notwendig?
    model.addAttribute("URL_LOGOUT", ApplicationPathHelper.URL_LOGOUT);

    model.addAttribute("TIMENOW", ZonedDateTimeHelper.nice(ZonedDateTimeHelper.nowCET()));      

    /*
     * URLs for DEV profile
     */
    List<String> profiles = Arrays.asList(environment.getActiveProfiles());

    if (profiles.contains(ApplicationProfileHelper.DEV_PROFILE)) {
        LOG.debug("Preparing model for DEV profile.");
        model.addAttribute("isDevProfile", true);
    }

    if (profiles.contains(ApplicationProfileHelper.REV_PROFILE)) {
        LOG.debug("Preparing model for REV profile.");
        model.addAttribute("isRevProfile", true);
    }
}

public void fillUiText(Model model, UiText uiText) {
    model.addAttribute(uiText.getUiTextKey().toString(), uiText.getText());
}

验证方法

private void validatePriceGameAccepted(Project project, Participant participant) throws PriceGameNotAcceptedException {
    boolean priceGameStatementAccepted = participant.isPriceGameAccepted();
    if(project.isPricegame() && ! priceGameStatementAccepted) {
        throw new PriceGameNotAcceptedException();
    }       
}

private void validateDataPrivacyAccepted(@Valid Participant participant) throws DataPrivacyNotAcceptedException {
    boolean dataPrivacyStatementAccepted = participant.isDataPrivacyAccepted();
    if(! dataPrivacyStatementAccepted) {
        throw new DataPrivacyNotAcceptedException();
    }
}

带有spring和thymeleaf模板引擎的java11软件,这可能不是这里的问题。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.1</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

思想

有没有我看不到或不懂的springweb或数据特性?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题