Struts 没有调用我的 servlet。我做错了什么?
好吧,基本上我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的应用程序无法正常工作,因为您已经创建了 Struts 1 Action 类,但您尝试使用 Struts 2 配置 XML 对其进行配置。
Struts 1 和 Struts 2 有很大不同。
Struts 1 配置称为
struts-config.xml
:http:// /struts.apache.org/dtds/struts-config_1_3.dtdStruts 2 配置称为
struts.xml
: http://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.dtdStruts 2 configuration is called
struts.xml
: http://struts.apache.org/dtds/struts-2.0.dtdYou 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.只需检查网络服务器日志并找到正确的错误..即特定于该问题的任何异常。这将帮助您缩小问题范围。
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.