问题描述
我有一个jsp,向我展示了一个来自基地的学生名单。对于每个学生我想要2个按钮 - 编辑和删除。在编辑上单击该请求应重定向到我的servlet控制器,并打开一个新的jsp编辑选定的学生的数据。在删除单击一个删除请求选定的学生被发送到控制器。现在看起来像这样:
<%@ page import =socnet2.Student%>
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title> JSP Page< / title>
< / head>
< body>
< jsp:useBean class =socnet2.DAOid =daoscope =request>< / jsp:useBean>
< p>< b>所有学生的列表:< / b>< / p>
< form action =/ JSP1 / Controlermethod =POST>
< jsp:scriptlet>
(Student's:dao.getAllStudents()){
< / jsp:scriptlet>
< p> < JSP:表达> s.getName()++ s.getSurname()< / jsp:expression>< / p>
< input type =submitname =Editvalue =Edit/>
< input type =submitname =Deletevalue =Delete/>
< jsp:scriptlet>
}
< / jsp:scriptlet>
< / form>
< / body>
问题是我无法弄清楚如何连接我的学生与按钮当请求被发送给控制器时,知道哪个按钮被点击以及选择了什么学生。我知道每个学生都需要一些独特的ID,但不知道如何创建它......
学生
已经有某种类型的ID。比方说,它有私有字符串id
和适当的getter public String getId()
。 在这种情况下,您应该在生成提交按钮的名称时使用此ID:
< input type =submitname =Edit_<%= s.getId()%> value =编辑/>
< input type =submitname =Delete_<%= s.getId()%> value =删除/>
现在您可以区分服务器端的学生。
另外,您可以为每位学生创建单独的表单,并为每个表单分配唯一的网址:
< form method =发布url =http:// myhost / myapp / students /<%s.getId()%>>
I have a jsp that shows me a list of students from base. For each student I want 2 buttons - Edit and Delete. On Edit click the request should be redirected to my servlet-controller and a new jsp for editing selected student's data is opened. On Delete click a delete request for selected student is sent to controler. Now it looks like this:
<%@page import="socnet2.Student"%>
<!-- class Student emulates real student -->
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean class="socnet2.DAO" id="dao" scope="request"></jsp:useBean>
<p><b>List of all students:</b></p>
<form action="/JSP1/Controler" method="POST">
<jsp:scriptlet>
for (Student s : dao.getAllStudents()) {
</jsp:scriptlet>
<p> <jsp:expression> s.getName() + " " + s.getSurname()</jsp:expression></p>
<input type="submit" name="Edit" value="Edit" />
<input type="submit" name="Delete" value ="Delete"/>
<jsp:scriptlet>
}
</jsp:scriptlet>
</form>
</body>
The problem is I can't figure out how to connect my students with buttons in a way that when request is sent to contoller it is known which button was clicked and what student was selected. I understand I need some unique id for each student, but don't know how to create it...
I believe that Student
already have some kind of ID. Let's say for example that it has private String id
and appropriate getter public String getId()
.
In this case you just should use this ID when generating name of submit buttons:
<input type="submit" name="Edit_<%=s.getId()%>" value="Edit" />
<input type="submit" name="Delete_<%=s.getId()%>" value ="Delete"/>
Now you can distinguish between students on server side.Alternatively you can create separate form for each student and give to each form its unique URL:
<form method="post" url="http://myhost/myapp/students/<%s.getId()%>">
这篇关于在JSP中处理多个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!