【问题标题】:Accessing Nested POST Parameters in Java在 Java 中访问嵌套的 POST 参数
【发布时间】:2018-08-25 13:24:06
【问题描述】:

如何访问 Java Servlet API 中的嵌套 POST 数据?在 PHP(我不能用于这个项目)中,我可以使用 $ds = $_POST['details']['double_sided']; 访问字段

我的表单中有以下输入:

    <div class="row">
        <div class="col-sm-6">
            <label for="details_double_sided" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[double_sided]">
                <input type="checkbox" id="details_double_sided" value="1" name="<portlet:namespace />details[double_sided]"> Double Sided
            </label>

            <label for="details_stapled" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[stapled]">
                <input type="checkbox" id="details_stapled" value="1" name="<portlet:namespace />details[stapled]"> Stapled
            </label>

            <label for="details_three_hole_punched" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[three_hole_punched]">
                <input type="checkbox" id="details_three_hole_punched" value="1" name="<portlet:namespace />details[three_hole_punched]"> Three Hole Punched
            </label>
        </div>

        <div class="col-sm-6">
            <label for="details_copied_in_color" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[copied_in_color]">
                <input type="checkbox" id="details_copied_in_color" value="1" name="<portlet:namespace />details[copied_in_color]"> Copied in Color
            </label>

            <label for="details_copied_on_color_page" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[copied_on_color_page]">
                <input type="checkbox" id="details_copied_on_color_page" value="1" name="<portlet:namespace />details[copied_on_color_page]"> Copied on Color Paper (Please indicate paper color in special instructions)
            </label>
        </div>
    </div>

我试过多种方法,比如

String[] details = ParamUtil.get(request, "details[double_sided]", "0")

Object details = request.getAttribute("details");

String[] details = ParamUtil.getParameterValues(request, "details[]");

但它们似乎都返回 null / 空值。

【问题讨论】:

  • 你使用什么网络框架?无论你使用什么,我都建议使用它提供的任何序列化机制。
  • 你试过 request.getParameter("details[double_lateral]"); (假设它是通过名称而不是 id 引用的)

标签: java servlets post url-parameters


【解决方案1】:

函数getParameter() 需要您输入的字段的名称,您的代码是&lt;portlet:namespace /&gt;details[double_sided]

如果这不起作用,您可以使用 getParameterNames() 获取已传递给您的 servlet 的所有参数名称,您可以对其进行迭代以检查您的输入,或者 getParameterMap 这是包含所有键和已发布到您的 servlet 的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多