[英]Load Bean from Servlet to JSP
I already searched a lot but can't find my error. 我已经搜索了很多,但是找不到我的错误。 I have a servlet and try to put an ArrayList in a bean:
我有一个servlet并尝试将ArrayList放入bean中:
ClientBean c = new ClientBean();
c.setList(ClientsHandler.getAllClients());
request.setAttribute("listClients", c);
RequestDispatcher dispatcher = request.getRequestDispatcher("showClients.jsp");
dispatcher.forward(request, response);
In the "showClients.jsp" i try to print the phone of the client 1: 在“ showClients.jsp”中,我尝试打印客户端的电话1:
<jsp:useBean id="listClients" class="beans.ClientBean" scope="request"/>
<% ArrayList<ClientsRowGateway> list = ((beans.ClientBean)request.getAttribute("listClients")).getList(); %>
<% out.println( ""+list.get(1).getPhone() ); %>
But i have a NullPointerException, because my object list is null. 但是我有一个NullPointerException,因为我的对象列表为null。 How can i have access to a variable(getPhone()) inside of an object(Client) inside of an ArrayList inside of a bean???
我怎样才能访问Bean内ArrayList内的object(Client)内的变量(getPhone())?
After more tests, i try to access my ArrayList directly without a bean and works, print the client phone! 经过更多测试后,我尝试不使用Bean直接访问我的ArrayList并工作,打印客户端电话!
<%
ArrayList<ClientsRowGateway> testList = ClientsHandler.getAllClients();
if( testList != null )
out.println( testList.get(1).getPhone() );
%>
But i need to use a bean, i just do this for a test, so can someone help me? 但是我需要使用一个bean,我只是做一个测试,所以有人可以帮助我吗?
Why don't you use the EL (Expression Language)?? 为什么不使用EL(表达语言)? to access the second item the sentence will be
${listClients.list[1].phone}
. 要访问第二个项目,该句子将为
${listClients.list[1].phone}
。
If you can't use EL, when you use the useBean tag you are already declaring an variable so you can access directly to the bean so. 如果不能使用EL,则在使用useBean标记时已经声明了一个变量,因此可以直接访问Bean。
<jsp:useBean id="listClients" class="beans.ClientBean" scope="request"/>
<% ArrayList<ClientsRowGateway> list = listClients.getList(); %>
<% out.println( ""+list.get(1).getPhone() ); %>
I suggest you to check if the getList method return correctly the list. 我建议您检查getList方法是否正确返回列表。
I hope this will help you 我希望这能帮到您
Check if you are loading the correct .jars. 检查是否正在加载正确的.jars。 Usually for this kind of stuff you need some specific jars to run JSP's/servlets on the server.
通常,对于这种东西,您需要一些特定的jar才能在服务器上运行JSP / servlet。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.