我試圖將Google-Plus嵌入到我的GWT應用程序中。我希望它被嵌入到HorizontalPanel中。我讀過+1button developers google。我沒有在stackoverflow中找到任何關於這個特定問題的文章。我的問題可能是我不明白如何將js包含到GUI組件中。我很感激如何將Google+代碼添加到Panel中的示例。在GWT中嵌入google-plus
2
A
回答
5
這裏是如何做到這一點:
文檔:
<!-- Place this tag in your head or just before your close body tag -->
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<!-- Place this tag where you want the +1 button to render -->
<g:plusone></g:plusone>
在GWT
:
private void drawPlusOne() {
String s = "<g:plusone href=\"http://urltoplusone.com\"></g:plusone>";
HTML h = new HTML(s);
somePanel.add(h);
// You can insert a script tag this way or via your .gwt.xml
Document doc = Document.get();
ScriptElement script = doc.createScriptElement();
script.setSrc("https://apis.google.com/js/plusone.js");
script.setType("text/javascript");
script.setLang("javascript");
doc.getBody().appendChild(script);
}
0
我個人從未在GWT中嵌入+1按鈕,但鏈接的文章看起來很自我解釋。
部分中「一個簡單的按鈕」,表示實施GOOGLEPLUS整合的最簡單的方法就是補充一點:
<script src="https://apis.google.com/js/plusone.js" />
<g:plusone></g:plusone>
首先,<script>
標籤應包含在你的.gwt.xml
文件。
然後,我實現了<g:plusone></g:plusone>
這樣的:
public class GPlusOne extends SimplePanel {
public GPlusOne() {
super((Element)Document.get().createElement("g:plusone").cast());
}
}
(請注意,此代碼是未經測試,但它是基於一個簡單的概念,一個SimplePanel
可以擴展到編譯任何HTML元素)
然後,無論您希望按鈕顯示哪個位置,都可以使用新的GPlusOne
元素。
+0
我會試試這個並回復你。我不喜歡把東西放在我的.gwt.xml中,因爲我喜歡保持它乾淨。但如果沒有其他辦法,我會這樣做,並進一步研究把js放在別的地方。非常感謝。 – Martin
+0
據我所知,外部JS應該放在.gwt.xml中。或者,您可以將'
這工作,非常感謝你。我沒有在生產模式下嘗試它,但我認爲它會起作用。如果不是,這將是一個不同的問題。 – Martin