[英]How do I construct an Html object from a String variable in Java controller code within the play (2.3) framework
[英]How to construct an object representing HTML page in java from the existing string data?
我想創建一個代表html文檔的對象,該對象假設是通過編程提交給Web服務器的。 我還必須在該文檔中設置各種參數。
場景:我已閱讀一個包含用戶名和密碼字段的網頁。 現在,我想通過桌面應用程序將用戶名和密碼設置為該html頁面。 之后,我想將該頁面提交到Web服務器。
現在,我在這里面臨的困惑是如何通過我從網站獲得的響應構造一個表示HTML文檔的對象。
您應該能夠使用名為“ Velocity ”的工具來執行此操作。 如果您向下滾動到http://velocity.apache.org/engine/devel/webapps.html的“教程”部分,則可以獲取有關其工作方式的示例。
您應該稍加閱讀,因為我已經有一段時間沒有使用它了。
首先,您應該使用示例中顯示的格式創建HTML模板。
然后,以下是要完成的要點(創建模板后):
VelocityEngine ve = new VelocityEngine();
Template template = ve.getTemplate("name_of_template");
VelocityContext context = new VelocityContext();
StringWriter writer = new StringWriter();
context.put("username", yourUsernameString);
context.put("password", yourPasswordString);
template.merge(context, writer);
// 'writer' now holds the output!
// try using 'writer.toString()' to get a string version.
您應該留給'writer'對象,其中包含您的HTML,其中現在包含變量!
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.