簡體   English   中英

Bean名稱的BindingResult和普通目標對象都不能用作請求屬性(Spring MVC)

[英]Neither BindingResult nor plain target object for bean name available as request attribute (Spring MVC)

請幫我。 我試圖在1個jsp上使用2個表單。 第一個表格工作正常。 當我嘗試提交第二個表單時,我得到了IllegalStateException。 我已經閱讀了很多有關此問題的內容,但是沒有任何東西可以解決我當前的問題。

形成:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<head>
    <%@ include file="../views/common/imports.jsp" %>
    <title>MARC</title>
</head>
<body onload="togglePieLines()">
    <%@ include file="../views/common/header.jsp"%>
    <br>
    <br>
    <div class="standard-form">
        <form:form commandName="unit" method="post" action="units" class="uk-form" id="unitForm">
            <!-- Allgemeine Informationen -->
            <table class="uk-table uk-table-condensed uk-text-small">
                <thead>
                    <tr>
                        <th><spring:message code="units.generalInformation"/></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="uk-width-1-1 td-form-row"><spring:message code="units.id"/>&nbsp;*&nbsp;<form:errors path="unitId" class="uk-badge uk-badge-warning"/></td>
                        <td class="uk-width-1-1"><form:input path="unitId" class="uk-form-width-medium" /></td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.name"/>&nbsp;*&nbsp;<form:errors path="name" class="uk-badge uk-badge-warning"/></td>                   
                        <td>            
                            <form:input path="name" class="uk-form-width-medium" />
                        </td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.country"/>&nbsp;*&nbsp;<form:errors path="country" class="uk-badge uk-badge-warning"/></td>
                        <td>
                            <div class="uk-form-row">
                               <form:select path="country" items="${countryList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.bu"/></td>
                        <td><form:input path="bu" class="uk-form-width-medium" /></td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.pie"/></td>
                        <td><form:checkbox id="pie" path="pie" onChange="togglePieLines()" /></td>
                    </tr>
                    <tr id="pieReason" style="display: none;">
                        <td><spring:message code="units.pieJustification"/></td>
                        <td>
                            <div class="uk-form-row">
                               <form:select path="pieReason" items="${pieReasonList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                    <tr id="pieSince" style="display: none;">
                        <td><spring:message code="units.pieSince"/></td>
                        <td>
                            <div class="uk-form-icon">
                                   <i class="uk-icon-calendar"></i>
                                   <form:input path="pieSince" data-uk-datepicker="{format:'DD.MM.YYYY'}" class="uk-form-width-medium" />
                             </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <!-- Anwendungsparameter -->
            <table class="uk-table uk-table-condensed uk-text-small">
                <thead>
                    <tr>
                        <th><spring:message code="units.applicationParameters"/></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="uk-width-1-1"><spring:message code="units.legalForm"/></td>
                        <td class="uk-width-1-1">
                            <div class="uk-form-row">
                                <form:select path="legalForm" items="${legalFormList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.group"/></td>
                        <td><form:checkbox path="consolidationType" /></td>
                    </tr>
                </tbody>
            </table>
            <!-- Verantwortlichkeiten -->
            <table class="uk-table uk-table-condensed uk-text-small">
                <thead>
                    <tr>
                        <th><spring:message code="units.responsibilities"/></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="uk-width-1-1"><spring:message code="units.responsibleUnit"/></td>
                        <td class="uk-width-1-1">
                            <div class="uk-form-row">
                               <form:select path="responsibleUnit" items="${userList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                   <tr>
                        <td><spring:message code="units.responsibleRotation"/></td>
                        <td>
                            <div class="uk-form-row">
                               <form:select path="responsibleRotation" items="${userList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                    <tr>
                        <td><spring:message code="units.responsibleNonAudit"/></td>
                        <td>
                            <div class="uk-form-row">
                               <form:select path="responsibleNonAudit" items="${userList}" class="uk-form-width-medium" />
                           </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="input-center">
                <button class="uk-button" type="submit"><i class="uk-icon-save"></i>&nbsp;<spring:message code="button.save"/></button>
                <button class="uk-button" type="reset"><i class="uk-icon-undo"></i>&nbsp;<spring:message code="button.reset"/></button>
                <a class="uk-button" href="<%=request.getContextPath()%>/unitsOverview"><i class="uk-icon-arrow-left"></i>&nbsp;<spring:message code="button.back"/></a>
                <br>
            </div>
        </form:form>
        <c:if test="${not empty showParentUnits}">
            <form:form commandName="parentUnit" method="post" action="parentUnits" class="uk-form">
                <!-- Zuordnung übergeordneter Einheiten -->
                <table class="uk-table uk-table-condensed uk-text-small">
                    <thead>
                        <tr>
                            <th colspan="6"><spring:message code="units.parentsAssignment"/></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="uk-text-left">&nbsp;<spring:message code="units.parent"/></td>
                            <td class="uk-text-center"><spring:message code="units.share"/></td>
                            <td class="uk-text-center"><spring:message code="units.usb"/></td>
                            <td class="uk-text-center"><spring:message code="units.fullLiability"/></td>
                            <td class="uk-text-center"><spring:message code="units.consolidation"/></td>
                            <td class="uk-text-center"></td>
                        <tr>
                        <tr>
                            <td>
                                <div class="uk-form-row">
                                    <form:select path="parentUnitId" items="${unitList}" class="uk-form-width-medium" />
                               </div>
                            </td>
                            <td class="uk-text-center"><input type="text" class="uk-form-width-mini"></td>
                            <td class="uk-text-center"><input type="text" class="uk-form-width-mini"></td>
                            <td class="uk-text-center"><input type="checkbox"></td>
                            <td class="uk-text-center"><input type="checkbox"></td>
                            <td class="uk-text-center"><button class="uk-button" type="submit"><i class="uk-icon-plus"></i></button></td>
                        </tr>
                    </tbody>
                </table>
            </form:form>
        </c:if>
    </div>
</body>
<script>
function togglePieLines() {
    if ($('#pie').prop('checked')) {
         $('#pieReason').fadeIn(400);
         $('#pieSince').fadeIn(400);
    }
    else {
         $('#pieReason').fadeOut(400);
         $('#pieSince').fadeOut(400); 
    }
}
</script>
</html>

控制器:

package de.amana.marc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import de.amana.marc.model.ParentUnit;
import de.amana.marc.model.Unit;
import de.amana.marc.service.ParentUnitService;
import de.amana.marc.service.UnitService;

@Controller
public class UnitsController
{

    private UnitService unitService;
    @Autowired(required=true)
    @Qualifier(value="unitService")
    public void setUnitService(UnitService us){
        this.unitService = us;
    }

    private ParentUnitService parentUnitService;
    @Autowired(required=true)
    @Qualifier(value="parentUnitService")
    public void setParentUnitService(ParentUnitService pus){
        this.parentUnitService = pus;
    }

    @RequestMapping(value = "/units", method = RequestMethod.GET)
    public String unitForm(Model model, HttpServletRequest request, HttpServletResponse response) { 
        model = this.unitService.fillFormSelectBoxes(model);
        // Formobjekte
        model.addAttribute("unit", new Unit());
        model.addAttribute("parentUnit", new ParentUnit());

        return "units";
    }

    @RequestMapping(value="/units", method =RequestMethod.POST)
    public String unitFormSubmit(@Valid @ModelAttribute Unit unit, BindingResult result, Model model) {
        model = this.unitService.fillFormSelectBoxes(model);
        //Validierung
        if (result.hasErrors()) {

            return "units";
        } 
        else {
            // Speichern in DB
            this.unitService.addUnit(unit);
            // Zuordnung übergeordneter Gesellschaften einblenden
            model.addAttribute("showParentUnits", true);
            // Form neu befüllen
            model.addAttribute("unit", unit);
            model.addAttribute("unitList", this.unitService.listUnits());
            ParentUnit newPu = new ParentUnit();
            newPu.setUnitId(unit.getUnitId());
            model.addAttribute("parentUnit", newPu);

            return "units";
        }
    }

    @RequestMapping(value="/parentUnits", method =RequestMethod.POST)
    public String parentUnitFormSubmit(@Valid @ModelAttribute ParentUnit parentUnit, BindingResult result, Model model) {
        // Formobjekt bearbeiten vor Speichern  
        ParentUnit newPu = new ParentUnit();        
        model = this.unitService.fillFormSelectBoxes(model);

        return "units";
    }
}

完整的堆棧跟蹤: 在此輸入圖像描述

有時,Stacktrace更改為: 在此輸入圖像描述 這有點奇怪,因為我應該提交,而且我不知道為什么第一個表格有問題

您是否可以嘗試在第一個這樣的表單上使用屬性“ modelAttribute”,並在其控制器參數中使用相同的名稱:

<form:form commandName="unit" method="post" action="units" class="uk-form" id="unitForm" modelAttribute="unit">

並且您的第二種形式在Controller Parameter中也具有相同的名稱:

<form:form commandName="parentUnit" method="post" action="parentUnits" class="uk-form" modelAttribute="parentUnit">

確保在模型中添加與添加屬性相同的名稱,以成功訪問.jsp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM