1. 首页 >后端开发 >Java >

java生成html自动表单提交

public static String CreateAutoFormHtml(String reqUrl, Map<String, String> reqData) {

    NStringBuilder html = new NStringBuilder();
    html.appendLine("<html>");
    html.appendLine("<head>");
    html.appendLine("<meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\" name=\"viewport\" />");
    html.appendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
    html.appendLine("</head>");
    html.appendLine("<body onload=\"OnLoadSubmit();\">");
    html.appendLine("<form id=\"go_form\" action=\"{0}\" method=\"post\">", reqUrl);
    for (Map.Entry<String, String> kvp : reqData.entrySet()) {
        html.appendLine("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", kvp.getKey(), kvp.getValue());
    }
    html.appendLine("");
    html.appendLine("<script type=\"text/javascript\">");
    html.appendLine("<!--");
    html.appendLine("function OnLoadSubmit()");
    html.appendLine("{");
    html.appendLine("document.getElementById(\"go_form\").submit();");
    html.appendLine("}");
    html.appendLine("//-->");
    html.appendLine("</script>");
    html.appendLine("</body>");
    html.appendLine("</html>");

    return html.toString();
}