Announcement Announcement Module
Collapse
No announcement yet.
CustomEditor for managing a child class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CustomEditor for managing a child class

    When I try to save a new Item (parent class) I got an error because the type (DmItemType child class) property is null
    It looks like that the forrm doesn't use the setAsText() method
    I'm able to understand the reason "item.type" is null
    Any idea where I'm wrong or do you know where I can find an example?

    Thanks

    Here the code:

    Item.java
    Code:
    public class Item implements Serializable {
      private Long id = null;
      private DmItemType type;
      private String name;
    ....
      setters & getters
    }
    DmItemType.java:
    Code:
    public class DmItemType implements Serializable {
      private Long id = null;
      private String name;
    .....
      setters & getters
    }
    ItemFormController.java
    Code:
    public ItemFormController() {
      setSessionForm(false);
      setBindOnNewForm(true);
    }
    
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
      // customeditor for DmItemType
      binder.registerCustomEditor(DmItemType.class,	new DmItemTypePropertyEditor(dmMgr));
    }
    
    public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    			throws Exception {
      Item item = (Item) command;
      if (request.getParameter("delete") != null) {
        mgr.removeItem(item.getId().toString());
        request.getSession().setAttribute("message", getText("item.deleted", item.getName()));
      } else {
        mgr.saveItem(item);
        request.getSession().setAttribute("message", getText("item.saved", item.getName()));
      }
    
      return new ModelAndView(getSuccessView());
    }
    
    protected Object formBackingObject(HttpServletRequest request)
    			throws ServletException {
    
      String itemId = request.getParameter("id");
      Item item = null;
    
      if ((itemId != null) && !"".equals(itemId)) {
        item = mgr.getItem(itemId);
        if (item == null) {
          item = new Item();
        }
      } else {
        String itemType = request.getParameter("itemType");
        if ((itemType != null) && !"".equals(itemType)) {
          DmItemType dmItemType = dmMgr.getDmItemTypeByName(itemType);
          item = new Item();
          if (dmItemType != null) {
            item.setType(dmItemType);
          }
        } else {
          item = new Item();
        }
      }
      return item;
    }
    DmItemTypePropertyEditor.java
    Code:
    public class DmItemTypePropertyEditor extends PropertyEditorSupport {
    	private DmManager dmMgr;
    	public DmItemTypePropertyEditor(DmManager dmMgr){
    		this.dmMgr = dmMgr;
    	}
    	
    	public String getAsText() {
    		DmItemType dmItemType = (DmItemType) getValue();
    		return dmItemType.getName();
    	}
    
    	public void setAsText(String text) throws IllegalArgumentException {
    		DmItemType dmItemType = null;
    		if (text != null && !"".equals(text)){
    			dmItemType = dmMgr.getDmItemTypeByName(text);
    		}
    		setValue(dmItemType);
    	}
    }
    itemForm.jsp
    Code:
    <form method="post" action="<c&#58;url value="/admin/itemForm.html"/>" name="itemForm">
    
    <input type="hidden" name="item.id" value="<c&#58;out value="$&#123;item.id&#125;"/>">
    
    <table class="detail">
    <tr>
        <th><label for="type" class="required">* <fmt&#58;message key="item.type"/>&#58;</label></th>
        <td>
            <spring&#58;bind path="item.type">
            <input type="text" disabled name="type" value="$&#123;status.value&#125;"/>
            <span class="fieldError">$&#123;status.errorMessage&#125;</span>
            </spring&#58;bind>
        </td>
    </tr>
    <tr>
        <th><label for="name" class="required">* <fmt&#58;message key="item.name"/>&#58;</label></th>
        <td>
            <spring&#58;bind path="item.name">
            <input type="text" name="name" value="$&#123;status.value&#125;"/>
            <span class="fieldError">$&#123;status.errorMessage&#125;</span>
            </spring&#58;bind>
        </td>
    </tr>
    <tr>
        <th><label for="description"><fmt&#58;message key="item.description"/>&#58;</label></th>
        <td>
            <spring&#58;bind path="item.description">
            <input type="text" name="description" value="$&#123;status.value&#125;"/>
            <span class="fieldError">$&#123;status.errorMessage&#125;</span>
            </spring&#58;bind>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <input type="submit" class="button" name="save" value="<fmt&#58;message key="item.save"/>"/>
          <c&#58;if test="$&#123;not empty param.id&#125;">
            <input type="submit" class="button" name="delete" value="<fmt&#58;message key="item.delete"/>"/>
          </c&#58;if>
          	<input type="submit" class="button" name="cancel" value="<fmt&#58;message key="item.cancel"/>" onclick="bCancel=true"/>
        </td>
    </tr>
    </table>
    </form>
    error

    Code:
    DEBUG - ItemFormController.onSubmit&#40;81&#41; | entering 'onSubmit' method...
    ------------------
    WARN - DispatcherServlet.processHandlerException&#40;872&#41; | Handler execution resulted in exception - forwarding to resolved error view
    org.springframework.orm.hibernate.HibernateSystemException&#58; not-null property references a null or transient value&#58; it.tud.webaccess.model.Item.type; nested exception is net.sf.hibernate.PropertyValueException&#58; not-null property references a null or transient value&#58; it.tud.webaccess.model.Item.type
    net.sf.hibernate.PropertyValueException&#58; not-null property references a null or transient value&#58; it.tud.webaccess.model.Item.type
    	at net.sf.hibernate.impl.SessionImpl.checkNullability&#40;SessionImpl.java&#58;1287&#41;
    	at net.sf.hibernate.impl.SessionImpl.doSave&#40;SessionImpl.java&#58;939&#41;
    	at net.sf.hibernate.impl.SessionImpl.doSave&#40;SessionImpl.java&#58;868&#41;
    	at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier&#40;SessionImpl.java&#58;786&#41;
    	at net.sf.hibernate.impl.SessionImpl.save&#40;SessionImpl.java&#58;749&#41;
    	at net.sf.hibernate.impl.SessionImpl.saveOrUpdate&#40;SessionImpl.java&#58;1398&#41;
    	at org.springframework.orm.hibernate.HibernateTemplate$16.doInHibernate&#40;HibernateTemplate.java&#58;532&#41;
    	at org.springframework.orm.hibernate.HibernateTemplate.execute&#40;HibernateTemplate.java&#58;312&#41;
    	at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate&#40;HibernateTemplate.java&#58;529&#41;
    	at it.tud.webaccess.dao.hibernate.ItemDAOHibernate.saveItem&#40;ItemDAOHibernate.java&#58;32&#41;
    	at it.tud.webaccess.service.impl.ObjManagerImpl.saveItem&#40;ObjManagerImpl.java&#58;36&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;585&#41;
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection&#40;AopUtils.java&#58;284&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint&#40;ReflectiveMethodInvocation.java&#58;155&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;122&#41;
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke&#40;TransactionInterceptor.java&#58;56&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;144&#41;
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke&#40;JdkDynamicAopProxy.java&#58;174&#41;
    	at $Proxy0.saveItem&#40;Unknown Source&#41;
    	at it.tud.webaccess.web.ItemFormController.onSubmit&#40;ItemFormController.java&#58;86&#41;
    	at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission&#40;SimpleFormController.java&#58;248&#41;
    	at it.tud.webaccess.web.ItemFormController.processFormSubmission&#40;ItemFormController.java&#58;52&#41;
    	at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal&#40;AbstractFormController.java&#58;235&#41;
    	at org.springframework.web.servlet.mvc.AbstractController.handleRequest&#40;AbstractController.java&#58;128&#41;
    	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle&#40;SimpleControllerHandlerAdapter.java&#58;44&#41;
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch&#40;DispatcherServlet.java&#58;675&#41;
    	at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;623&#41;
    	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;384&#41;
    	at org.springframework.web.servlet.FrameworkServlet.doPost&#40;FrameworkServlet.java&#58;353&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;252&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;173&#41;
    	at org.displaytag.filter.ResponseOverrideFilter.doFilter&#40;ResponseOverrideFilter.java&#58;125&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;202&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;173&#41;
    	at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage&#40;PageFilter.java&#58;118&#41;
    	at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter&#40;PageFilter.java&#58;52&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;202&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;173&#41;
    	at org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal&#40;OpenSessionInViewFilter.java&#58;172&#41;
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter&#40;OncePerRequestFilter.java&#58;76&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;202&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;173&#41;
    	at it.tud.webaccess.util.filter.GZIPFilter.doFilter&#40;GZIPFilter.java&#58;51&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;202&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;173&#41;
    	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
    	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;178&#41;
    	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;126&#41;
    	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;105&#41;
    	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;107&#41;
    	at org.apache.catalina.connector.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;148&#41;
    	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;825&#41;
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;738&#41;
    	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket&#40;PoolTcpEndpoint.java&#58;526&#41;
    	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt&#40;LeaderFollowerWorkerThread.java&#58;80&#41;
    	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;684&#41;
    	at java.lang.Thread.run&#40;Thread.java&#58;595&#41;

  • #2
    The type form element will not be posted because it is disabled. Disabled elements aren't submitted with the form. So you might want to change that to a hidden input.
    Also, you probably want to change your other hidden input from "item.id" to "id".
    Other than that, the CustomEditor looks correct.

    Comment


    • #3
      Originally posted by dgynn
      The type form element will not be posted because it is disabled. Disabled elements aren't submitted with the form. So you might want to change that to a hidden input.
      Also, you probably want to change your other hidden input from "item.id" to "id".
      Other than that, the CustomEditor looks correct.
      Thanks! Now it works

      Comment

      Working...
      X