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

  • setAsText not called

    Hello,
    I am trying to use a custom property editor in my code to bind a select to an object, but the method setAsText is not being called.
    The name of the class is Operacion and the property editor is OperacionPropertyEditor.
    Operacion contains two fields: codOperacion (long) and descOperacion (String).

    Here I show my code:


    public class OperacionPropertyEditor extends PropertyEditorSupport {

    private IDiccionarioServiceDAO diccionarioService;

    public void setAsText(String text) { //throws IllegalArgumentException {
    long codOperacion = -1;
    try {
    codOperacion = Long.parseLong(text);
    } catch (NumberFormatException ex) {
    throw new IllegalArgumentException("Operacion not numeric");
    }

    if (codOperacion < 0) {
    throw new IllegalArgumentException("Not valid value for operacion");
    }

    Operacion operacion = diccionarioService.getOperacion(codOperacion);

    if (operacion == null) {
    throw new IllegalArgumentException("Not valid value for operacion");
    }

    setValue(operacion);
    }

    public String getAsText() {
    Operacion operacion = (Operacion)getValue();

    if (operacion != null && operacion.getCodOperacion() > 0) {
    return String.valueOf(operacion.getCodOperacion());
    }
    return "";
    }

    public IDiccionarioServiceDAO getDiccionarioService() {
    return diccionarioService;
    }

    public void setDiccionarioService(IDiccionarioServiceDAO service) {
    this.diccionarioService = service;
    }
    }


    The code in the FormController is:

    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    logger.info("Binding operacion");
    OperacionPropertyEditor ope = (OperacionPropertyEditor)getApplicationContext().g etBean("operacionPropertyEditor");
    binder.registerCustomEditor(Operacion.class, ope);
    }


    And in the view:

    <tr>
    <td><b>Operaci&oacute;n:</b></td>
    <spring:bind path="command.operacion">
    <td width="30%">
    <select name="selOperacion">
    <c:forEach items="${listaOperacions}" var="op">
    <option value="<c:out value="${op.codOperacion}"/>" >
    <c:out value="${op.descOperacion}"/>
    </option>
    </c:forEach>
    </select>
    </td>
    <td width="60%">
    <font color="red"><c:out value="${status.errorMessage}"/></font>
    <td>
    </spring:bind>
    </tr>


    The application context file for spring contains:

    <bean id="operacionPropertyEditor" class="propertyEditors.OperacionPropertyEditor" singleton="false">
    <property name="diccionarioService">
    <ref bean="diccionarioServiceHibernate"/>
    </property>
    </bean>


    When I check the logs I see that the getAsText of the OperacionPropertyEditor is called, but not the setAsText method.
    I think it should be called when the form is submitted to bind the selected element with the Operacion object.

    Do you see anything wrong in my code?

    Thank you very much in advance,
    Manuel Valladares

  • #2
    Yes it should, are you absolutely sure it isn't Also, what is the value of your command object after the page is submitted?

    Comment


    • #3
      OK, I found the mistake, but I still don't understand why my previous version didn't work.

      I changed the name of the html select from 'selOperacion' to 'operacion' and now everything works fine.

      So my new jsp page looks like:

      <tr>
      <td><b>Operaci&oacute;n:</b></td>
      <spring:bind path="command.operacion">
      <td width="30%">
      <select name="operacion">
      <c:forEach items="${listaOperacions}" var="op">
      <option value="<c:out value="${op.codOperacion}"/>" >
      <c:out value="${op.descOperacion}"/>
      </option>
      </c:forEach>
      </select>
      </td>
      <td width="60%">
      <font color="red"><c:out value="${status.errorMessage}"/></font>
      <td>
      </spring:bind>
      </tr>


      Could someone explain why this makes a difference?
      I thought that the <spring:bind> tag was enough to mark the relation between the control and the field of the command form.

      Again, thank you very much.
      Manuel Valladares

      Comment


      • #4
        The request Paramter is going to contain operacion which is the one thats going to get mapped to ur object. I think Only the Parameters in your request parameters are mapped with the backing command object

        Comment

        Working...
        X