Announcement Announcement Module
Collapse
No announcement yet.
Problem with typeMismatch (and formBackingObject) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with typeMismatch (and formBackingObject)

    (I tried searching the forum under the terms "typeMismatch formBackingObject" and found no help, so hopefully i am not repeating something)

    I am trying to use a single SimpleFormController to handle a form for creating an entry as well as updating an entry. Since the form data is persisted, once I added a hidden field for the primary key my form fails due to the following for editting current data with the form (4 is the primary key of the data being editted):

    Error: typeMismatch
    Field error in object 'retailer' on field 'id': rejected value []; codes [typeMismatch.retailer.id,typeMismatch.id,typeMisma tch.java.lang.Integer,typeMismatch]; arguments [MessageSourceResolvable: codes [retailer.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'id'; nested exception is java.lang.NumberFormatException: For input string: "4,4"]


    And the following when adding new data the form:

    Error: typeMismatch
    Field error in object 'retailer' on field 'id': rejected value []; codes [typeMismatch.retailer.id,typeMismatch.id,typeMisma tch.java.lang.Integer,typeMismatch]; arguments [MessageSourceResolvable: codes [retailer.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'id'; nested exception is java.lang.NumberFormatException: For input string: ""]


    SimpleFormController

    Code:
    public class RetailerCreateController extends SimpleFormController {
    	private RetailerService retailerService;
    	
    	public RetailerCreateController() {
    		this.setCommandClass(Retailer.class);
    	}
    
    	protected Object formBackingObject(HttpServletRequest request) throws Exception {
    		Retailer retailer = null;
    		
    		try {
    			Integer id = new Integer(request.getParameter("id"));
    			retailer = retailerService.getById(id);
    		} catch (NumberFormatException e) {}
    		
    		if (retailer == null) {
    			retailer = new Retailer();
    		}
    		
    		return retailer;
    	}
    	
    	protected void doSubmitAction(Object command) throws Exception {
    		Retailer retailerCommand = (Retailer)command;
    		retailerService.save(retailerCommand);
    	}
    
    	public void setRetailerService(RetailerService retailerService) {
    		this.retailerService = retailerService;
    	}
    }
    Bean Definition

    HTML Code:
    <bean id="retailerCreateController" class="net.cya.spring.mvc.admin.RetailerCreateController">
      <property name="retailerService"><ref bean="retailerService"/></property>
      <property name="commandName"><value>retailer</value></property>
      <property name="formView"><value>adminRetailerCreate</value></property>
      <property name="successView"><value>adminRetailerView</value></property>
      <property name="validator"><bean class="net.cya.spring.validator.RetailerValidator"/></property>
    </bean>
    Form

    HTML Code:
    <spring:hasBindErrors name="retailer">
      <c:if test="${errors.errorCount > 0}">
        Please correct the following errors:
        <ul>
          <c:forEach items="${errors.allErrors}" var="error">
            <li>
              <spring:message code="${error.code}"/>
            </li>
            DEBUGGINg: ${error}
          </c:forEach>
        </ul>
      </c:if>
    </spring:hasBindErrors>
    <form method="post">
      <input type="hidden" name="id" value="${retailer.id}"/>
      <table>
        <tr>
          <td align="right">Name:</td>
          <td><input name="name" value="${retailer.name}"/></td>
        </tr>
        <tr>
          <td align="right">Phone:</td>
          <td><input name="phone" value="${retailer.phone}"/></td>
        </tr>
        <tr>
          <td align="center" colspan="2">
            <input type="submit" value="Save"/>
          </td>
        </tr>
      </table>
    </form>

  • #2
    1) you can set isSessionForm to true in your controller, and remove that hidden field.
    2) When do you get this exception, while showForm, or after submit?

    Comment


    • #3
      endless,

      thanks for your help! item (1) did the trick. not that it matters now, but i'd encountered the errors after submit

      Comment

      Working...
      X