将数组从servlet传递到jsp以及bean

时间:2017-04-29 06:18:49

标签: java jsp servlets arraylist packages

我将一个bean数组从servlet传递给jsp。我还想为每个bean发送状态“onHand”。我正在使用arrayList作为状态。

在Servlet中:

    import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import pckg.ProductBean;



public class GetProducts extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException  {
    PrintWriter out = response.getWriter();
    Vector<String[]> v = DBHelper.runQuery("SELECT * FROM SKU;");
    ProductBean [] beans = new ProductBean[v.size()];
    ArrayList<String> onHand=new ArrayList<String>();
    //onHand.add("a");
    for(int i=0; i < v.size(); i++)  {
        String [] tmp = v.elementAt(i);

        Vector<String[]> on = DBHelper.runQuery("SELECT on_hand_quantity FROM on_hand where sku='"+tmp[0]+"' ;");
        if((on.size())>0){
        String [] tmp1 = on.elementAt(0);
        if(Integer.parseInt(tmp1[0])>0){
            onHand.add("InStock");}
            else if(Integer.parseInt(tmp1[0])==0){
            onHand.add("MoreOnTheWay");
            }

        }
        beans[i] = new ProductBean(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],
            tmp[5],tmp[8],Double.parseDouble(tmp[6]),Double.parseDouble(tmp[7]));

        }
        request.setAttribute("p_beans",beans);
    request.setAttribute("onHand",onHand);





    String toDo = "/WEB-INF/jsp/cameraList.jsp";
        RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher(toDo); 
        dispatcher.forward(request, response);                              

    }
}

在JSP页面中:

    <%@ page import = "java.util.*"%>

<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
    <link rel="stylesheet" href="../css/cameraList.css" type="text/css"></link>

   <script src="../script/jquery.js"></script>    
    <script src="../script/cameraList.js"></script>
</head>


<body>

    <div id="main"> 
     <% ArrayList<String> onHand=(ArrayList<String>) request.getAttribute("onHand");%>

    <c:forEach items="${p_beans}" var="item">


    <table >        
        <tr><td rowspan=5><img id="image" src="upload_dir/${item.image}" style="height:350px; width:350px; background-color:yellow";></td>

        <td><b> ${item.vendor}  ${item.model}  ${item.category}</b></td></tr>

        <tr><td width=45%> Price:$${item.retail}</td><td>Status:</td></tr>

        <c:url value="/servlet/GetProductDetails?" var="myURL">
                <c:param name="sku" value="${item.sku}" />
                </c:url>
        <tr><td><a href="${myURL}" >Get Details</a></td>
        <c:url value="/servlet/OrderPage" var="cartURL">
                <c:param name="itemID" value="${item.sku}" />
                </c:url>
        <td><a href="${cartURL}">Add To Cart</a></td></tr>




    </table>    
    </c:forEach>

</div>



</body>
</html>

我在服务器中收到以下错误: 在第44行处理JSP页面/WEB-INF/jsp/cameraList.jsp时发生异常:

41: <body>
42: 
43:     <div id="main"> 
44:     <% ArrayList<String> onHand=(ArrayList<String>) request.getAttribute("onHand");%>
45:     <c:forEach items="${p_beans}" var="item">
46:     
47:     <table >

3 个答案:

答案 0 :(得分:1)

request.getParameter()总是返回一个String,它主要用于从用户提交的表单中获取数据或从查询字符串中获取数据。 因为您正在调用 getParameter(),所以它将返回String,因此您将获得该异常。

根据您的问题,因为您要将 onHand 字段的值设置为请求中的属性,所以您应该在JSP中调用 getAttribute()方法。 getAttribute()的返回类型是Object。

答案 1 :(得分:0)

 <%
 String param = request.getAttribute("onHand");
%>

<c:forEach items="${p_beans}" var="item">
    <tr>
        <td>iterated fields</td>
        <td>
        ..
        ..
        <td><%=param%></td>


    </tr>

答案 2 :(得分:0)

  

无法从String转换为ArrayList

您的onHand对象似乎是String类型,您正在设置您的servlet /控制器类(使用request.setAttribute("onHand",onHand)),因此您需要对String进行类型转换而不是如下所示的ArrayList<String>

<% String onHand= (String)request.getAttribute("onHand"); %>

此外,重要的是您需要使用 request.getAttribute(key)来检索JSP中的对象