Reputation: 33
Please help me. Im trying to use 2 Forms on 1 jsp. The first Form works just fine. When i try to submit the second form i get the IllegalStateException. I have already read a lot of things about this problem, but nothing could solve my current problem.
Form:
<%@ 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"/> * <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"/> * <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"/> * <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> <spring:message code="button.save"/></button>
<button class="uk-button" type="reset"><i class="uk-icon-undo"></i> <spring:message code="button.reset"/></button>
<a class="uk-button" href="<%=request.getContextPath()%>/unitsOverview"><i class="uk-icon-arrow-left"></i> <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"> <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>
Controller:
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";
}
}
Full Stacktrace:
Sometimes the Stacktrace changes to:
This is kinda wierd, because i should be in submit and i have no clue why the first form has the problem
Upvotes: 0
Views: 695
Reputation: 1479
Could you try to use attribute "modelAttribute" on your first form like this and use it in your controller Parameter like the same name:
<form:form commandName="unit" method="post" action="units" class="uk-form" id="unitForm" modelAttribute="unit">
and your second form also the same name in the Controller Parameter:
<form:form commandName="parentUnit" method="post" action="parentUnits" class="uk-form" modelAttribute="parentUnit">
Be sure to add the same name as added attribute in your model to access in your success .jsp
Upvotes: 0