【发布时间】:2016-01-31 22:48:18
【问题描述】:
我正在尝试将特定元素从我的模型传递到 servlet 到 jsp。
这在我的 servlet 中工作:System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1]) 当我转到 http://localhost:8080/Comparebet/Controller
但我不知道我做错了什么,因为我没有把它放到我的 jsp 中。我看的教程大部分都是输入参数或者把代码放在JSP里。
更新编辑 在我的 JSP 中尝试过这个,我得到“null”
<%= request.getAttribute("rank1") %>
伺服器
@WebServlet("/Controller")
public class Controller extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ArrayList<String> sortedDomainList = new BeanModel().getSortedDomainList();
BeanModel beanModel = new BeanModel();
request.setAttribute("rank1", beanModel.getSortedDomainList().get(0).split(";")[1]);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
//TESTING SERVLET
System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1]);
System.out.println("CONTROLLER CALLED");
// System.out.println(${rank1});
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
}
}
JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>CompareBet</title>
</head>
<body>
<form action="/Controller/*" method="get">
<h1>${rank1.getSortedDomainList().get(0).split(";")[1] } </h1>
</form>
</body>
</html>
【问题讨论】:
-
你能告诉我们你的 beanmodel 类吗?
-
为什么需要看?它的工作我也在普通的 Java 应用程序中尝试过。当我在 Servlet 中打印它时它就可以工作了。
-
我希望你能比这更具体。它打印一个字符串吗?
-
试试这个
<h1>Testing: ${request.rank1 } </h1>。您看到用 JSP 打印的“测试”了吗?我想知道您是否正在更新正确的 JSP。 -
@Armaiti 是的,我看到“测试:”。
标签: java jsp servlets model-view-controller