java 捕获多个复选框选择 JSP 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2426380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:05:27  来源:igfitidea点击:

Capture multiple check box selection JSP Parameters

javaarraysjsp

提问by Leslie

I found this postthat shows how to pass multiple check box selections to another JSP page, but it's not working for me. When I try to get the selected values I get:

我发现这篇文章展示了如何将多个复选框选择传递给另一个 JSP 页面,但它对我不起作用。当我尝试获取选定的值时,我得到:

checked boxes: [Ljava.lang.String;@3f3fbd

复选框:[Ljava.lang.String;@3f3fbd

Here are my two pages (be gentle, this is my first attempt at JSP!)

这是我的两页(温柔点,这是我第一次尝试 JSP!)

createSHAREfile.jsp basically runs a query to find all the terms that have not been processed and show each term with a check box next to it:

createSHAREfile.jsp 基本上运行一个查询来查找所有尚未处理的术语,并在每个术语旁边显示一个复选框:

  <title>Create SHARE Files</title>
</head>
<body>
  <jsp:include page="../menu/header.jsp" flush="false" />
  <form name='SelectSHARETerms' method='post' action="SHAREProcessing.jsp">
    <fieldset><legend>Select Terms to Process for SHARE</legend>
    <table align='left'>
    <% String termDetail = "", currDate = "";
       currentDateTime datetime = new currentDateTime();
       datetime.setCurrDate();
       currDate = datetime.getCurrDate();
       java.sql.Date todayDate = java.sql.Date.valueOf(currDate);
       Terms terms = new Terms();
       ArrayList<Terms.termsTable> termsObjList = new ArrayList<Terms.termsTable>();
       terms.setTermsSql("Select * from Terms where TermDate <= '" + currDate + "' AND VoucherProcessDate Is Null");
       boolean indicator = terms.setListOfTerms();
       if (indicator == true) {
         int size = terms.getListSize();
         termsObjList = terms.getTermsList();
         for (int i=0; i<size; ++i) {
           Terms.termsTable eachTerm = (Terms.termsTable)termsObjList.get(i);
           java.sql.Date termDate = eachTerm.TermDate;
     %>         
      <tr><td><input type=checkbox name=SelectedTermDate id='SelectedTermDate<%=i%>' value="<%=i%>"><%=termDate %></td></tr>
    <%
         }
       }
     %>
      <tr><td align='center'><input type='submit' value='Submit'></input></td></tr>
    </table>
    </fieldset>
    </form>
  </body>
</html>

When the submit button is pressed I call SHAREProcessing.jsp. Right now all i'm trying to do on this page is show which termdates the user has selected so I can use them as parameters to a Java Class that will create the files for the selected terms:

当提交按钮被按下时,我调用 SHAREProcessing.jsp。现在,我在此页面上要做的就是显示用户选择了哪些学期日期,以便我可以将它们用作 Java 类的参数,该 Java 类将为所选术语创建文件:

  <title>SHARE Processing</title>
</head>
<body>
<jsp:include page="../menu/header.jsp" flush="false" />
<table width='50%' align='center' border='1'>
 <% String[] SelectedValues = request.getParameterValues("SelectedTermDate");
    System.out.println("checked boxes: " + SelectedValues);
  %>
</body>
</html>

Here's where I'm trying to use the code shown in the other post but it's not working :(

这是我尝试使用另一篇文章中显示的代码的地方,但它不起作用:(

Thanks for any help! Leslie

谢谢你的帮助!莱斯利

回答by Pointy

You're trying to print the whole string array with System.out.println, and so you get that. It's probably working fine.

您正在尝试使用 System.out.println 打印整个字符串数组,因此您明白了。它可能工作正常。

Try this:

试试这个:

System.out.println("checked boxes:");
for (int i = 0; i < SelectedValues.length; ++i)
  System.out.println("  " + SelectedValues[i]);

Also, I beg you: in your spare time, find out about a modern web framework (there are zillions for Java) and strive to escape from the painful world of coding Java inside JSP files.

另外,我恳求您:在您的空闲时间,了解一个现代 Web 框架(Java 有无数个),并努力摆脱在 JSP 文件中编写 Java 的痛苦世界。

回答by BalusC

You're just facing the default value of Object#toString().

你只是面对的默认值Object#toString()

Either just loop over it and print each item, or use Arrays#toString(). Here's an SSCCE:

要么循环遍历它并打印每个项目,要么使用Arrays#toString(). 这是一个SSCCE

package com.stackoverflow.q2426380;

import java.util.Arrays;

public class Test {

    public static void main(String... args) {
        String[] array = {"foo", "bar" , "waa"};
        System.out.println(array); // [Ljava.lang.String;@addbf1

        String arrayAsString = Arrays.toString(array);
        System.out.println(arrayAsString); // [foo, bar, waa]
    }

}

That said, this problem has actually nothing to do with JSP. It's just a view technology. The problem is rather in the basic Java code --which you wrote at the wrong place, in a JSP file instead of a Java class. I strongly agree with the comments that writing raw Java code in JSP files is a bad practice. Start learning Servlets.

也就是说,这个问题实际上与 JSP 无关。这只是一种视图技术。问题在于基本的 Java 代码——您写错了地方,在 JSP 文件而不是 Java 类中。我非常同意在 JSP 文件中编写原始 Java 代码是一种不好的做法的评论开始学习 Servlet