2011-02-23 72 views
2

我在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+"')");    
}                                      
}); 

創造了一個新的AjaxLink並將其添加到我的.html文件內

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

的網址只是一個例子,但T1是動態的,我從我的.java文件中得到。我希望「TEXT TO REPLACE」等於T1字符串,但我不知道該怎麼做。我試圖創建一個標籤,並添加它像

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

但這給出了一個錯誤。

有什麼建議嗎?

感謝

+0

@ user561793,所以你得到它的工作?一定要接受最有用的答案(綠色標記),或者如果有些事情不清楚,請留言! – Jonik 2011-02-24 17:11:55

回答

1

Label方向是正確的,但你必須確保你在Java代碼中添加標籤爲好,它應該是你的Ajax鏈接的子組件。

(在旁註:你可能要考慮使用<wicket:container>,而不是<span>在這種情況下,它並沒有多大關係,但也有情況下,額外的<span>標籤會使你的HTML無效。)

+0

工作。謝謝。我沒有將該標籤聲明爲AjaxLink的子組件,但是一旦我修復它可以正常工作。 – BourneAgain 2011-02-24 20:25:36

0

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)); 
+0

爲什麼要點擊時更新鏈接組件? – biziclop 2011-02-23 20:38:13

+0

嗯,[AjaxRequestTarget]中沒有'appendComponent'方法(http://wicket.apache.org/apidocs/1.4/org/apache/wicket/ajax/AjaxRequestTarget.html) – Jonik 2011-02-23 20:48:05

+0

@Jonik addComponent(),很抱歉,匆忙 – 2011-02-23 20:56:58