Announcement Announcement Module
Collapse
No announcement yet.
FormController question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FormController question

    Can anyone help me? I am desperate about this problem :-(

    I have 2 classes Town -*------1-> Country :
    Code:
    public class Town extends BaseObject implements Serializable {
    	protected String postalCode;
    	protected String name_i18n;
    	protected Country country;
    // getters and setters
    }
    Code:
    public class Country extends BaseObject implements Serializable {
    	protected String code;
    	protected String name_i18n;
    // getters and setters
    }
    All DAO and Services works correctly, even whole Countr, even displaying Town in SpringMVC. Problem came when I try to save class Town - which compose class Country:

    ## Error message ##
    Code:
    Failed to convert property value of type [java.lang.String] to required
    type [org.appfuse.model.Country] for property 'country'
    ## Error Logs ##
    Code:
    [appfuse] DEBUG [http-8080-Processor24]
    TownFormController.formBackingObject(78) | entering 'formBackingObject'
    method...
    [appfuse] DEBUG [http-8080-Processor24]
    SimpleFormController.processFormSubmission(218) | Data binding errors: 1
    ## TownFormController ##
    Code:
    public class TownFormController extends BaseFormController {
       private TownManager townManager = null;
    
       public void setTownManager(TownManager townManager) {
           this.townManager = townManager;
       }
    
       public ModelAndView onSubmit(HttpServletRequest request,
               HttpServletResponse response, Object command, BindException errors)
               throws Exception {
           if (log.isDebugEnabled()) {
               log.debug("entering 'onSubmit' method...");
           }
           Town town = (Town) command;
    
           Locale locale = request.getLocale();
    
           if (request.getParameter("delete") != null) {
               townManager.removeTown(town.getPostalCode().toString());
    
               saveMessage(request, getText("town.deleted", locale));
           } else if (request.getParameter("update") != null) {
    
               townManager.updateTown(town);
    
               saveMessage(request, getText("country.updated", locale));
    
               return new ModelAndView("redirect:editTown.html", "id", town
                       .getPostalCode());
           } else {
               try {
                   townManager.saveTown(town);
               } catch (DataAccessException e) {
                   // error message
                   saveError(request, getText("town.exists", locale));
                   request.setAttribute("town", town);
                   return new ModelAndView(getFormView());
               }
               saveMessage(request, getText("country.added", locale));
           }
           return new ModelAndView(getSuccessView());
       }
    
       protected Object formBackingObject(HttpServletRequest request)
               throws Exception {
           if (log.isDebugEnabled()) {
               log.debug("entering 'formBackingObject' method...");
           }
           Country cc = new Country();
           cc.setCode("CZ");
    
           String id = request.getParameter("id");
           Town town = null;
    
           if (!StringUtils.isEmpty(id)) {
               town = townManager.getTown(id);
               return town;
           } else {
               town = new Town();
           }
           return town;
       }
    }
    Any help will be very appreciated.

  • #2
    The problem seems to come from the part which binds the request onto the command object.
    See this log :

    Failed to convert property value of type [java.lang.String] to required
    type [org.appfuse.model.Country] for property 'country'
    It means that a part of the request (of type String) should be converted to a Country (since you have a Town as command object with a setCountry) and that spring doesn't know how to do this. You should add a propertyEditor suitable to convert a String to a Country and register it in the initBinder method. That should do the trick.

    Comment


    • #3
      Thank you ojolly.

      Does anyone know about some example or tutorial how to create propertyEditor?
      I cant find anything usable.

      Comment


      • #4
        Spring contains several PropertyEditors. For instance, CustomNumberEditor or CustomBooleanEditor, you could use those as an example.

        Comment


        • #5
          It's pretty simple: extend PropertyEditorSupport. There are two methods you should override: 'String getAsText()' and setAsText(String text). getAsText() can obtain the value it should transform to a string using 'Object getValue()' whereas setAsText(String text) should transform the text given to an Object and set it using setValue(Object). Something like this:

          Code:
          DatePropertyEditor extends PropertyEditorSupport {
          
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
          
            void setAsText(String text) {
              setValue(df.parse(text));
            }
          
            String getAsText() {
              return df.format((Date)getValue());
            }
          }

          Comment


          • #6
            radone,
            if country id is all you need in your controller, you can bind the form field to city.country.id binding of mapping it to city.country. That way, you do not need to create / use any specific PropertyEditorSupport. Spring does handle the binding to sub-objects properties out of the box.
            HTH

            Comment


            • #7
              Thanks

              Comment


              • #8
                Hi radone,
                If your problem was solved, would you plz send me a copy of your form controller and the form JSP because I'm facing the same problem.
                Thanks in Advance.
                Sherihan.

                Comment

                Working...
                X