如何将在javascript中创建的数组传递给另一个jsp,然后在该jsp的java函数中使用该数组?

时间:2015-08-31 04:00:08

标签: javascript java html jsp

这是我的脚本,我希望能够将数组“playernames”传递到另一个.jsp上的java函数中。我想知道如何将该数组传递给另一个页面,然后为我的java函数检索它。

<script>
function getPlayerNames() {
      var selected = document.querySelectorAll("#selected-players > tr > td");
      var playernames = [];
      for(var i=0; i<selected.length; ++i){
         var id = selected[i].getAttribute('id');
         if (id.indexOf('Player')>-1) {
             playernames.push(selected[i].textContent);
         }

      }  
}
</script>

编辑:

<td style="vertical-align: top;"><button   onclick="getPlayerNames()"id="generate">Generate</button><br></td>

<input type="hidden" id="players" />

<script>
function getPlayerNames(){
      var selected = document.querySelectorAll("#selected-players > tr >    td");
      var playernames = [];
      for(var i=0; i<selected.length; ++i){
         var id = selected[i].getAttribute('id');
         if (id.indexOf('Player')>-1) {
             playernames.push(selected[i].textContent);
         }

      }
      document.getElementById("players").values=playernames;
      document.getElementById("players").submit();
      window.location.replace("lineups.jsp");   
}</script>

其他jsp

<%String[] players = request.getParameterValues("players");%>

2 个答案:

答案 0 :(得分:1)

您需要在表单标记中包含隐藏字段,其id和action属性设置如下。

<xsl:template name="itemSummaryView-DIM-other-language">
    <xsl:if test="dim:field[@element='relation' and @qualifier='languageVersion' and descendant::text()]">
        <div class="col-sm-6 col-print-4 item-page-field-wrapper">
            <h5><i18n:text>xmlui.dri2xhtml.METS-1.0.item-languageVersion</i18n:text></h5>
            <span>
                <xsl:for-each select="dim:field[@element='relation' and @qualifier='languageVersion']">
                    <xsl:apply-templates  select="./node()" mode="showRelatedLang"/>
                    <xsl:if test="count(following-sibling::dim:field[@element='relation' and @qualifier='languageVersion']) != 0">
                        <xsl:text>; </xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </span>
        </div>
    </xsl:if>
</xsl:template>

<xsl:template match="dim:field[@element='relation' and @qualifier='languageVersion' and descendant::text()]" mode="showRelatedLang">
    <xsl:variable name="otherItemMetadataURL">
        <xsl:text>cocoon:/metadata/handle/</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>/mets.xml</xsl:text>
    </xsl:variable>
    <xsl:apply-templates select="document($otherItemMetadataURL)" mode="showLang"/>
</xsl:template>

<xsl:template match="dim:field[@element='language' and @qualifier='iso' and descendant::text()]" mode="showLang">
    <xsl:value-of select="util:isoLanguageToDisplay(node())"/>
</xsl:template>

答案 1 :(得分:0)

1)对数组进行字符串化,然后分配给隐藏字段 参考:Javascript Hidden Input Array
2)将表单中的隐藏字段提交给服务器。

header.Typeflag = '0'

3)在服务器上,您可以将此作为请求的一部分,即在下一个jsp上,您可以将此值作为请求参数检索。

package main

 import (
         "archive/tar"
         "compress/gzip"
         "flag"
         "fmt"
         "io"
         "os"
         "strings"
         "path/filepath"
 )

 func checkerror(err error) {

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }
 }

 func main() {

         flag.Parse() // get the arguments from command line

         destinationfile := flag.Arg(0)

         if destinationfile == "" {
                 fmt.Println("Usage : gotar destinationfile.tar.gz source")
                 os.Exit(1)
         }

         sourcedir := flag.Arg(1)

         if sourcedir == "" {
                 fmt.Println("Usage : gotar destinationfile.tar.gz source-directory")
                 os.Exit(1)
         }

         dir, err := os.Open(sourcedir)

         checkerror(err)

         defer dir.Close()

         files, err := dir.Readdir(0) // grab the files list

         checkerror(err)

         tarfile, err := os.Create(destinationfile)

         checkerror(err)

         defer tarfile.Close()
         var fileWriter io.WriteCloser = tarfile

         if strings.HasSuffix(destinationfile, ".gz") {
                 fileWriter = gzip.NewWriter(tarfile) // add a gzip filter
                 defer fileWriter.Close()            // if user add .gz in the destination filename
         }

         tarfileWriter := tar.NewWriter(fileWriter)
         defer tarfileWriter.Close()

         for _, fileInfo := range files {

                 if fileInfo.IsDir() {
                    continue
                 }

                 // see https://www.socketloop.com/tutorials/go-file-path-independent-of-operating-system

                 file, err := os.Open(dir.Name() + string(filepath.Separator) + fileInfo.Name())

                 checkerror(err)

                 defer file.Close()


                 // prepare the tar header

                 header := new(tar.Header)
                 header.Name = file.Name()
                 header.Size = fileInfo.Size()
                 header.Mode = int64(fileInfo.Mode())
                 header.ModTime = fileInfo.ModTime()


                 err = tarfileWriter.WriteHeader(header)

                 checkerror(err)

                 _, err = io.Copy(tarfileWriter, file)

                 checkerror(err)
         }

 }