gpt4 book ai didi

java - 使用单个提交按钮发送下拉列表的多个值

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:06 25 4
gpt4 key购买 nike

我正在尝试制作一个如下所示的表单。每行有两个下拉菜单:第一个是仅选择一个,另一个是选择多个。 Here is the output screen.

在此屏幕上,我想使用单个提交按钮将所有行(包括两个下拉列表)的值发送到 servlet (Testing.java)。在Testing.java中,我基本上希望能够访问与每个测试名称对应的每行的所有值。

这是我的 JSP 代码,用于图像中显示的输出:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import = "java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" href="jquery chosen utils/chosen.css">
<link rel="stylesheet" type="text/css" href="jquery chosen utils/welcomestyle.css">
</head>
<body>
<div align = center>
<table style="width: 100%">
<tr>
<th> Script Name </th>
<th> Main Action Keywords </th>
<th> Sub Action Keywords </th>
</tr>
</table>
<%
for(int i =0; i<5;i++)
{
%>
<form action = "Testing">
<table style="width: 100%">
<tr>
<td> Test <%= i %> </td>
<td>
<div>
<select name = "Actions" class = "Actions" >
<%
for(int j=0;j<4;j++)
{
%>
<option value="Action<%= j %>">
Action <%= j %>
</option>
<%
}
%>
</select>
</div>
</td>
<td>
<select name = "SubActions" class = "SubActions" multiple>
<%
for(int k=0;k<4;k++)
{
%>
<option value="SubAction<%= k %>">
Sub Action <%= k %>
</option>
<%
}
%>
</select>
</td>
</tr>
</table>
<%
}
%>
<br><br>
<input type = submit value = "Submit" name ="submit" />
</form>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="jquery chosen utils/chosen.jquery.js" type="text/javascript"></script>
<script src="jquery chosen utils/prism.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".Actions").chosen({
width: "75%"
});
$(".SubActions").chosen({
width: "75%"
});
});
</script>
</html>

我希望我的Testing.java 是这样的:

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Testing")
public class Testing extends HttpServlet
{

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{


String a = request.getParameter("Actions"); //Should get actions corrsponding to each test
String b = request.getParameter("SubActions"); //Should get subactions corrsponding to each test


}
}

当前的Testing.java仅返回第一行(Test 0)的操作和第一个子操作。我主要使用 HTML 和一些 jQuery 作为下拉菜单。

任何人都可以给我建议如何做同样的事情吗?

最佳答案

您正在寻找的是Array对于操作2D Array对于子操作:

也适用于子操作

更改:
<select name = "SubActions" class = "SubActions" multiple>
至:
<select name = "SubActions[<%= i %>]" class = "SubActions" multiple>

要访问 Servlet 中的值,请执行以下操作:

String[] actions =request.getParameterValues('Actions');
int length = 5 ; // Length of Rows
String[][] subActions = new String[length][];

for (int i = 0; i < length; i++) {
subActions[i] = request.getParameterValues("SubActions["+i+"]");
}

我可以看到您用于创建行的 5 循环,因此它已修复。
如果您的行(选择)是动态/可增长,则在Servlet中,您需要以动态方式访问它。然后在 Servlet 中动态更改长度:

int length = 0 ;       // Length of Rows
if( actions != null )
length = actions.length; // Calculate Rows Dynamic

关于java - 使用单个提交按钮发送下拉列表的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com