好吧,基本上我的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>
用于激活struts和servlet的listProducts.jsp 链接
<div id="cartLink"><a href="<s:url action="cart?productCode=XM123456"/>">Add to Cart</a></div>
答案 0 :(得分:1)
只需检查网络服务器日志并找到正确的错误,即任何特定于问题的例外情况。这将帮助您缩小问题范围。
答案 1 :(得分:1)
您的应用程序无法正常运行,因为您已创建 Struts 1 Action 类,但您尝试使用 Struts 2配置 XML对其进行配置。
Struts 1 and Struts 2 are very different
Struts 1配置称为struts-config.xml
:http://struts.apache.org/dtds/struts-config_1_3.dtd
Struts 2配置称为struts.xml
:http://struts.apache.org/dtds/struts-2.0.dtd
你创建了一个Struts 1 Action类(正如@BalusC所指出的那样,不是Servlet)所以你必须使用struts-config.xml
来配置它......
或强>
...您必须创建一个Struts 2类来匹配struts.xml
配置。