我是web开发的初学者。我已经将jsp和servlet用于我的项目。我创建了两个jsp文件,一个是内容,所有用户的值和一个内容为特定用户的值。如何连接拖动jsp.Passing值从一个到另一个按一个按钮,将重定向用户列表太用户。
<form name="admin_users" action="${pageContext.request.contextPath}/admin/users" method="GET">
<ul class="listing">
<c:forEach var="obj" items="${list}">
<li>
<div class="listinfo"> <img src="${pageContext.request.contextPath}/images/imageholder.jpg" alt="" class="listingimage" />
<div>
<table>
<tr>
<td width="10%" class="label">Nickname</td>
<td name="nickname" id="nickname" class="text longfield" value="${nickname}">
<c:out value="${obj.nickname}"/>
</td>
</tr>
<tr>
<td width="10%" class="label">Email</td>
<td name="email" id="email" class="text longfield" value="${email}">
<c:out value="${obj.email}"/>
</td>
</tr>
<tr>
<td width="10%" class="label">Phone Number</td>
<td name="phoneNumber" id="phoneNumber" class="text longfield" value="${phoneNumber}">
<c:out value="${obj.phoneNumber}"/>
</td>
</tr>
</table>
</div>
</div>
<!-- <div class="listingbtns"> <span class="listbuttons"> <a href="#">View Details</a></span>
</div>-->
<div>
<button type="button" onclick="viewdeteils()"/>View Details</button>
</div>
<div class="clear"> </div>
</li>
</c:forEach>
</ul>
<div id="single_item_details">
<form name="admin_user" action="${pageContext.request.contextPath}/admin/users/user" method="GET">
<div id="leftcolumn">
<div>
<img src="${pageContext.request.contextPath}/images/imageholder_detailspage.jpg" alt="" width="220" height="220" class="previewimg" />
</div>
<div>
<button type="button" onclick="alert('activate')">Activete</button>
</div>
</div>
<div id="rightcolumn">
<table class="table">
<tr>
<td width="10%" class="td_attributes">Nickname</td>
<td name="nickname" id="nickname" class="text longfield" value="${nickname}">
<c:out value="${nickname}"/>
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Name</td>
<td name="firstname" id="firstname" class="text longfield" value="${firstname}">
<c:out value="${firstname}"/>
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Surname</td>
<td name="surname" id="surname" class="text longfield" value="${surname}">
<c:out value="${surname}"/>
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Email</td>
<td name="email" id="email" class="text longfield" value="${email}">
<c:out value="${email}"/>
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Phone</td>
<td name="phonenumber" id="phonenumber" class="text longfield" value="${phonenumber}">
<c:out value="${phonenumber}"/>
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Role</td>
<td name="role" id="role" class="text longfield" value="${role}">
</td>
</tr>
<tr>
<td width="10%" class="td_attributes">Active</td>
<td name="active" id="active" class="text longfield" value="${active}">
<c:out value="${active}"/>
</td>
</tr>
</table>
<div>
<button type="button" onclick="alert('edit')">Edit</button>
</div>
</div>
<div class="clear"> </div>
</form>
我可以举一个例子。所以按下用户列表上的按钮查看详细信息并重定向到用户档案jsp。
答案 0 :(得分:1)
我知道有两种方法可以用来做到这一点。
1:环绕表格
您可以使用表单在用户列表中包围该表,然后隐藏输入。每个表单都有一个按钮,无论用户点击什么按钮,都会发送哪些变量。
<c:forEach var="obj" items="${list}" varStatus="count">
<form action="GetUser" method="get">
<div class="listinfo"> <img src="${pageContext.request.contextPath}/images/imageholder.jpg" alt="" class="listingimage" />
<div>
<table>
<tr>
<td width="10%" class="label">Nickname</td>
<td id="nickname" class="text longfield">
nickname: <c:out value="${obj.nickname}"></c:out>
</td>
</tr>
<tr>
<td width="10%" class="label">Email</td>
<td id="email" class="text longfield" >
email: <c:out value="${obj.email}"></c:out>
</td>
</tr>
<tr>
<td width="10%" class="label">Phone Number</td>
<td id="phoneNumber" class="text longfield">
<c:out value="${obj.phoneNumber}"></c:out>
</td>
</tr>
</table>
</div>
</div>
<input type="hidden" name="nickname" value="${obj.nickname}">
<input type="hidden" name="email" value="${obj.email}">
<input type="hidden" name="phoneNumber" value="${obj.phoneNumber}">
<div>
<button type="submit">View Details</button>
</div>
<div class="clear"> </div>
</form>
</c:forEach>
1:通过链接提交
另一个选项是使用带有href属性的锚标记,该属性将值发送到GetUser servlet。注意:这仅适用于GET请求。因为GET请求被映射到您的servlet url(检查web.xml)。 POST请求无法执行此操作。 另请注意:&#34; ../"您的设置可能不需要。例如,如果用户位于url website.com/users/randomName。然后它将是必需的。
例如:
<c:forEach var="obj" items="${list}" varStatus="count">
<a href="../GetUser?nickname="${obj.nickname}"&email="${obj.email}"&phoneNumber="${obj.phoneNumber}">${obj.nickname}</a>
</c:forEach>
查看发送内容的好建议 右键单击页面并单击inspect,如果你继续使用网络标题,在这里你可以看到发送到你的servlet的内容。它对调试很有用。
Servlet:
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetUser extends HttpServlet {
private static final long serialVersionUID = 1L;
public GetUser() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get the values submitted to servlet
String nickName = request.getParameter("nickname");
String email = request.getParameter("email");
String number = request.getParameter("phoneNumber");
//set the values that will be accessed in userprofile
request.setAttribute("name", nickName);
request.setAttribute("email", email);
request.setAttribute("number", number);
RequestDispatcher rd=request.getRequestDispatcher("Userprofile.jsp");
rd.forward(request,response);
}
}
要在userprofile jsp中访问这些变量,只需:
${name}
${email}
${number}
希望有所帮助。随意问什么。