今天我们来学习一下,关于wicket中回调一些知识。最后做成的页面如下图所示。
我们来看一下对应上面的homepage.java代码。
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
public HomePage() {
List colors = Arrays.asList(new String[] { "Red", "Blue", "Green", "Yellow"});
IModel radioModel = Model.of();
IModel selectModel = Model.of();
add(new RadioChoice("choices", radioModel, colors)
.add(new OnChangeSingleChoiceBehavior()));
add(new DropDownChoice("drpDwnChoices", selectModel, colors)
.add(new OnChangeSingleChoiceBehavior()));
add(new Label("radioModel", radioModel));
add(new Label("selectModel", selectModel));
}
}
以及html页面
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Radio choices. Model value:
<span wicket:id="radioModel"></span>
<br />
<div wicket:id="choices"></div>
<br />
Select choices. Model value:
<span wicket:id="selectModel"></span>
<br />
<select wicket:id="drpDwnChoices"></select>
</body>
</html>
在看一下OnChangeSingleChoiceBehavior.java代码
public class OnChangeSingleChoiceBehavior extends Behavior implements IRequestListener {
private AbstractSingleSelectChoice boundComponent;
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
Application app = Application.get();
ResourceReference jqReference = app.getJavaScriptLibrarySettings().getJQueryReference();
response.render(JavaScriptHeaderItem.forReference(jqReference));
}
@Override
public void bind(Component hostComponent) {
super.bind(hostComponent);
Args.notNull(hostComponent, "hostComponent");
if (boundComponent != null){
throw new IllegalStateException("this kind of handler cannot be attached to " +
"multiple components; it is already attached to component " + boundComponent +
", but component " + hostComponent + " wants to be attached too");
}
if(!(hostComponent instanceof AbstractSingleSelectChoice)){
throw new IllegalStateException("This behavior must be attached to AbstractSingleSelectChoice"
+ " component, but component " + hostComponent + " is not one of those");
}
boundComponent = (AbstractSingleSelectChoice) hostComponent;
}
@Override
public void onComponentTag(Component component, ComponentTag tag) {
super.onComponentTag(component, tag);
CharSequence callBackURL = getCallbackUrl();
String separatorChar = callBackURL.toString().indexOf('?') > -1 ? "&" : "?";
String finalScript = "var isSelect = $(this).is('select');\n" +
"var component;\n" +
"if(isSelect)\n" +
" component = $(this);\n" +
"else \n" +
" component = $(this).find('input:radio:checked');\n" +
"window.location.href='" + callBackURL + separatorChar + "choiceId=' + " +
"component.val()";
tag.put("onchange", finalScript);
}
public CharSequence getCallbackUrl(){
if (boundComponent == null){
throw new IllegalArgumentException(
"Behavior must be bound to a component to create the URL");
}
return boundComponent.urlForListener(this, new PageParameters());
}
protected Object convertChoiceIdToChoice(String id){
final List choices = boundComponent.getChoices();
final IChoiceRenderer renderer = boundComponent.getChoiceRenderer();
for (int index = 0; index < choices.size(); index++){
final Object choice = choices.get(index);
if (renderer.getIdValue(choice, index).equals(id)){
return choice;
}
}
return null;
}
public void onRequest() {
Request request = RequestCycle.get().getRequest();
IRequestParameters requestParameters = request.getRequestParameters();
StringValue choiceId = requestParameters.getParameterValue("choiceId");
boundComponent.setDefaultModelObject(convertChoiceIdToChoice(choiceId.toString()));
}
protected Component getComponent() {
return boundComponent;
}
}
对应的webapplication
public class WicketApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return HomePage.class;
}
}
我们来点击一个radiobutton都经历哪些过程。
首先进入到Request
然后是下面的函数
然后回到下面的函数
进入到如的函数
最后页面做了相应的更改