Results 1 to 8 of 8

Thread: form:select sending two values instead of one

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore
    Posts
    16

    Default form:select sending two values instead of one

    Hi!,
    Scenario: I have a form with a form:select tag as below:
    Code:
      <form:form method="POST" commandName="reportField">
    .............
    			<tr>
    			    <td><fmt:message key="reportfield.businessTerm"/></td>
    	      		<td>
    	      		<form:select path="businessTermID">
    	      			<form:options items="${businessTerms}" itemValue="businessTermID" itemLabel="businessTermName"></form:options>
    	      		</form:select>
    				</td>
    			</tr>
    there is a form backing object which will get the reportfield object during modification. And businessTermID is of type 'int'.

    When i press transmit, i get a binding error, as the form is actually sending the value "1,2".Here 1 is the initial value of businessTermID and 2 is the value after it was changed in the form. the form should send only "2".

    I am able to get around this problem, by renaming businessTermID with a different name in the class which implements reportField, as below.
    Code:
      <form:form method="POST" commandName="reportField">
    .............
    			<tr>
    			    <td><fmt:message key="reportfield.businessTerm"/></td>
    	      		<td>
    	      		<form:select path="reportFieldBusinessTermID">
    	      			<form:options items="${businessTerms}" itemValue="businessTermID" itemLabel="businessTermName"></form:options>
    	      		</form:select>
    				</td>
    			</tr>
    This works perfectly fine.

    If my understanding is correct, this looks like a bug to me. Or there should be other way of handling this by keeping the names intact.

    Can somebody please throw some light on this!?

    Thanks,
    Sreedhar.

  2. #2
    Join Date
    Apr 2005
    Location
    Finland
    Posts
    314

    Default

    Make sure that you don't have a a dublicate binding on the "businessTermID" property somewhere.
    if a trainstation is where the train stops, what's a workstation...

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore
    Posts
    16

    Default

    Hi there!
    Here is my full jsp code:
    Code:
    <%@ include file="../common/includes.jsp" %>
    
    	<c:choose>
           <c:when test="${reportField == null}">
    			<h2><fmt:message key="section.field.add.title"/></h2>
           </c:when>
           <c:otherwise>
        		<h2><fmt:message key="section.field.modify.title"/></h2>
           </c:otherwise>
        </c:choose>
    
    	<hr/>
      <form:form method="POST" commandName="reportField">
         <table>
            <tbody>
            	<tr>
    			    <td><fmt:message key="report.name"/></td>
    		    	<td>
    		    		<c:out value="${report.reportName}"></c:out>
    		    	</td>
    			</tr>
            	<tr>
    			    <td><fmt:message key="section.name"/></td>
    		    	<td>
    		    		<c:out value="${section.sectionName}"></c:out>
    		    	</td>
    			</tr>			
    
    			<tr>
    			    <td><fmt:message key="reportfield.businessTerm"/></td>
    	      		<td>
    	      		<form:select path="businessTermID">
    	      			<form:options items="${businessTerms}" itemValue="businessTermID" itemLabel="businessTermName"></form:options>
    	      		</form:select>
    				</td>
    			</tr>
    			
    			<tr>
    			    <td><fmt:message key="reportfield.displayFormat"/></td>
    		    	<td>
    		    		<form:input path="displayFormat"/>
    		    	</td>
    			</tr>
    			<tr>
    			    <td><fmt:message key="reportfield.displayName"/></td>
    		    	<td>
    		    		<form:input path="displayName"/>
    		    	</td>
    			</tr>						
    			<tr>
    			    <td><fmt:message key="reportField.pageBreak"/></td>
    		    	<td>
    		    		Yes <form:radiobutton path="pageBreak" value="Y"/>
    		    		No <form:radiobutton path="pageBreak" value="N"/>
    		    	</td> 
    			</tr>
    			<tr>
    			    <td><fmt:message key="reportfield.totalsOn"/></td>
    		    	<td>
    		    		<form:input path="totalsOn"/>
    		    	</td>
    			</tr>
    			<tr>
    			    <td><fmt:message key="reportField.sorting.order"/></td>
    		    	<td>
    		    		<form:input path="sortingOrder"/>
    		    	</td>
    			</tr>									
            <tr>
                <td colspan="2" style="text-align:center;">
                    <input type="submit" value="Submit"/>
                 </td>
            </tr>
            </tbody>
    	</table>
    
        <hr/>
      </form:form>
    There is no other binding any where else. probably i should take a dive into Spring to find out!
    Thanks,
    Sreedhar

  4. #4

    Default

    Try a button which does a simple Javascript call alerting the number of elements with that name: alert(documents.getElementsByName("businessTermID" ).length)

    A second item might come from any included jsp's, check those as well.

  5. #5
    Join Date
    Jul 2008
    Location
    Ottawa
    Posts
    6

    Default

    You mentioned that there is an initial value for businessTermID and then it is changed to another value.

    The form:form tag will have generated an action URL, which may contain the previous value for businessTermID in the query string.

    Before you submit your form, to update businessTermID, view the HTML source and see if the value is there.

    You could always hard code the action URL, with no params, for the form:form tag.

    Hope that helps!

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore
    Posts
    16

    Default

    Hi Laura,
    I tried the alert thing. But it returned me 0. dont know why!

    this is what is happening.
    - The URL that schedules this page is

    "modifyreportfield.htm?sectionID=8&businessTermID= 3"

    - And when i submit the page, the form is submitted as well which contains another 'businessTermID field.
    - and 2 values are passed for businessTermID as "1,2". As this is an Integer it throws an error while casting it to int.

    i changed the request parameter to bizTermID and it is working perfectly now.

    definitely something is wrong. Both th request params and the form field are being clubbed into one field if the names are same and they are being sent to the host.
    Thanks,
    Sreedhar

  7. #7
    Join Date
    Jul 2008
    Location
    Ottawa
    Posts
    6

    Default

    Hey Sreedhar,

    That is exactly what I described is happening.

    You could try this:

    <form:form method="POST" commandName="reportField" action="modifyreportfield.htm">

    This will eliminate the previously submitted values being added to your action URL.
    Although, this may cause you some grief, if you have other business logic that depends on those parameters being included in the URL.

    James

  8. #8
    Join Date
    Aug 2006
    Location
    Bangalore
    Posts
    16

    Default

    Hey James,
    I forgot to mention that. I tried it as well. But my controller, expects a businessTermID as a request parameter as well. I differentiate between the create and modify screens based on this parameter. My controller is as below:

    Code:
    package com.icreate.dataconnect.web.controller.report;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.validation.BindException;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    import org.springframework.web.servlet.view.RedirectView;
    
    import com.icreate.dataconnect.dictionary.bo.DictionaryBO;
    import com.icreate.dataconnect.domain.datadictionary.BusinessTerm;
    import com.icreate.dataconnect.domain.datadictionary.BusinessTermImpl;
    import com.icreate.dataconnect.domain.report.ReportField;
    import com.icreate.dataconnect.domain.report.ReportFieldImpl;
    import com.icreate.dataconnect.domain.report.ReportSectionImpl;
    import com.icreate.dataconnect.report.bo.ReportBO;
    
    public class SectionFieldFormController extends SimpleFormController{
    	private ReportBO reportBO;
    	private DictionaryBO dictionaryBO;
    	
    	protected ModelAndView onSubmit(HttpServletRequest request,
    			HttpServletResponse response, Object command, BindException errors)
    			throws Exception {
    		
    		ReportFieldImpl reportField= (ReportFieldImpl)command;
    		reportField.setSectionID(Integer.parseInt(request.getParameter("sectionID")));
    		
    		reportBO.createReportField(reportField);
    		RedirectView redirectView;
    		
    		Map map = new HashMap();
    		map.put("sectionID", reportField.getSectionID());
    		
    		if(request.getParameter("bizTermID") == null){
    			redirectView = new RedirectView("listreportsectionfields.htm");
    		}else{
    			redirectView = new RedirectView("reportfielddetail.htm");
    			map.put("businessTermID", reportField.getBusinessTermID());
    		}
    	    
    	    redirectView.setAttributesMap(map);
    		return new ModelAndView(redirectView);
    	}
    	
    	
    	@Override
    	protected Map referenceData(HttpServletRequest request) throws Exception {
    		Map map = new HashMap();
    		List businessTermsList = new ArrayList();
    		List businessTermNamesList = new ArrayList();
    		
    		for(BusinessTerm businessTerm: dictionaryBO.getAllBusinessTerms()){
    			businessTermsList.add(((BusinessTermImpl)businessTerm).getBusinessTermID());
    			businessTermNamesList.add(((BusinessTermImpl)businessTerm).getBusinessTermName());
    		}
    		map.put("businessTerms", dictionaryBO.getAllBusinessTerms());
    		
    		ReportSectionImpl section = (ReportSectionImpl)reportBO.getReportSectionDetail(Integer.parseInt(request.getParameter("sectionID")));
    		map.put("section",section);
    		map.put("report", reportBO.getReportDetails(section.getReportID()));
    		return map;
    	}
    
    	@Override
    	protected Object formBackingObject(HttpServletRequest request)
    	throws Exception {
    			ReportField reportField= null;
    			
    			if(request.getParameter("bizTermID") != null){
    				reportField = reportBO.getReportFieldDetail(Integer.parseInt(request.getParameter("sectionID")),Integer.parseInt(request.getParameter("bizTermID")));
    				return reportField;
    			}
    			return super.formBackingObject(request);
    	}
    
    	public ReportBO getReportBO() {
    		return reportBO;
    	}
    	public void setReportBO(ReportBO reportBO) {
    		this.reportBO = reportBO;
    	}
    
    
    	public DictionaryBO getDictionaryBO() {
    		return dictionaryBO;
    	}
    
    
    	public void setDictionaryBO(DictionaryBO dictionaryBO) {
    		this.dictionaryBO = dictionaryBO;
    	}
    	
    }
    Thanks,
    Sreedhar

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •