根据wicket框架中的条件启用或禁用链接

时间:2015-07-30 15:03:35

标签: java wicket

我在WICKET框架中有一个要求。

我想基于if else条件启用和禁用链接。有人可以建议如何存档吗?

以下是示例代码:

Link<OrderAssetAncillaryListEntry> getDesc = new Link<OrderAssetAncillaryListEntry>(
            "descLink", new Model(oale)) {
        @Override
        public void onClick() {
            final OrderAssetAncillaryListEntry oale = this.getModelObject();
            String[] scrids = {oale.getScrid()};

            try {

                byte[] content = getReportBytes(scrids);
                IResourceStream resourceStream = new ByteArrayResourceStream(
                        content, "application/vnd.ms-excel");

                getRequestCycle().setRequestTarget(
                        new ResourceStreamRequestTarget(resourceStream) {

                            @Override
                            public String getFileName() {
                                return oale.getShowCode() + "_desc.xls";
                            }
                        });


            } catch (Exception e) {
                LOGGER.error("Unable to fetch Description Report file", e);
            } 
        }
    };
    // add label
    getDesc.add(new Label("descLinkLabel", "Description"));
    return getDesc;

3 个答案:

答案 0 :(得分:2)

每个请求多次调用

#isEnabled ()。最好覆盖#onConfigure ()并在其中使用setEnabled ()

答案 1 :(得分:0)

我建议您覆盖Link#isEnabled()并评估您的状况。

答案 2 :(得分:0)

您有不同的选择:

    //Business logic turning someCondition true or false
   final boolean someCondition = verifyBusiness();

    Link<String> testLink = new Link<String>("test", Model.of("someProperty")){


        @Override
        public boolean isEnabled() {//Option 1
            if (someCondition){
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void onClick() {
            //business logic
        }
    };

    //OR use something as below

    testLink.setEnabled(someCondition ? true : false);//Option 2
    private boolean verifyBusiness(){

        return true; //Whatever you want to return based upon your logic
    }