Click will always follow it's life cycle. It onInit is used to build up the page controls. If this step doesn't occur no processing or events would be possible. In other words your onCancel won't fire.
Nothing stops you from conditionally reacting to state you receive from the browser though ie. checking if printProviderMockEnt has a value or not. Not sure from your question if you expect printProviderMockEnt to have a value when submitting the form? If you want the value to be passed to the page on subsequent submits you need to add it's value to the form through a HiddenField. regards Bob On Wed, May 15, 2013 at 3:45 AM, Kristian Lind <[email protected]> wrote: > But I dont see any reason why the OnInit method should be called... > especially not on a cancel with a setRedirect > > The problem that I am facing here is that my > @Bindable > private PrintProviderMockEnt printProviderMockEnt; > > is binded first time i go to the page. And the OnInit method is called. > > When pressing the submit button to save my changes... The binding is not > happening, and the printProviderMockEnt is null. > > public class CustomerPrintProvider extends BorderedPage { > private static final Logger logger = > LoggerFactory.getLogger(CustomerPrintProvider.class); > private static final String PAGE_TITLE_CREATE = "Add Test Print > Provider"; > private static final String PAGE_TITLE_EDIT = "Edit Test Print Provider"; > > @Bindable > private PrintProviderMockEnt printProviderMockEnt; > > private PrintProviderMockSBBeanLocal printProviderMockSBBeanLocal = > SessionBeanManager.getPrintProviderMockSBBeanLocal(); > private DashboardSBBeanLocal dashboardSBBeanLocal = > SessionBeanManager.getDashboardSBBeanLocal(); > private EditForm form; > private boolean newTestPrintProvider = false; > > private PrintCustomerEnt printCustomerEnt; > > @Override > public void onRender() { > super.onRender(); > } > > @Override > public final void onInit() { > super.onInit(); > String customerName = > getContext().getRequest().getUserPrincipal().getName(); > try { > printCustomerEnt = dashboardSBBeanLocal.findCustomer(customerName); > } catch (BusinessException e) { > addModel("error", e.getMessage()); > return; > } catch (SystemException e) { > // TODO show errorpage > logger.error(e.getMessage(), e); > } > form = new EditForm("form"); > if (printProviderMockEnt == null) { > newTestPrintProvider = true; > } else { > form.nameField.setValue(printProviderMockEnt.getName()); > form.nameField.setDisabled(true); > } > addModel("newTestPrintProvider", newTestPrintProvider); > addControl(form); > } > > @Override > public final String getTitle() { > if (printProviderMockEnt == null) { > return PAGE_TITLE_CREATE; > } else { > return PAGE_TITLE_EDIT; > } > } > > public final boolean onSave() throws SystemException { > if (form.isValid()) { > try { > if (newTestPrintProvider) { > printProviderMockEnt = new PrintProviderMockEnt(); > printProviderMockEnt.setName(form.nameField.getValue()); > printProviderMockEnt.setPrintCustomerEnt(printCustomerEnt); > printProviderMockSBBeanLocal.saveNewPrintProvider(printProviderMockEnt); > } else { > PrintProviderMockEnt printProvider = > printProviderMockSBBeanLocal.getPrintProvider((Long)form.printProviderMockEntId.getValueObject()); > printProvider.setName(form.nameField.getValue()); > printProviderMockSBBeanLocal.updatePrintProvider(printProvider); > } > form.clearValues(); > setRedirect(CustomerPrintProviders.class); > } catch (BusinessException e) { > addModel("error", e.getMessage()); > return false; > } catch (SystemException e) { > logger.error(e.getMessage(), e); > throw e; > } > } > return true; > } > > public final boolean onCancel() { > setRedirect(CustomerPrintProviders.class); > return false; > } > > private class EditForm extends Form { > > private HiddenField printProviderMockEntId; > private TextField nameField; > > public static final String DEFAULT_PRINT_PROVIDER_KEY = "Default"; > public static final String DEFAULT_PRINT_PROVIDER_VALUE = ""; > > public EditForm(final String name) { > super(name); > printProviderMockEntId = new HiddenField("printProviderMockEntId", > Long.class); > if (printProviderMockEnt != null) { > printProviderMockEntId.setValueObject(printProviderMockEnt.getId()); > } > add(printProviderMockEntId); > nameField = new TextField("name", "Name", true); > nameField.setFocus(true); > add(nameField); > > Submit save = new Submit("save", "Save"); > save.setActionListener(new ActionListener() { > public boolean onAction(final Control control) { > try { > return onSave(); > } catch (SystemException e) { > // TODO show errorpage > logger.error(e.getMessage(), e); > return false; > } > } > }); > > save.addStyleClass("btn btn-primary"); > add(save); > > Submit cancel = new Submit("cancel", "Cancel"); > cancel.setActionListener(new ActionListener() { > public boolean onAction(final Control control) { > return onCancel(); > } > }); > > cancel.addStyleClass("btn"); > add(cancel); > > ClickUtils.bind(save); > ClickUtils.bind(cancel); > if (isFormSubmission() && !save.isClicked() && !cancel.isClicked()) { > setValidate(false); > } > > setButtonAlign(Form.ALIGN_RIGHT); > } > > @Override > public void validate() { > super.validate(); > } > } > } > > > > > > > > On Tue, May 14, 2013 at 6:28 PM, Malcolm Edgar <[email protected]>wrote: > >> Hi Kris, >> >> The page onInit() method is generally called, please see the sequence >> diagram below for more info: >> >> http://click.apache.org/docs/click-cheat-sheet.pdf >> >> regards >> >> >> On Wed, May 15, 2013 at 10:54 AM, Kristian Lind <[email protected]> wrote: >> >>> Hi, I have a page with a Form, with submit and cancel buttons. >>> >>> When I go to the page with a PageLink with OnInit method is called... >>> nice.. :) >>> >>> But why is the OnInit method called when I press the submit button ?? >>> and even more strange, when I press the cancel button. >>> >>> Best regards >>> >>> Kris >>> >> >> > > > -- > Best regards > > Kristian Lind >
