《wicket学习六》&CallbackURLExample

24 篇文章 1 订阅

今天我们来学习一下,关于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

然后是下面的函数

然后回到下面的函数

进入到如的函数

最后页面做了相应的更改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值