How to set event for submit button in wicket and catch it in home page

Viewed 21

My problem is when I want to click on the submit button, the data will display in home page.

You will say that I should use a form for this, but I'm working only with panel and I can't add form inside of it.

So I want to create an event in this button and catch it in the home page.

This is my HTML code :

<wicket:panel>
        <div class="input-group mb-3">
            <div class="input-form">
                    <div class="form-select-sm">
                        
                        <select wicket:id="option" class="form-select form-select-sm" aria-label=".form-select-sm example">
                             <option selected>Select your coffee</option>
                             <option>coffeeName</option>
                        </select>
                    </div>
                    <br>
                    <div class="input-group mb-3">
                            <input type="number" class="form-control" wicket:id="quantity-of-coffee" placeholder="Quantity" aria-label="Username" aria-describedby="basic-addon1" min="1" max="50">
                    </div>
                
                    <div class="submit-button">
                        <button wicket:id="submit" type="submit" class="btn btn-success">Add</button>
                    </div>
                    <br>
                    <div>
                        <wicket:enclosure>
                           <div class="alert alert-success" wicket:id="fb" role="alert">
                              
                           </div>
                        </wicket:enclosure>
                        
                    </div>
            </div>              
        </div>
</wicket:panel>

and this is my customPanel class :

public class CustomPanel extends Panel{

public static final String  DDC_MARKUP_ID="option";
public static final String  QUANTITY_MARKUP_ID="quantity-of-coffee";
public static final String  SUBMIT_BUTTON_MARKUP_ID="submit";
public static final String  FEEDBACKPANEL_MARKUP_ID="fb";
public static final String  ORDER_VIEW_MARKUP_ID="order-content";
public static final String  COFFEE_NAME_MARKUP_ID="coffee-name";
public static final String  COFFEE_QUANTITY_MARKUP_ID="quantity";
public static final String  COFFEE_PRICE_MARKUP_ID="coffee-price";
public static final String  ORDER_PRICES_VIEW_MARKUP_ID="order-total";
public static final String  TOTAL_DISCOUNT_MARKUP_ID="discount";
public static final String  ORDER_TOTAL_MARKUP_ID="total";

private   IModel<List<CoffeQuantityAssociation>> listOfCoffees ;
private   IModel<Order> order ;
private   String choice="";


public CustomPanel(String id,IModel<Order> order,IModel<List<CoffeQuantityAssociation>> listOfCoffes) {
    super(id);
    this.order = order;
    this.listOfCoffees=listOfCoffes;
}

@Override
protected void onInitialize() {
    super.onInitialize();
    FeedbackPanel feedbackPanel=new FeedbackPanel(FEEDBACKPANEL_MARKUP_ID);
    Model<Integer> quantityModel=Model.of(0);
    List<Coffe> coffeeListMockData=MockData.getListOfCoffes();
    IModel<List<String>> coffeNamesListMockData=Model.ofList(MockData.getCoffeNames(coffeeListMockData));
    DropDownChoice<String> ddc=new DropDownChoice<String>(DDC_MARKUP_ID,coffeNamesListMockData.getObject());
    ddc.setDefaultModel(coffeNamesListMockData);
    add(ddc);
    add(new NumberTextField<Integer>(QUANTITY_MARKUP_ID,quantityModel)
    .setLabel(new Model<>(QUANTITY_MARKUP_ID))
    .setRequired(true))
    .setDefaultModel(Model.of(QUANTITY_MARKUP_ID))
    .add(RangeValidator.range(0, 50));
    
    add(feedbackPanel);
    get(FEEDBACKPANEL_MARKUP_ID).setVisible(false);
    //choice=ddc.getModelObject();
    Button submit= new Button(SUBMIT_BUTTON_MARKUP_ID);
    // here i want to create the event

}

1 Answers

From the question's comments:

@martin-g I want to display data in home page by clicking on 
the submit button of the panel. How can I do this with creating
an event in panel class and catch it in home page ? 

It depends how you navigate from the current page (the one with the panel) to the home page.

You could use any of the following:

1) setResponsePage(new HomePage(myEvent));

2) setResponsePage(HomePage.class, new PageParameters().add("myParamKey", "someValue"));

3) in the button#onSubmit(): MySession.get().setAttribute("key", "value") 
    in the HomePage: String value = MySession.get().getAttribute("key")
Related