JSP 页面无法识别我的数据类型

发布于 2024-11-28 22:53:34 字数 4617 浏览 0 评论 0原文

我正在将 Eclipse Helios 与 Struts 一起使用,并且遇到了我想象中的菜鸟问题:我一生都无法弄清楚为什么我的 jsp 页面不会承认我的业务对象 .java 文件。

我认为这与 .java 类文件有关,因为我无法让它们出现在“库资源”标题下的“导入类”部分中。到目前为止,我能做的最好的事情就是将类文件作为单独的 jar 加载(当然,它仍然不会出现在 IMPORTED CLASSES 部分中)。

有谁知道为什么我的 cart.jsp 文件不允许我引用我的 Cart.java 业务对象?当我尝试创建下面的 Cart、LineItem 和 Product 对象时,出现空指针错误。

以下是一些片段:

webapp\WebContent\example\cart.jsp

 <%@ page import="bo.*, java.util.ArrayList" %>
 <% 
    Cart cart = (Cart) session.getAttribute("cart"); 
    ArrayList<LineItem> items = cart.getItems();
    for (LineItem item : items)
    {
        Product product = item.getProduct();
 %>

   <tr valign="top">
     <td>
       <form action="<%=response.encodeURL("cart")%>" method="post">
         <input type="hidden" name="productCode" 
           value="<%=product.getCode()%>">
         <input type=text size=2 name="quantity" 
           value="<%=item.getQuantity()%>">
         <input type="submit" value="Update">
       </form>
    </td>
    <td>
 <%=product.getDescription()%>
    </td>
    <td>
 <%=product.getPriceCurrencyFormat()%>
    </td>
    <td>
 <%=item.getTotalCurrencyFormat()%>
    </td>
    <td>
       <form action="<%= response.encodeURL("cart")%>" method="post">
         <input type="hidden" name="productCode" 
           value="<%=product.getCode()%>">
         <input type="hidden" name="quantity" 
           value="0">
         <input type="submit" value="Remove Item">
       </form>
    </td>
 </tr><% } %>

Java Resources\src\bo\Cart.java

 package bo;

 import java.util.*;
 import java.io.Serializable;

 public class Cart implements Serializable
 {
     private ArrayList<LineItem> items;

     public Cart()
     {
         items = new ArrayList<LineItem>();
     }

     public void setItems(ArrayList<LineItem> lineItems)
     {
         items = lineItems;
     }

     etc. . . .      

Java Resources\src\bo\DisplayCartServlet。 java

 package action;

 import java.io.*;
 import java.sql.SQLException;

 import javax.servlet.*;
 import javax.servlet.http.*;
 import org.apache.struts.action.*;

 import bo.*;
 import dao.*;

 public class DisplayCartServlet extends Action
 {
     public ActionForward execute(ActionMapping mapping, ActionForm form,      HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
     {

        String forward = new String("success");     ;
         String productCode = request.getParameter("productCode");      

         HttpSession session = request.getSession();

         Cart cart = (Cart) session.getAttribute("cart");  
         if (cart == null)
         {
            cart = new Cart();
            session.setAttribute("cart", cart);
         }

         int quantity = 1;

         // Get product from product code
         Product product=null;
    try {
        product = ProductDB.selectProduct(productCode);         
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         session.setAttribute("product", product);

         // If product exists, add or remove from cart
         if (product != null)
         {
            LineItem lineItem = new LineItem();
            lineItem.setProduct(product);
            lineItem.setQuantity(quantity);
            if (quantity > 0)
              cart.addItem(lineItem);
            else
            cart.removeItem(lineItem);
          }
          session.setAttribute("cart", cart);

          return(mapping.findForward(forward));
     }

 }

Struts.xml声明:

 <struts>

 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="false" />

   <package name="example" namespace="/example" extends="struts-default">

     <action name="cart" class="action.DisplayCartServlet" method="execute">
        <result name="success">/example/cart.jsp</result>
     </action>

   </package>
  . . . . 
  </struts>

请求action/servlet的链接:

 <div id="cartLink"><a href="<s:url action="cart?productCode=XM123456"/>">Add to Cart</a></div> 

I am using Eclipse Helios with Struts and am having what I imagine is a rookie problem: for the life of me I have not been able to figure out why my jsp page will not acknowledge my business object .java file.

I think it has something to do with the .java class files as I have been unable to get them to appear in the IMPORTED CLASSES section under my Library Resources heading. The best I have been able to do thus far is load the class files as a seperate jar (which of course still doesn't appear in the IMPORTED CLASSES section).

Does anyone know why my cart.jsp file refuses to allow me to reference my Cart.java business object? I get a null pointer error when I try to create the Cart, LineItem, and Product objects below.

Here are some snippets:

webapp\WebContent\example\cart.jsp

 <%@ page import="bo.*, java.util.ArrayList" %>
 <% 
    Cart cart = (Cart) session.getAttribute("cart"); 
    ArrayList<LineItem> items = cart.getItems();
    for (LineItem item : items)
    {
        Product product = item.getProduct();
 %>

   <tr valign="top">
     <td>
       <form action="<%=response.encodeURL("cart")%>" method="post">
         <input type="hidden" name="productCode" 
           value="<%=product.getCode()%>">
         <input type=text size=2 name="quantity" 
           value="<%=item.getQuantity()%>">
         <input type="submit" value="Update">
       </form>
    </td>
    <td>
 <%=product.getDescription()%>
    </td>
    <td>
 <%=product.getPriceCurrencyFormat()%>
    </td>
    <td>
 <%=item.getTotalCurrencyFormat()%>
    </td>
    <td>
       <form action="<%= response.encodeURL("cart")%>" method="post">
         <input type="hidden" name="productCode" 
           value="<%=product.getCode()%>">
         <input type="hidden" name="quantity" 
           value="0">
         <input type="submit" value="Remove Item">
       </form>
    </td>
 </tr><% } %>

Java Resources\src\bo\Cart.java

 package bo;

 import java.util.*;
 import java.io.Serializable;

 public class Cart implements Serializable
 {
     private ArrayList<LineItem> items;

     public Cart()
     {
         items = new ArrayList<LineItem>();
     }

     public void setItems(ArrayList<LineItem> lineItems)
     {
         items = lineItems;
     }

     etc. . . .      

Java Resources\src\bo\DisplayCartServlet.java

 package action;

 import java.io.*;
 import java.sql.SQLException;

 import javax.servlet.*;
 import javax.servlet.http.*;
 import org.apache.struts.action.*;

 import bo.*;
 import dao.*;

 public class DisplayCartServlet extends Action
 {
     public ActionForward execute(ActionMapping mapping, ActionForm form,      HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
     {

        String forward = new String("success");     ;
         String productCode = request.getParameter("productCode");      

         HttpSession session = request.getSession();

         Cart cart = (Cart) session.getAttribute("cart");  
         if (cart == null)
         {
            cart = new Cart();
            session.setAttribute("cart", cart);
         }

         int quantity = 1;

         // Get product from product code
         Product product=null;
    try {
        product = ProductDB.selectProduct(productCode);         
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         session.setAttribute("product", product);

         // If product exists, add or remove from cart
         if (product != null)
         {
            LineItem lineItem = new LineItem();
            lineItem.setProduct(product);
            lineItem.setQuantity(quantity);
            if (quantity > 0)
              cart.addItem(lineItem);
            else
            cart.removeItem(lineItem);
          }
          session.setAttribute("cart", cart);

          return(mapping.findForward(forward));
     }

 }

Struts.xml declaration:

 <struts>

 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="false" />

   <package name="example" namespace="/example" extends="struts-default">

     <action name="cart" class="action.DisplayCartServlet" method="execute">
        <result name="success">/example/cart.jsp</result>
     </action>

   </package>
  . . . . 
  </struts>

Link that requests action/servlet:

 <div id="cartLink"><a href="<s:url action="cart?productCode=XM123456"/>">Add to Cart</a></div> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-05 22:53:34

您期望购物车出现在(新的)HttpSession 中。假设 HttpSession 存在,因为您正在 tomcat/jetty 中运行,并且这些 servlet 容器中的任何一个都会为您创建一个会话。

现在您在该会话中请求购物车。你为什么期望它在那里?

Cart cart = (Cart) session.getAttribute("cart");

如果 session.getAttribute("cart") 返回 null 并将其存储在会话对象中,您可以创建一个。

You expect a cart to be present in a (new) HttpSession. Given an HttpSession exists because you are running in tomcat/jetty and either of these servlet containers will create a session for you.

Now you ask for a cart in that session. Why do you expect it to be there?

Cart cart = (Cart) session.getAttribute("cart");

You could create one if session.getAttribute("cart") returns null and store it in your session object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文