How to retrieve form value within onSubmit

2012-01-24 Thread Kayode Odeyemi
Hi,

I will like to understand the dynamics behind retrieving form values when
they are submitted.

I have the following code:

Dashboard.html
---
body
span wicket:id='mainNavigation'/

 form wicket:id=searchForm
 label for=searchSearch:/label
 input wicket:id=searchtextfield type=text / br /
 input type=submit/
/form

/body

Dashboard.java
--
public class Dashboard extends BasePage {

private static final Logger log =
Logger.getLogger(Dashboard.class.getName());

public Dashboard() {
super ();
final SearchForm searchForm = new SearchForm(searchForm);
add(searchForm);
}

public Dashboard(IModel model) {
super(model);
}

private class SearchForm extends Form implements
IFormSubmittingComponent {
TextField searchtextfield;
Search search = new Search();

public SearchForm(String id) {
super(id);
add(searchtextfield = new TextField(searchtextfield, new
Model() {

@Override
public Serializable getObject() {
return search.getText();
}

@Override
public void setObject(Serializable object) {
search.setText((String) object);
}
}));
}

@Override
public boolean getDefaultFormProcessing() {
return false;
}

@Override
public Form? getForm() {
return this;
}

@Override
public String getInputName() {
return searchtextfield;
}

@Override
public void onSubmit() {

//String value = (String)searchtextfield.getModelObject();
String value = searchtextfield.getValue();
log.log(Level.INFO, Search value is: , value);

// Using SolrJ to make the request to Solr
String url = http://localhost:8983/solr/;;
try {
// Connect to Solr Server
SolrServer server = new CommonsHttpSolrServer(url);
//server.query(null, METHOD.GET);
SolrQuery solrQuery = new SolrQuery(value);
QueryResponse response = server.query(solrQuery);

// log the response
log.log(Level.INFO, Search result is: ,
response.toString());
System.out.println(System print. Response is:  +
response.toString());
} catch (MalformedURLException ex) {

 Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
} catch(SolrServerException sse) {

}
}
}

}

Search.java
-
public class Search {

private String text;

public Search() {
}

public Search(String text) {
this.text = text;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

I really don't know what I'm doing wrong as I can't seem to be able to
retrieve the value entered into the textfield.

Any help will be much appreciated.

Thanks

-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde


Re: How to retrieve form value within onSubmit

2012-01-24 Thread Martin Grigorov
Hi,

See below.

On Tue, Jan 24, 2012 at 1:56 PM, Kayode Odeyemi drey...@gmail.com wrote:
 Hi,

 I will like to understand the dynamics behind retrieving form values when
 they are submitted.

 I have the following code:

 Dashboard.html
 ---
 body
        span wicket:id='mainNavigation'/

         form wicket:id=searchForm
             label for=searchSearch:/label
             input wicket:id=searchtextfield type=text / br /
             input type=submit/
        /form

    /body

 Dashboard.java
 --
 public class Dashboard extends BasePage {

    private static final Logger log =
 Logger.getLogger(Dashboard.class.getName());

    public Dashboard() {
        super ();
        final SearchForm searchForm = new SearchForm(searchForm);
        add(searchForm);
    }

    public Dashboard(IModel model) {
        super(model);
    }

    private class SearchForm extends Form implements
 IFormSubmittingComponent {
        TextField searchtextfield;
        Search search = new Search();

        public SearchForm(String id) {
            super(id);
            add(searchtextfield = new TextField(searchtextfield, new
 Model() {

replace with: new PropertyModel(search, text)


                @Override
                public Serializable getObject() {
                    return search.getText();
                }

                @Override
                public void setObject(Serializable object) {
                        search.setText((String) object);
                }
            }));
        }

        @Override
        public boolean getDefaultFormProcessing() {
            return false;
        }

remove this method


        @Override
        public Form? getForm() {
            return this;
        }

        @Override
        public String getInputName() {
            return searchtextfield;
        }

        @Override
        public void onSubmit() {

            //String value = (String)searchtextfield.getModelObject();
            String value = searchtextfield.getValue();
            log.log(Level.INFO, Search value is: , value);

            // Using SolrJ to make the request to Solr
            String url = http://localhost:8983/solr/;;
            try {
                // Connect to Solr Server
                SolrServer server = new CommonsHttpSolrServer(url);
                //server.query(null, METHOD.GET);
                SolrQuery solrQuery = new SolrQuery(value);
                QueryResponse response = server.query(solrQuery);

                // log the response
                log.log(Level.INFO, Search result is: ,
 response.toString());
                System.out.println(System print. Response is:  +
 response.toString());
            } catch (MalformedURLException ex) {

  Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
            } catch(SolrServerException sse) {

            }
        }
    }

 }

 Search.java
 -
 public class Search {

make this Serializable


    private String text;

    public Search() {
    }

    public Search(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
 }

 I really don't know what I'm doing wrong as I can't seem to be able to
 retrieve the value entered into the textfield.

 Any help will be much appreciated.

 Thanks

 --
 Odeyemi 'Kayode O.
 http://www.sinati.com. t: @charyorde



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com


Re: How to retrieve form value within onSubmit

2012-01-24 Thread Kayode Odeyemi
On Tue, Jan 24, 2012 at 12:09 PM, Martin Grigorov mgrigo...@apache.orgwrote:

 Hi,

 See below.

 On Tue, Jan 24, 2012 at 1:56 PM, Kayode Odeyemi drey...@gmail.com wrote:
  Hi,
 
  I will like to understand the dynamics behind retrieving form values when
  they are submitted.
 
  I have the following code:
 
  Dashboard.html
  ---
  body
 span wicket:id='mainNavigation'/
 
  form wicket:id=searchForm
  label for=searchSearch:/label
  input wicket:id=searchtextfield type=text / br /
  input type=submit/
 /form
 
 /body
 
  Dashboard.java
  --
  public class Dashboard extends BasePage {
 
 private static final Logger log =
  Logger.getLogger(Dashboard.class.getName());
 
 public Dashboard() {
 super ();
 final SearchForm searchForm = new SearchForm(searchForm);
 add(searchForm);
 }
 
 public Dashboard(IModel model) {
 super(model);
 }
 
 private class SearchForm extends Form implements
  IFormSubmittingComponent {
 TextField searchtextfield;
 Search search = new Search();
 
 public SearchForm(String id) {
 super(id);
 add(searchtextfield = new TextField(searchtextfield, new
  Model() {

 replace with: new PropertyModel(search, text)

 
 @Override
 public Serializable getObject() {
 return search.getText();
 }
 
 @Override
 public void setObject(Serializable object) {
 search.setText((String) object);
 }
 }));
 }
 
 @Override
 public boolean getDefaultFormProcessing() {
 return false;
 }

 remove this method

 
 @Override
 public Form? getForm() {
 return this;
 }
 
 @Override
 public String getInputName() {
 return searchtextfield;
 }
 
 @Override
 public void onSubmit() {
 
 //String value = (String)searchtextfield.getModelObject();
 String value = searchtextfield.getValue();
 log.log(Level.INFO, Search value is: , value);
 
 // Using SolrJ to make the request to Solr
 String url = http://localhost:8983/solr/;;
 try {
 // Connect to Solr Server
 SolrServer server = new CommonsHttpSolrServer(url);
 //server.query(null, METHOD.GET);
 SolrQuery solrQuery = new SolrQuery(value);
 QueryResponse response = server.query(solrQuery);
 
 // log the response
 log.log(Level.INFO, Search result is: ,
  response.toString());
 System.out.println(System print. Response is:  +
  response.toString());
 } catch (MalformedURLException ex) {
 
   Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
 } catch(SolrServerException sse) {
 
 }
 }
 }
 
  }
 
  Search.java
  -
  public class Search {

 make this Serializable

 
 private String text;
 
 public Search() {
 }
 
 public Search(String text) {
 this.text = text;
 }
 
 public String getText() {
 return text;
 }
 
 public void setText(String text) {
 this.text = text;
 }
  }
 
  I really don't know what I'm doing wrong as I can't seem to be able to
  retrieve the value entered into the textfield.
 
  Any help will be much appreciated.
 
  Thanks
 


Thanks Martin. Worked as expected.

It looks like I will have lots of experimenting to do to understand wicket
model/components architecture. I've read lot's of docs, books and
references and I'm still surprised that I'm missing basic understanding of
how things work.

-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde