java 如何更改 Wicket 中的链接文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3244216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:57:47  来源:igfitidea点击:

How do I change link-text in Wicket?

javaevent-handlinghyperlinkwicket

提问by Sylvus

I created a link with static text. That works fine but now I also want to change the text when you click the link.

我创建了一个带有静态文本的链接。这工作正常,但现在我还想在您单击链接时更改文本。

I got as far as this:

我得到了这个:

add(new Link("doAnything") {
    @Override
    public void onClick() {
            // change the text!
            // this.modelChanging();
            // this.detach();
    }
});

I hope you can give me some easy advice - I'm really new to this :)

我希望你能给我一些简单的建议 - 我真的很陌生:)

Best regards Elias

最好的问候埃利亚斯

回答by Sean Patrick Floyd

The HTML:

HTML:

<html><head></head><body>
<wicket:panel>

    <a href="" wicket:id="link">
        <wicket:container wicket:id="label" />
    </a>

</wicket:panel>
</body></html>

The java:

爪哇:

public class MyPanel extends Panel{

    private static class Mybean{

        String labelText = "click me";

        public String getLabelText(){
            return this.labelText;
        }

        public void setLabelText(final String labelText){
            this.labelText = labelText;
        }

    }

    public MyPanel(final String id){
        super(id);
        final Mybean bean = new Mybean();
        this.add(new Link<Void>("link"){

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(){
                bean.setLabelText("Thanks for clicking");
            }
        }.add(new Label("label", new PropertyModel<String>(bean, "labelText")))

        );

    }

}

I tend to use wicket:container in order to not pollute the HTML with superfluous elements (the wicket:container won't be rendered in production)

我倾向于使用 wicket:container 以免用多余的元素污染 HTML(wicket:container 不会在生产中呈现)

回答by bert

you need to back the text in the link with its own model:

您需要使用自己的模型支持链接中的文本:

<a wicket:id="doAnything"> <span wicket:id="linktext"/> </a>

<a wicket:id="doAnything"> <span wicket:id="linktext"/> </a>

and in java:

在 Java 中:

add(new Link("doAnything").add(new Label("linktext", Model.of("i 'm the text"));

better yet if you use a (Compound)PropertyModel and have a getLinktext() function the returns the text, depending on the state.

更好的是,如果您使用 (Compound)PropertyModel 并具有 getLinktext() 函数,则会根据状态返回文本。

回答by user2081279

No change to the html necessary:

无需更改 html:

Link link = new Link("doAnything") {
    @Override
    public void onClick() {
            // change the text!
            // this.modelChanging();
            // this.detach();
    }
});
link.setBody(Model.of("visible link name"));

add(link);

回答by sanluck

In our Wicket 6 project we use something like that:

在我们的 Wicket 6 项目中,我们使用类似的东西:

public abstract class AjaxLabeledLink extends AjaxLink {

private IModel<String> text = new Model<>();

public AjaxLabeledLink(String string) {
    super(string);
    this.text = new Model<>("");
}

public AjaxLabeledLink(String string, String linkText) {
    super(string);
    this.text = new Model<>(linkText);
}

public AjaxLabeledLink(String string, IModel<String> linkText) {
    super(string);
    this.text = linkText;
}

@Override
public IModel<?> getBody() {
    return text;
}

public IModel<String> getText() {
    return text;
}

public void setText(IModel<String> text) {
    this.text = text;
}
}

This component satisfy our needs.

这个组件满足了我们的需求。

回答by Ehsan Amiryousefi

use the HTML tag as follow for your link and set the text in the property file for key in order to use it.

将 HTML 标记如下用于您的链接,并在属性文件中为 key 设置文本以使用它。

HTML:

HTML:

<a wicket:id="doAnything"><wicket:message key="label.LABLENAME" /></a>

Property:

财产:

label.LABLENAME        =YOUR OWN TEXT FOR LABLE!

Java:

爪哇:

add(new BookmarkablePageLink<YOURCLASS>(
            "doAnything", YOURCLASS.class)));

Output would be a link label with the text specified in the property file. Simply change the corresponding text in properties to whatever text you want.

输出将是带有属性文件中指定文本的链接标签。只需将属性中的相应文本更改为您想要的任何文本。

回答by Sebas

Wicket can't change the text attribute of your link, so, add a new html component to do the hack.

Wicket 无法更改链接的文本属性,因此,添加一个新的 html 组件来进行破解。

I don't agree with this philosophy, and prefer to leave it to the man of the situation: javascript.

我不同意这种哲学,更愿意把它留给这种情况的人:javascript。

add(new AjaxLink("doAnything") {
    @Override
    public void onClick(final AjaxTarget t) {
            // change the text!
            t.appendJavaScript( "document.getElementById('idofyourlink').text = 'newtext';" );
            // this.modelChanging();
            // this.detach();
    }
});

This way you don't need the additional label/span inside your <a>element.

这样你就不需要<a>元素内的额外标签/跨度。

You can modify your text from java if you need to. If you don't, just do it from javascript then.

如果需要,您可以从 Java 修改文本。如果你不这样做,那就从 javascript 做吧。