You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Tony Nelson <tn...@starpoint.com> on 2011/06/13 17:54:05 UTC

onPrepare(Integer id)

I'm trying to create a simple form that will work for both Add and Edit.  I'm trying to use onPrepare to setup the database object and the code works fine for an existing object.  When I try to use the form with a new object (id = null) it appears that onPrepare isn't called, so I end up with null pointer exceptions trying to build the form.

Here is my simple controller:

package com.starpoint.helpdesk.pages.office;

import com.starpoint.helpdesk.business.OfficeLogic;
import com.starpoint.helpdesk.domain.Office;
import com.sun.istack.internal.Nullable;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;


/**
 */
public class EditOffice {

    @Inject
    private Logger logger;

    @Inject
    private OfficeLogic officeLogic;

    @Persist
    private Integer officeId;

    @Property
    private Office office;

    public Integer getOfficeId() {
        return officeId;
    }

    @Log
    public void setOfficeId(@Nullable Integer officeId) {
        this.officeId = officeId;
    }

    @Log
    void onPrepare(Integer officeId) {
        if (office == null) {
            office = officeId == null ? new Office() : officeLogic.getOffice(officeId);
        }
    }
    void onActivate(Integer officeId) {
        this.officeId = officeId;
    }

    Integer onPassivate() {
        return officeId;
    }

    public Object onSuccess() {
        logger.info("**************: " + office.toString());
        return this;
    }
}


And my form:

<html t:type="layout" title="helpdesk Index"
      t:sidebarTitle="Current Time"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
      xmlns:p="tapestry:parameter">

<t:form t:id="editOfficeForm" t:context="officeId">
    <t:errors/>
    <table>
        <tr>
            <td><t:label for="officeName">Office Name</t:label></td>
            <td><t:textfield t:id="officeName" value="prop:office.officeName"></t:textfield></td>
        </tr>

        <tr>
            <td colspan="2">
                <t:submit t:id="saveOffice" class="button">Save Office</t:submit>
            </td>
        </tr>
    </table>

</t:form>

</html>

What am I missing?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: onPrepare(Integer id)

Posted by Nillehammer <ta...@winfonet.eu>.
Hi Tony,

 >> <t:form t:id="editOfficeForm" t:context="officeId">
Given this line in your template, Tapestry will just append nothing to 
the url, if officeId is null. This means the context is simply not 
there. When processing the post request, the correct signature for an 
eventhanlder wit no context would be one with no parameters i.e. 
"onPrepare()". This method should be called, if officeId is null.

As having multple eventHandlers for basically the same event just 
because of different amount of parameters may clutter the code, I 
suggest implementing just a single one with 
org.apache.tapestry5.EventContext as parameter.

Cheers, nillehammer


Am 13.06.2011 17:54, schrieb Tony Nelson:
> I'm trying to create a simple form that will work for both Add and Edit.  I'm trying to use onPrepare to setup the database object and the code works fine for an existing object.  When I try to use the form with a new object (id = null) it appears that onPrepare isn't called, so I end up with null pointer exceptions trying to build the form.
>
> Here is my simple controller:
>
> package com.starpoint.helpdesk.pages.office;
>
> import com.starpoint.helpdesk.business.OfficeLogic;
> import com.starpoint.helpdesk.domain.Office;
> import com.sun.istack.internal.Nullable;
> import org.apache.tapestry5.annotations.Log;
> import org.apache.tapestry5.annotations.Persist;
> import org.apache.tapestry5.annotations.Property;
> import org.apache.tapestry5.ioc.annotations.Inject;
> import org.slf4j.Logger;
> t:context="officeId"
>
> /**
>   */
> public class EditOffice {
>
>      @Inject
>      private Logger logger;
>
>      @Inject
>      private OfficeLogic officeLogic;
>
>      @Persist
>      private Integer officeId;
>
>      @Property
>      private Office office;
>
>      public Integer getOfficeId() {
>          return officeId;
>      }
>
>      @Log
>      public void setOfficeId(@Nullable Integer officeId) {
>          this.officeId = officeId;
>      }
>
>      @Log
>      void onPrepare(Integer officeId) {
>          if (office == null) {
>              office = officeId == null ? new Office() : officeLogic.getOffice(officeId);
>          }
>      }
>      void onActivate(Integer officeId) {
>          this.officeId = officeId;
>      }
>
>      Integer onPassivate() {
>          return officeId;
>      }
>
>      public Object onSuccess() {
>          logger.info("**************: " + office.toString());
>          return this;
>      }
> }
>
>
> And my form:
>
> <html t:type="layout" title="helpdesk Index"
>        t:sidebarTitle="Current Time"
>        xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
>        xmlns:p="tapestry:parameter">
>
> <t:form t:id="editOfficeForm" t:context="officeId">
>      <t:errors/>
>      <table>
>          <tr>
>              <td><t:label for="officeName">Office Name</t:label></td>
>              <td><t:textfield t:id="officeName" value="prop:office.officeName"></t:textfield></td>
>          </tr>
>
>          <tr>
>              <td colspan="2">
>                  <t:submit t:id="saveOffice" class="button">Save Office</t:submit>
>              </td>
>          </tr>
>      </table>
>
> </t:form>
>
> </html>
>
> What am I missing?
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
http://www.winfonet.eu


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: onPrepare(Integer id)

Posted by "dragan.sahpaskix@gmail.com" <dr...@gmail.com>.
Hi,
You are not missing anything.

Insert an onPrepare() with no arguments.
tapestry will not call onPrepare(Integer officeId) with null for officeId.

Cheers,
Dragan Sahpaski



On Mon, Jun 13, 2011 at 5:54 PM, Tony Nelson <tn...@starpoint.com> wrote:

> I'm trying to create a simple form that will work for both Add and Edit.
>  I'm trying to use onPrepare to setup the database object and the code works
> fine for an existing object.  When I try to use the form with a new object
> (id = null) it appears that onPrepare isn't called, so I end up with null
> pointer exceptions trying to build the form.
>
> Here is my simple controller:
>
> package com.starpoint.helpdesk.pages.office;
>
> import com.starpoint.helpdesk.business.OfficeLogic;
> import com.starpoint.helpdesk.domain.Office;
> import com.sun.istack.internal.Nullable;
> import org.apache.tapestry5.annotations.Log;
> import org.apache.tapestry5.annotations.Persist;
> import org.apache.tapestry5.annotations.Property;
> import org.apache.tapestry5.ioc.annotations.Inject;
> import org.slf4j.Logger;
>
>
> /**
>  */
> public class EditOffice {
>
>    @Inject
>    private Logger logger;
>
>    @Inject
>    private OfficeLogic officeLogic;
>
>    @Persist
>    private Integer officeId;
>
>    @Property
>    private Office office;
>
>    public Integer getOfficeId() {
>        return officeId;
>    }
>
>    @Log
>    public void setOfficeId(@Nullable Integer officeId) {
>        this.officeId = officeId;
>    }
>
>    @Log
>    void onPrepare(Integer officeId) {
>        if (office == null) {
>            office = officeId == null ? new Office() :
> officeLogic.getOffice(officeId);
>        }
>    }
>    void onActivate(Integer officeId) {
>        this.officeId = officeId;
>    }
>
>    Integer onPassivate() {
>        return officeId;
>    }
>
>    public Object onSuccess() {
>        logger.info("**************: " + office.toString());
>        return this;
>    }
> }
>
>
> And my form:
>
> <html t:type="layout" title="helpdesk Index"
>      t:sidebarTitle="Current Time"
>      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
>      xmlns:p="tapestry:parameter">
>
> <t:form t:id="editOfficeForm" t:context="officeId">
>    <t:errors/>
>    <table>
>        <tr>
>            <td><t:label for="officeName">Office Name</t:label></td>
>            <td><t:textfield t:id="officeName"
> value="prop:office.officeName"></t:textfield></td>
>        </tr>
>
>        <tr>
>            <td colspan="2">
>                <t:submit t:id="saveOffice" class="button">Save
> Office</t:submit>
>            </td>
>        </tr>
>    </table>
>
> </t:form>
>
> </html>
>
> What am I missing?
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>