简体   繁体   English

替换Wicket AjaxLink中的文本

[英]Replace text inside Wicket AjaxLink

I have created a new AjaxLink in my .java file 我在.java文件中创建了一个新的AjaxLink

add(new AjaxLink("link"){                                                                                                                                                                                                                                                                                                  
private static final long serialVersionUID = 1L;                                                                                                       

@Override                                                                                                                                              
public void onClick(AjaxRequestTarget target) {                                                                                                        
target.appendJavascript("window.open('http://www.cnn.com/2011/WORLD/africa/02/23/libya.protests/index.html?hpt="+T1+"')");              
}                                                                                                                                                      
});

And added it to my .html file 并将其添加到我的.html文件中

<a href="#" wicket:id="link">TEXT TO REPLACE</a> 

The url is just an example but T1 is dynamic and I get that from my .java file. url只是一个例子,但是T1是动态的,我可以从我的.java文件中获取。 I would like the "TEXT TO REPLACE" to equal the T1 string but I don't know how to do this. 我希望“ TEXT TO REPLACE”等于T1字符串,但我不知道该怎么做。 I have tried creating a Label and adding it like 我尝试创建一个标签并添加它像

<a href="#" wicket:id="link"><span wicket:id="linkLbl"></span></a> 

but that gives an error. 但这会导致错误。

Any suggestions? 有什么建议么?

Thanks 谢谢

The Label is the right direction, but you have to make sure that you add the label in the java code as well, it should be a child component of your ajax link. Label是正确的方向,但是您必须确保也在Java代码中添加标签,它应该是ajax链接的子组件。

(On a sidenote: you might want to consider using <wicket:container> instead of <span> . In this case it doesn't matter much, but there are cases where an extra <span> tag would make your HTML invalid.) (在旁注中:您可能要考虑使用<wicket:container>而不是<span> 。在这种情况下,这无关紧要,但是在某些情况下,额外的<span>标记会使您的HTML无效。)

a) Use a common Model for both Link and Label, b) don't forget to update the link a)对链接和标签使用通用模型,b)不要忘记更新链接

IModel<String> model = // get model data from somewhere

add(new AjaxLink("link"){                                                                                                                                                                                                                                                                                                  
private static final long serialVersionUID = 1L;                                                                                                       

@Override                                                                                                                                              
public void onClick(AjaxRequestTarget target) {
target.addComponent(this); // update the link component
target.appendJavascript("window.open('http://www.cnn.com/2011/WORLD/africa/02/23"
  + "/libya.protests/index.html?hpt="+model.getObject()
     /* you probably want to encode this first */+"')");              
}                                                                                                                                                      
}).setOutputMarkupId().add(new Label("label",model));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM