我想确定哪个链接正在点击,并相应地设置一个请求属性;在控制器中处理。控制器是通用的。它将根据JSP中设置的属性调度到另一个页面。我怎样才能做到这一点?
编辑:
我遵循了BalusC的建议
<a href="RandomController/Register">Register</a>
<a href="RandomController/Login">Login</a>
RandomController :
@WebServlet(name = "RandomController", urlPatterns = {"/RandomController/*"})
public class RandomController extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int theRandom = 0;
Random myRandom = new Random();
RequestDispatcher dispatcher = null;
String pathInfo = request.getPathInfo();
String contextPath = request.getContextPath();
String dispatchesPath = contextPath + pathInfo;
System.out.println(pathInfo);
System.out.println(contextPath);
System.out.println(dispatchesPath);
theRandom = myRandom.nextInt(MAX_RANDOM);
request.setAttribute("random", theRandom);
if(pathInfo.equals("/Login")) {
dispatcher = request.getRequestDispatcher("Login.jsp");
dispatcher.forward(request, response);
}
else {
dispatcher = request.getRequestDispatcher("Register.jsp");
dispatcher.forward(request, response);
}
}
}
我尝试过这种方法但是关于嵌套请求调度程序的最大深度的异常:20是throw。 通用控制器是RandomController,服务器2生成随机数并在请求对象中设置为属性,并将其发送到登录或注册页面。
Login.jsp:
<%--
Document : Register
Created on : May 28, 2011, 5:49:35 PM
Author : nicholas_tse
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Online Store Register Page</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<jsp:useBean id="randomBean" class="Helper.RandomInt"
scope="request">
</jsp:useBean>
<h1>Online Store</h1>
<p></p>
<div align="right">
<h3>
<c:if test="${not empty sessionScope.username}">
! Welcome ${sessionScope["username"]} !
<form action="LogoutController" method="POST">
<input type="submit" name="submit" value="Logout"/>
</form>
</c:if>
</h3>
</div>
<ul id="nav_list">
<li><a href="http://localhost:8080/OnlineStore">Home</a></li>
<li><a href="Product.jsp">Product</a></li>
<li><a href="">Add Stock</a></li>
<li><a href="">Shopping Cart</a></li>
<li><a href="">Order Status</a></li>
<li><a href="RandomController/Register">Register</a></li>
<li><a href="RandomController/Login">Login</a></li>
</ul>
<br></br>
<p></p>
<c:if test="${!not empty sessionScope.username}">
<div id="registerForm" align="center"
style="border:1px solid black; width:760px" >
<form name="RegisterForm" action="RegisterController"
method="POST">
<p>
Username : <input type="text" id="username" name="username">
<c:if test="${message.msg} != null" >
${message.msg}
</c:if>
</p>
<p>
Password : <input type="password" id="password" name="password"
</p>
<p>
Name : <input type="text" id="name" name="name">
<c:if test="${message.msg} != null" >
${message.msg}
</c:if>
</p>
<p>
Address : <input type="text" id="address" name="address">
<c:if test="${message.msg} != null" >
${message.msg}
</c:if>
</p>
<p>
State :
<select>
<option>Selangor</option>
<option>Kuala Lumpur</option>
<option>Cyberjaya</option>
<option>Putrajaya</option>
</select>
</p>
<p></p>
<input type="submit" name="submit" value="Register">
<input type="reset" name="clear" value="Clear">
<p></p>
</form>
</div>
</c:if>
</body>
</html>
答案 0 :(得分:2)
如果没有进一步的信息,我能想到的唯一建议是为您的网址添加查询字符串。像这样:
<a href="mycontroller.do?action=page1">Page 1</a>
通过这种方式,您可以查看请求中的actiuon参数,以确定控制器中的操作。
答案 1 :(得分:0)
您可以添加请求参数。这可以通过两种方式完成,具体取决于您的表单是否具有POST或GET方法。
答案 2 :(得分:0)
您已在name
属性submit
和不同value
属性的表单中实施了提交按钮。这应该意味着请求将在查询字符串或POST表单数据中包含submit
参数。在服务器端,您可以使用它来确定单击了哪些提交按钮。