我想确定哪个链接被点击,并相应地设置一个请求属性;在 Controller 中进行处理。 Controller 是通用的。它将根据 JSP 中设置的属性分派(dispatch)到另一个页面。我怎样才能做到这一点?
编辑:
我听从了 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。 通用 Controller 是 RandomController,它的两个服务器生成随机数并将其设置为请求对象中的属性,并将其发送到登录或注册页面。
登录.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>
最佳答案
在没有更多信息的情况下,我唯一能想到的建议是向您的 URL 添加一个查询字符串。像这样:
<a href="mycontroller.do?action=page1">Page 1</a>
这样您就可以查看请求中的 actiuon 参数以确定在您的 Controller 中执行的操作。
关于java - 根据点击的链接设置请求属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6165810/