Struts 没有调用我的 servlet。我做错了什么?

发布于 2024-11-28 16:57:21 字数 2799 浏览 0 评论 0原文

好吧,基本上我的 struts servlet 无法正常工作。如果下面出现任何问题,请告诉我:

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
    <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" >
                      <result name="success">/example/cart.jsp</result>
                    </action>        

                </package>
     </struts>

listProducts.jsp 链接激活struts和servlet

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

Well, basically my struts servlet isn't working right. Please tell me if anything appears wrong below:

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
    <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" >
                      <result name="success">/example/cart.jsp</result>
                    </action>        

                </package>
     </struts>

listProducts.jsp link that activates the struts and servlet

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

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

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

发布评论

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

评论(2

寻梦旅人 2024-12-05 16:57:21

您的应用程序无法正常工作,因为您已经创建了 Struts 1 Action 类,但您尝试使用 Struts 2 配置 XML 对其进行配置。

Struts 1 和 Struts 2 有很大不同

Struts 1 配置称为 struts-config.xmlhttp:// /struts.apache.org/dtds/struts-config_1_3.dtd

Struts 2 配置称为 struts.xmlhttp://struts.apache.org/dtds/struts-2.0.dtd

您创建Struts 1 Action 类(正如 @BalusC 指出的那样,不是 Servlet),因此您必须使用 struts-config.xml 配置它...

或者,

...您必须创建一个 Struts 2 类来匹配 struts.xml 配置。

Your application isn't working correctly because you have created a Struts 1 Action class but you are trying to configure it with a Struts 2 configuration XML.

Struts 1 and Struts 2 are very different.

Struts 1 Configuration is called struts-config.xml: http://struts.apache.org/dtds/struts-config_1_3.dtd

Struts 2 configuration is called struts.xml: http://struts.apache.org/dtds/struts-2.0.dtd

You created a Struts 1 Action class (which as @BalusC pointed out, is not a Servlet) so you must configure it using struts-config.xml...

Or,

... you have to create a Struts 2 class to match the struts.xml configuration.

一杯敬自由 2024-12-05 16:57:21

只需检查网络服务器日志并找到正确的错误..即特定于该问题的任何异常。这将帮助您缩小问题范围。

Just check the webserver logs and find the proper error.. i.e., any exceptions specific to the problem. which will help you narrow down the problem.

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