Q1. Tell Me The Example Of Username Validation In Apache-wicket?
import org.apache.wicket.validation.CompoundValidator;
import org.apache.wicket.validation.validator.PatternValidator;
import org.apache.wicket.validation.validator.StringValidator;
public class UsernameValidator extends CompoundValidator
private static final long serialVersionUID = 1L;
public UsernameValidator() {
add(StringValidator.lengthBetween(6, 15));
add(new PatternValidator(“[a-z0-9_-]+”));
}
}
Q2. How To Create Radio Button In Apache-wicket?
//choices in radio button
private static final List
RadioChoice
Q3. How To Create Dropdown Choice In Apache-wicket?
//Java
import org.apache.wicket.markup.html.form.DropDownChoice;
//choices in dropdown box
private static final List
“Google”, “Bing”, “Baidu” });
//variable to hold the selected value from dropdown box,
//and also make “Google” is selected by default
private String selected = “Google”;
DropDownChoice
“sites”, new PropertyModel
//HTML for dropdown box
Q4. How To Keep File Validation In Apache-wicket If No File Has Been Selected?
To fix it, just override the validateOnNullValue() method like this :
FileUploadField fileUpload = new FileUploadField(“fileupload”,new Model
fileUpload .add(new AbstractValidator() {
public boolean validateOnNullValue(){
return true;
}
protected void onValidate(IValidatable validatable) {
FileUpload fileUpload = (FileUpload) validatable.getValue();
}
protected String resourceKey() {
return “yourErrorKey”;
}
});
Now, when no file is selected, and submit button is clicked, validation will be performed.
Q5. How To Get Servletcontext In Apache-wicket Application?
Yes, you can get the ServletContext class via Wicket’s WebApplication class like this :
import javax.servlet.ServletContext;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import com.withoutbook.hello.Hello;
public class CustomApplication extends WebApplication {
@Override
public Class extends Page> getHomePage() {
ServletContext servletContext = WebApplication.get().getServletContext();
return Hello.class; //return default page
}
}
Q6. How To Create Checkbox In Apache-wicket?
final CheckBox chk0 = new CheckBox(“checkbox0”, Model.of(Boolean.TRUE));
final CheckBox chk1 = new CheckBox(“checkbox1”,
new PropertyModel
final CheckBox chk2 = new CheckBox(“checkbox2”,
new PropertyModel
Q7. How To Create 404 Error Page?
public class WicketApplication extends WebApplication {
@Override
protected void init() {
mount(new QueryStringUrlCodingStrategy(“error404”,ErrorPage404.class));
}
}
Q8. What Is Pallet Component In Apache-wicket?
Wicket extension comes with a special “Palette” component, which render two select boxes, and allow user to move items from one select box into another.
//Java
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
final Palette
new ListModel
new CollectionModel
renderer, 10, true);
//HTML
Q9. How To Create Custom Validator In Apache-wicket?
See summary steps to create a custom validator :
@Implements IValidator.
import org.apache.wicket.validation.IValidator;
public class Strong PasswordValidator implements IValidator
…
}
@Override validate(IValidatable validatable).
public class StrongPasswordValidator implements IValidator
…
@Override
public void validate(IValidatable
//get input from attached component
final String field = validatable.getValue();
}
}
@Attached custom validator to form component.
public class CustomValidatorPage extends WebPage {
public CustomValidatorPage(final PageParameters parameters) {
final PasswordTextField password = new PasswordTextField(“password”,Model.of(“”));
//attached custom validator to password field
password.add(new StrongPasswordValidator());
//…
}
}
Q10. How To Create Select Option As Menu Wise In Apache-wicket?
//Java
import org.apache.wicket.extensions.markup.html.form.select.Select;
import org.apache.wicket.extensions.markup.html.form.select.SelectOption;
//variable to hold the selected value from dropdown box,
//and also make “jQuery” selected by default
private String selected = “jQuery”;
Select languages = new Select(“languages”, new PropertyModel
form.add(languages);
languages.add(new SelectOption
languages.add(new SelectOption
languages.add(new SelectOption
languages.add(new SelectOption
languages.add(new SelectOption
//HTML for dropdown box
Q11. How To Create A Textarea In Apache-wicket?
//create a textarea field for address
final TextArea
address.setRequired(true);
Q12. What Is About Web Application In Wicket?
A web application is a subclass of Application which associates with an instance of WicketServlet to serve pages over the HTTP protocol. This class is intended to be subclassed by framework clients to define a web application.
Q13. How To Create Fileupload Field In Apache-wicket?
//Java
import org.apache.wicket.markup.html.form.upload.FileUploadField;
form.setMultiPart(true);
form.add(fileUpload = new FileUploadField(“fileUpload”));
//HTML
Q14. What Are Wicket Models?
A Model holds a value for a component to display and/or edit :
- Simple Models
- Dynamic Models
- Property Models
- Compound Property Models
- Wrapped Object Models
- Resource Models
- Detachable Models
- Chaining models
Q15. How To Create A Password Field In Apache-wicket?
create a password field
final PasswordTextField password = new PasswordTextField(“password”, Model.of(“”));
//for properties file
password.setLabel(Model.of(“Password”));
Q16. What Is Wicket Framework?
- Wicket is one of the most recent in a long line of Java web development frameworks.Wicket is a component-basedframework, which puts it in stark contrast to some of the earlier solutions to the sometimes monotonous task of web programming.Wicket builds on top of Sun’s servlet API. Wicket is mostly removed from the request/response nature that is inherent with the web and Servlets. Instead of building controllers that must service many users and threads simultaneously, taking in requests, returning responses, and never storing any state, the Wicket developer thinks in terms of stateful components. Instead of creating a controller or action class, he or she creates a page, places components on it, and defines how each component reacts to user input.
- It is a lightweight component-based web application framework for the Java programming.
Q17. What Are The Ways To Create A Page In Wicket?
There are 2 ways to Create New Wicket Page.
- create a Page Extending “WebPage” Class.
- create a Page Extending “BasePage” ( BasePage Should Extend “WebPage”).
- IF you are using first Way you should Create Whole page with thier Header,Footer and other parts
- and that HTML file’s content may be large (complicated).This is an Unreliable way to create Page. suppose you have to change some content in Header part then you have to edit all pages that having Header Content
- If you are using second way, first Create your BasePage then you can extend these page to other while creating new page. in that page you have to add only Body part (Content that you want to show on that Page) Using
Q18. What Is Base Class For Html Pages?
Base class for HTML pages: Webpage Class.
Q19. How To Integrate Apache-wicket With Spring?
Override Wicket application init() method with this “addComponentInstantiationListener(new SpringComponentInjector(this));“.
File : Wicket application class
package com.withoutbook;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.withoutbook.user.SimplePage;
public class WicketApplication extends WebApplication {
@Override
public Class
return SimplePage.class; // return default page
}
@Override
protected void init() {
super.init();
addComponentInstantiationListener(new SpringComponentInjector(this));
}
}
Now, you can inject Spring bean into Wicket component via @SpringBean.
Q20. How To Create A Textfield In Apache-wicket?
final TextField username = new TextField(“username”,Model.of(“”));
username.setRequired(true);
username.add(new Username Validator());
Q21. How To Submit A Form In Apache-wicket?
Form> form = new Form
@Override
protected void onSubmit() {
final String usernameValue = username.getModelObject();
PageParameters pageParameters = new PageParameters();
pageParameters.add(“username”, usernameValue);
setResponsePage(SuccessPage.class, pageParameters);
}
};
Q22. How To Create Single Selected Listbox?
// single list choice
private static final List
ListChoice
listFruits.setMaxRows(5);
Q23. How To Create Multiple Selected Listbox In Apache-wicket?
//choices in list box
private static final List
//variable to hold the selected multiple values from listbox,
//and make “Number 6” selected as default value
private ArrayList
Arrays.asList(new String[] { “Number 6” }));
ListMultipleChoice
“number”, new Model(selectedNumber), NUMBERS);
//HTML for multiple select listbox
Q24. How To Create Multiple Checkboxes In Apache-wicket?
private static final List
// hold the checkbox values
private ArrayList
final CheckBoxMultipleChoice
Q25. Dependency To Start Wicket?
Wicket need SLF4J !
You have to include the slf4j logging implementation, otherwise Wicket will be failed to start.
Wicket need resource filter
Remember to add the resource filter, Wicket puts all files in same package folder, if you didn’t define the resource filter to include everything “