Announcement Announcement Module
Collapse
No announcement yet.
urgent:Problem with validator and wrapper object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • urgent:Problem with validator and wrapper object

    Spring 1.2.5
    I am using WebMVC to handle forms/validation etc...
    My problem is I have a page that has a form in it. There are other things on the page that were not part of the object the for needed so I made a wrapper object that had the 2 other objects that the jsp needed. Everything works great except for validation when a field is found invalid. Then I get the following
    Code:
    13:08:41,046 ERROR [DispatcherServlet] Could not complete request
    org.springframework.beans.NotReadablePropertyException: Invalid property 'appname' of bean class [edu.bju.aem.controller.wrapperobject.AppRightsWrapper]: Bean property 'appname' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    	at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:387)
    	at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:379)
    	at org.springframework.validation.BindException.rejectValue(BindException.java:197)
    	at edu.bju.aem.controller.validator.AppValidator.validate(AppValidator.java:32)
    Here is my controller
    Code:
      protected Object formBackingObject(HttpServletRequest req) throws Exception {
        AppRightsWrapper appWrapper = new AppRightsWrapper();
    	  App app = new App();
        if(Parameter.getInt(req.getParameter("appId")) != 0){
          app = appDAO.find(Parameter.getLong(req.getParameter("appId")));
        }
        else{
        	req.getSession().setAttribute("appId", 0);
        }
        appWrapper.setApp(app);
        appWrapper.setApps(appDAO.findAll());
        return appWrapper;
      }
    here is the jsp
    Code:
       <Spring:hasBindErrors name="appWrapper">
          Your form contains invalid data.  Please fix the errors and submit again.
        </Spring:hasBindErrors>
            </td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
          <Spring:bind path="appWrapper.app.appname">
            <td>Name&nbsp;</td>
            <td><input type="text" name="app.appname" size="35"  value="<c:out value="${status.value}" />"/><font color="red"><c:out value="${status.errorMessage}" /></font>
            </Spring:bind></td>
          </tr>
          <tr>
          <Spring:bind path="appWrapper.app.description">
            <td>Description&nbsp;</td>
            <td><textarea rows="3" cols="35" name="app.description"><c:out value="${status.value}" /></textarea><br />
    			<font color="red"><c:out value="${status.errorMessage}" /></font>
    			</Spring:bind>
    here is the validator
    Code:
     public void validate(Object o, Errors errors) {
    	  AppRightsWrapper appWrapper = (AppRightsWrapper)o;
        App app = appWrapper.getApp();
        if (app == null) {
          errors.rejectValue("name", "Error!", null, "Value required.");
        } else {
          if (app.getAppname() == null || "".equals(app.getAppname())) {
            errors.rejectValue("appname", "Value not present", null, "Name required.");
          }
          
          if (app.getDescription() == null) {
            errors.rejectValue("description", "Value not present", null, "Description required.");
          }
          
          if (app.getDescription().equals("")) {
            app.setDescription("no description given");
          }
        }
      }
    and finally here is the object
    Code:
    public class AppRightsWrapper {
    
    	private ArrayList<App> apps;
    	private App app;
    	
    	public AppRightsWrapper(){}
    
    	/**
    	 * @return Returns the app.
    	 */
    	public App getApp() {
    		return app;
    	}
    
    	/**
    	 * @param app The app to set.
    	 */
    	public void setApp(App app) {
    		this.app = app;
    	}
    
    	/**
    	 * @return Returns the apps. Intended to easily pass the selected app to the jsp
    	 */
    	public ArrayList<App> getApps() {
    		return apps;
    	}
    
    	/**
    	 * @param apps The apps to set. Intended to easily pass the selected app to the jsp
    	 */
    	public void setApps(ArrayList<App> apps) {
    		this.apps = apps;
    	}
    the app object
    Code:
    public class App  implements java.io.Serializable {
    
    
        // Fields    
    
         private long id;
         private String appname;
         private String description;
         private Set<AppRole> approles;
         private String entityPackage;
    
    
        // Constructors
    
        /** default constructor */
        public App() {
        }
        
        /** constructor with id */
        public App(long id) {
            this.id = id;
        }
        
        // Property accessors
    
        public long getId() {
            return this.id;
        }
        
        public void setId(long id) {
            this.id = id;
        }
    
        public String getAppname() {
            return this.appname;
        }
        
        public void setAppname(String appname) {
            this.appname = appname;
        }
    
        public String getDescription() {
            return this.description;
        }
        
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Set getApproles() {
            return this.approles;
        }
        
        public void setApproles(Set<AppRole> approles) {
            this.approles = approles;
        }
       
        /**
         * @return
         */
        public String getEntityPackage() {
          return entityPackage;
        }
    
        /**
         * @param newString
         */
        public void setEntityPackage(String newString) {
          entityPackage = newString;
        }
    
    }

  • #2
    Well I changed a few things and got it to not throw the error. It all appears to be working now except I don't see my text on my jsp for the error. It puts the has bind errors text but not the status.errorMessage text

    see below
    Code:
        <Spring:hasBindErrors name="appWrapper">
          Your form contains invalid data.  Please fix the errors and submit again.
        </Spring:hasBindErrors>
            </td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
          <Spring:bind path="appWrapper.app.appname">
            <td>Name&nbsp;</td>
            <td><input type="text" name="appname" size="35"  value="<c:out value="${status.value}" />"/><font color="red"><c:out value="${status.errorMessage}" /></font>
            </Spring:bind></td>
          </tr>
          <tr>
          <Spring:bind path="appWrapper.app.description">
            <td>Description&nbsp;</td>
            <td><textarea rows="3" cols="35" name="description"><c:out value="${status.value}" /></textarea>
    			<font color="red"><c:out value="${status.errorMessage}" /></font>
    			</Spring:bind>
    I changed my wrapper and validator to look like the following

    Code:
    public class AppRightsWrapper {
    
    	private ArrayList<App> apps;
    	private App app;
    	
    	public AppRightsWrapper(){}
    
    	/**
    	 * @return Returns the app.
    	 */
    	public App getApp() {
    		return app;
    	}
    
    	/**
    	 * @param app The app to set.
    	 */
    	public void setApp(App app) {
    		this.app = app;
    	}
    
    	/**
    	 * @return Returns the apps. Intended to easily pass the selected app to the jsp
    	 */
    	public ArrayList<App> getApps() {
    		return apps;
    	}
    
    	/**
    	 * @param apps The apps to set. Intended to easily pass the selected app to the jsp
    	 */
    	public void setApps(ArrayList<App> apps) {
    		this.apps = apps;
    	}
    
    	public String getAppname(){
    		return app.getAppname();
    	}
    	public String getDescription(){
    		return app.getDescription();
    	}
    	public void setAppname(String appname){
    		app.setAppname(appname);
    	}
    	public void setDescription(String description){
    		app.setDescription(description);
    	}
    }
    validator
    Code:
    public class AppValidator implements Validator {
    
      /* (non-Javadoc)
       * @see org.springframework.validation.Validator#supports(java.lang.Class)
       */
      public boolean supports(Class aClass) {
        return aClass.equals(AppRightsWrapper.class);
      }
    
      /* (non-Javadoc)
       * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
       */
      public void validate(Object o, Errors errors) {
    	  AppRightsWrapper appWrapper = (AppRightsWrapper)o;
    
        if (appWrapper.getApp() == null) {
          errors.rejectValue("name", "Error!", null, "Value required.");
        } else {
          if (appWrapper.getAppname() == null || "".equals(appWrapper.getAppname())) {
            errors.rejectValue("appname", "Value not present", null, "Name required.");
          }
          
          if (appWrapper.getDescription() == null) {
            errors.rejectValue("description", "Value not present", null, "Description required.");
          }
          
          if (appWrapper.getDescription().equals("")) {
        	  appWrapper.setDescription("no description given");
          }
        }
      }
    
    }

    Comment

    Working...
    X