2015-02-10 95 views
0

请帮助我。我试图在1个JSP上使用2个表单。第一个表单工作得很好。当我尝试提交第二个表单时,我得到IllegalStateException。我已经阅读了很多关于这个问题的内容,但是没有任何东西可以解决我目前的问题。BindingResult和bean名称的普通目标对象都不作为请求属性(Spring MVC)

形式:

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

完整堆栈跟踪: enter image description here

有时堆栈跟踪更改为: enter image description here 这是有点儿奇怪,因为我应该在提交和我不知道为什么第一个表格有问题

+0

您可以提供完整的堆栈跟踪? – 2015-02-10 13:13:19

+0

只需一秒钟。我将上传完整的堆栈跟踪 – 2015-02-10 13:16:30

+0

Stacktrace上传 – 2015-02-10 13:19:48

回答

0

你能尝试使用属性“的ModelAttribute”你的第一个表格上这样的,并用它在你的控制器参数就像同一个名字:

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

和你的第二个形式也在控制器参数同名:

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

一定要在你的模型在成功添加相同的名称属性附加到访问的.jsp

相关问题