[英]Servlet to JSP certain element in ArrayList
我试图将特定元素从我的模型传递到servlet到jsp。 这在我的servlet中System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1])
: System.out.println(beanModel.getSortedDomainList().get(0).split(";")[1])
当我转到http:// localhost:8080 / Comparebet / Controller但我不知道我在做什么错,因为我没有把它交给我的jsp。 我一直关注的所有教程都是输入参数,或者将代码放在JSP中。
更新编辑在我的JSP中对此进行了尝试,但我得到了“空”
<%= request.getAttribute("rank1") %>
SERVLET
@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>
尝试在您的jsp中替换此行:
<h1>${rank1.getSortedDomainList().get(0).split(";")[1] } </h1>
对此:
<h1>${rank1} </h1>
在JSP中,您可以使用${rank1}
类的表达语言访问此数据。
并从<form
这样取出:
<form action="/Controller/*" method="get">
...
</form>
尝试使用<h1><%= request.getParameter("rank1")%></h1>
在您的JSP中尝试此操作,而不是rank1.getSortedDomainList().get(0).split(";")[1]
<h1>${request.rank1 } </h1>
您已经在beanModel.getSortedDomainList().get(0).split(";")[1]
完成了beanModel.getSortedDomainList().get(0).split(";")[1]
,并将结果添加到了请求范围对象中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.