public
Last active

wicket: submitting a form

  • Download Gist
HomePage.html
HTML
1 2 3 4 5 6 7 8 9 10 11
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<head>
<title>Wicket Quickstart Archetype Homepage</title>
</head>
<body>
<form wicket:id="searchform">
<input type="text" wicket:id="textField" name="query"/>
<input type="submit" value="Search"/>
</form>
</body>
</html>
Homepage.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package com.mycompany;
 
import java.io.Serializable;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.WebRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
* Homepage
*/
public class HomePage extends WebPage {
 
private static final long serialVersionUID = 1L;
private Logger logger = LoggerFactory.getLogger(getClass());
 
public HomePage(final PageParameters parameters) {
super(parameters);
String q = parameters.getString("q");
final Model txtModel = new Model(q) {
 
@Override
public Serializable getObject() {
if (super.getObject() == null)
return "";
else
return super.getObject();
}
};
TextField text = new TextField("textField", txtModel);
Form form = new Form("searchform") {
 
@Override
protected void onSubmit() {
logger.info(getMethod() + ": onSubmit textField object:" + txtModel.getObject());
PageParameters pp = new PageParameters();
pp.add("q", txtModel.getObject().toString());
setResponsePage(HomePage.class, pp);
}
};
add(form);
form.add(text);
logger.info(getMethod() + ": HomePage ctor params: " + parameters);
}
 
public String getMethod() {
return ((WebRequest) getRequest()).getHttpServletRequest().getMethod();
}
}
WicketApplication.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package com.mycompany;
 
import org.apache.wicket.protocol.http.WebApplication;
 
/**
* Application object for your web application. If you want to run this application without deploying, run the Start class.
*
* @see com.mycompany.Start#main(String[])
*/
public class WicketApplication extends WebApplication {
 
/**
* Constructor
*/
public WicketApplication() {
}
 
/**
* @see org.apache.wicket.Application#getHomePage()
*/
public Class<HomePage> getHomePage() {
return HomePage.class;
}
 
@Override
protected void init() {
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.