Announcement Announcement Module
Collapse
No announcement yet.
Saving table information to database Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving table information to database

    I am not sure if this helps but I used the Scaffold Spring CRUD Application to generate my code from existing database. Anyways, I needed to modify the edit customers jsp so that in addition to a table containing their personal information, there is also a table containing information on the customer payments. I added the second "payments" table, but when pressing the save button only the information in the original table saves, and the information in the "payments" table I created does not save.

    My Customer object contains all customer information found in first table such as name, phone number, address, etc, and it also contains a Set of the customer's Payments. Then, my Payment object has information regarding each individual payment (check number, amount, etc). Again it is the payment information that will not save to the database.


    Here is part of my editCustomer.jsp
    Code:
    <form:form action="${pageContext.request.contextPath}/saveCustomer" method="POST" modelAttribute="customer">
    	<table cellpadding="0" cellspacing="0">
    		<tbody>
    			<tr>
    				<td class="label">
    					<fmt:message key="customer.customernumber.title"/>:
    				</td>
    				<td>
         					<c:choose>
    						<c:when test='${customer.customernumber != null}' >
    	 						${customer.customernumber}
           						&nbsp;
    	 						<form:hidden id="Customer_customernumber" path="customernumber"/>
    						</c:when>
    						<c:otherwise>
    							<form:input id="Customer_customernumber" path="customernumber"/>
    	 						<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "Customer_customernumber",widgetType : "dijit.form.NumberTextBox",widgetAttrs : {promptMessage: "<fmt:message key="customer.customernumber.help"/>", constraints : {places:0}}})); </script>
    						</c:otherwise>
    					</c:choose>
    				</td>
    			</tr>
    			<tr>
    				<td class="label">
    					<fmt:message key="customer.contactlastname.title"/>:
    				</td>
    				<td>
    					<form:input id="Customer_contactlastname" path="contactlastname"/>
    					<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "Customer_contactlastname",widgetType : "dijit.form.ValidationTextBox",widgetAttrs : {promptMessage: "<fmt:message key="customer.contactlastname.help"/>"}})); </script>
    				</td>
    			</tr>
    			<tr>
    				<td class="label">
    					<fmt:message key="customer.contactfirstname.title"/>:
    				</td>
    				<td>
    					<form:input id="Customer_contactfirstname" path="contactfirstname"/>
    					<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "Customer_contactfirstname",widgetType : "dijit.form.ValidationTextBox",widgetAttrs : {promptMessage: "<fmt:message key="customer.contactfirstname.help"/>"}})); </script>
    				</td>
    			</tr>
    			<tr>
    				<td class="label">
    					<fmt:message key="customer.phone.title"/>:
    				</td>
    				<td>
    					<form:input id="Customer_phone" path="phone"/>
    					<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "Customer_phone",widgetType : "dijit.form.ValidationTextBox",widgetAttrs : {promptMessage: "<fmt:message key="customer.phone.help"/>"}})); </script>
    				</td>
    			</tr>
    			<tr>
    				<td class="label">
    					<fmt:message key="customer.addressline1.title"/>:
    				</td>
    				<td>
    					<form:input id="Customer_addressline1" path="addressline1"/>
    					<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "Customer_addressline1",widgetType : "dijit.form.ValidationTextBox",widgetAttrs : {promptMessage: "<fmt:message key="customer.addressline1.help"/>"}})); </script>
    				</td>
    			</tr>
    		</tbody>
    	</table>
    
    	<h1><fmt:message key="navigation.edit"/> <fmt:message key="payment.title"/>s</h1>
    	<div class="navitem"><a class="button" href="${pageContext.request.contextPath}/newCustomerPayment?customer_customernumber=${customer.customernumber}&"><span><img src="${pageContext.request.contextPath}/images/icons/new.gif" /><fmt:message key="navigation.new"/> <fmt:message key="payment.title"/></span></a></div>
    	<table cellpadding="0" cellspacing="0">
    		<thead>
    			<tr>
    				<th>&nbsp;</th>
    				<th><fmt:message key="payment.customernumber.title"/></th>
    				<th><fmt:message key="payment.checknumber.title"/></th>
    				<th><fmt:message key="payment.paymentdate.title"/></th>
    				<th><fmt:message key="payment.amount.title"/></th>
    			</tr>
    		</thead>
    		<tbody>
    			<c:forEach items="${customer.payments}" var="current"  varStatus="i">	
    				<c:choose>
    					<c:when test="${(i.count) % 2 == 0}">
    					    	<c:set var="rowclass" value="rowtwo"/>
    					</c:when>
    					<c:otherwise>
    					    	<c:set var="rowclass" value="rowone"/>
    					</c:otherwise>
    				</c:choose>
    				<tr class="${rowclass}">
    					<td nowrap="nowrap">
    						<a title="<fmt:message key="navigation.view" />" href="${pageContext.request.contextPath}/selectCustomerPayment?customer_customernumber=${customer.customernumber}&payment_customernumber=${current.customernumber}&payment_checknumber=${current.checknumber}&"><img src="images/icons/view.gif" /></a>
    						<a title="<fmt:message key="navigation.edit" />" href="${pageContext.request.contextPath}/editCustomerPayment?customer_customernumber=${customer.customernumber}&payment_customernumber=${current.customernumber}&payment_checknumber=${current.checknumber}&"><img src="images/icons/edit.gif" /></a>
    						<a title="<fmt:message key="navigation.delete" />" href="${pageContext.request.contextPath}/confirmDeleteCustomerPayment?customer_customernumber=${customer.customernumber}&payment_customernumber=${current.customernumber}&payment_checknumber=${current.checknumber}&"><img src="images/icons/delete.gif" /></a>
    					</td>
    					<td>
    						<c:choose>
    							<c:when test='${current.customernumber != null}' >
    								${current.customernumber}
    								&nbsp;
    								<form:hidden id="customernumber" path="payments[${i.index}].customernumber"/>
    							</c:when>
    							<c:otherwise>
    								<form:input id="customernumber" path="payments[${i.index}].customernumber"/>
    								<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "customernumber",widgetType : "dijit.form.NumberTextBox",widgetAttrs : {promptMessage: "<fmt:message key="payment.customernumber.help"/>", constraints : {places:0}}})); </script>
    							</c:otherwise>
    						</c:choose>
    					</td>
    					<td>
    						<c:choose>
    							<c:when test='${current.checknumber != null}' >
    								${current.checknumber}
    								&nbsp;
    								<form:hidden id="checknumber" path="payments[${i.index}].checknumber"/>
    							</c:when>
    							<c:otherwise>
    								<form:input id="checknumber" path="payments[${i.index}].checknumber"/>
    								<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "checknumber",widgetType : "dijit.form.ValidationTextBox",widgetAttrs : {promptMessage: "<fmt:message key="payment.checknumber.help"/>", required : true}})); </script>
    							</c:otherwise>
    						</c:choose>
    					</td>
    					<td>
    						<input id="Payment_paymentdate${i.index}" name="paymentdate" type="text" value="<fmt:formatDate value="${current.paymentdate.time}" pattern="yyyy-MM-dd"/>" dojoType="dijit.form.DateTextBox" constraints="{datePattern:'<fmt:message key="date.format"/>'}" trim="true" promptMessage="<fmt:message key="date.format" />" invalidMessage="<fmt:message key="date.format.invalid" /> <fmt:message key="date.format" />." />
    					</td>
    					<td>
    						<form:input id="amount${i.index}" path="payments[${i.index}].amount"/>
    						<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : "amount${i.index}",widgetType : "dijit.form.NumberTextBox",widgetAttrs : {promptMessage: "<fmt:message key="payment.amount.help"/>"}})); </script>
    					</td>
    				</tr>
    			</c:forEach>
    		</tbody>
    	</table>
    	<span class="inputbutton"><input class="button" id="save" type="submit" value="<fmt:message key="navigation.save"/>"/></span>
    	<script type="text/javascript">Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'save', event:'onclick'}));</script>
    </form:form>
    And here is part of my CustomerController
    Code:
    @RequestMapping("/saveCustomer")
    public String saveCustomer(@ModelAttribute Customer customer) {
    	customerService.saveCustomer(customer);
    	return "forward:/indexCustomer";
    }
    Thanks in advance for your time and assistance.
Working...
X