使用Spark

时间:2016-02-06 18:19:38

标签: java freemarker spark-java

我有一个复选框的HTML列表,它是使用 Freemarker 动态创建的。 Html看起来像这样:

<div data-role="collapsible">
    <h4>Software</h4>
    <form action="/" method="POST">
        <ul data-role="listview" >
            <#list softwareoptions as software>
                <input type="checkbox" id="softwarefilter" name="softwarefilter" value="${software}" autocomplete="off" >${software}
            </#list>
        </ul>
    </form>
</div>

这是生成的列表:

<div data-role="collapsible">
<h4>Software</h4>
<form action="/" method="POST">
    <ul data-role="listview" >
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Adobe Creative Cloud" autocomplete="off" >Adobe Creative Cloud
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Adobe Web Premium" autocomplete="off" >Adobe Web Premium
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="MATLAB" autocomplete="off" >MATLAB
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Quicken" autocomplete="off" >Quicken
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Steam" autocomplete="off" >Steam
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="IBM SPSS Statistics" autocomplete="off" >IBM SPSS Statistics
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft SQL Server 2008" autocomplete="off" >Microsoft SQL Server 2008
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft SQL Server 2012" autocomplete="off" >Microsoft SQL Server 2012
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Office" autocomplete="off" >Microsoft Office
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Autodesk Infrastructure Design Suite" autocomplete="off" >Autodesk Infrastructure Design Suite
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Visual Studio 2010" autocomplete="off" >Microsoft Visual Studio 2010
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Visual Studio 2013" autocomplete="off" >Microsoft Visual Studio 2013
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Visual Studio 2015" autocomplete="off" >Microsoft Visual Studio 2015
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Visio" autocomplete="off" >Microsoft Visio
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Unity 3d" autocomplete="off" >Unity 3d
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="GIMP" autocomplete="off" >GIMP
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Maxon CINEMA 4D Broadcast" autocomplete="off" >Maxon CINEMA 4D Broadcast
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="CrazyBump" autocomplete="off" >CrazyBump
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="FRAPS" autocomplete="off" >FRAPS
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="InfraRecorder" autocomplete="off" >InfraRecorder
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Garageband" autocomplete="off" >Garageband
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Microsoft Project" autocomplete="off" >Microsoft Project
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Numbers" autocomplete="off" >Numbers
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Pages" autocomplete="off" >Pages
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Fontographer" autocomplete="off" >Fontographer
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Keynote" autocomplete="off" >Keynote
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Wacom Tablet" autocomplete="off" >Wacom Tablet
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Arduino" autocomplete="off" >Arduino
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="iBooks" autocomplete="off" >iBooks
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="iMovie" autocomplete="off" >iMovie
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="Logic Pro X" autocomplete="off" >Logic Pro X
            <input type="checkbox" id="softwarefilter" name="softwarefilter" value="iTunes" autocomplete="off" >iTunes
    </ul>
</form>
</div>

要请求复选框的值,我使用Java代码:

Object softstring = request.queryParams("softwarefilter");

但是,这仅返回选中的第一个复选框名称的字符串值。例如,如果选中Adobe Creative Suite和MATLAB,则为softstring =&#34; Adob​​e Creative Suite&#34;。

当我使用时:

 Object softstring = request.queryParamsValues("softwarefilter");

QueryParamsMap softmap = request.queryMap("softwarefilter");

我分别获得一个空数组和一个空映射。关于我可能遗失的任何提示?

1 个答案:

答案 0 :(得分:2)

我使用了与问题相同的HTML代码,只是我在表单中添加了一个提交标记。

根据检查,所选的所有选项都在表单数据部分中。

enter image description here

因此,可以从req.body();检索选项。例如,如果我选择“Adobe Creative Cloud”和“Adobe Web Premium”,则req.body();的输出为

softwarefilter=Adobe+Creative+Cloud&softwarefilter=Adobe+Web+Premium

现在解决这个原始字符串以获取所有选项。希望这可以帮助。