这是 servlet:-6ren">
gpt4 book ai didi

java - 无法从列表(jsp、servlet)中获取选定的值

转载 作者:行者123 更新时间:2023-11-29 08:00:00 25 4
gpt4 key购买 nike

抱歉我的英语不好:)有人可以帮助我吗?我创建了包含员工姓名的列表。当您单击其姓名时,每个员工都应该有自己的页面,其中包含有关所选员工的信息。这是形式:

<form name="submitForm" method="get" action="zakaz" id="form1"  class="dropdown-menu">
<%for(int i=0;i<dataList4.size();i++){%>
<input type="hidden" name="fio" value="<%=dataList4.get(i)%>">
<ul class="dropdown-menu" style="width:500px;">
<li><a href=# onclick="document.forms['form1'].submit();return false;"><%=dataList4.get(i)%></a></li>
</ul>
<%}%>
</form>

这是 servlet:

String fio = request.getParameter("fio");
String query = "select * from culture where name like'"+fio+"'";
try{
try (Statement s = con.createStatement()) {
s.executeQuery(query);
rs = s.getResultSet();

while (rs.next()) {
dataList4.add(rs.getString("name"));
dataList4.add(rs.getString("room"));
dataList4.add(rs.getString("number"));
}

rs.close();
}
}catch (Exception e) {
System.out.println("Exception is ;" + e);
}

request.setAttribute("dataList4",dataList4);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/zakaz.jsp");
dispatcher.forward(request, response);

并转发到另一个 JSP:

<body>
<%
List booklist=new ArrayList();
booklist=(ArrayList)request.getAttribute("dataList4");
for(int i=0;i<booklist.size();i++){
booklist.get(i);
}
%>
<ul>
<li>ФИО: <span><%=booklist.get(0) %></span></li>
<li>Кабинет:<span> <%=booklist.get(1) %></span></li>
<li>Телефон: <span><%=booklist.get(3) %></span> </li>
</ul>

所有数据均取自 db Mysql,当我这样做时,我仅获得列表中第一个员工的信息,它并不适用于每个员工。

最佳答案

<form name="submitForm" method="get" action="zakaz" id="form1"  class="dropdown-menu">
<input type="hidden" name="fio" id="fio" >
<ul class="dropdown-menu" style="width:500px;">
<%for(int i=0;i<dataList4.size();i++){%>
<li><a href="javascript:void(0)" onclick="submitForm('<%=dataList4.get(i)%>')"><%=dataList4.get(i)%></a></li>
<%}%>
</ul>

<script>
function submitForm(name){
document.getElementById("fio").value = name;
document.forms['form1'].submit();
// return false;
}
</script>

关于java - 无法从列表(jsp、servlet)中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075212/

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