2011-08-08 5 views
0

글쎄, 기본적으로 내 스트럿츠 서블릿이 제대로 작동하지 않습니다. 일이 잘못 아래에 표시되는 경우 알려주세요 :Struts가 내 서블릿을 호출하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

자바 자원 \ SRC \ 보 \ 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 링크를 스트럿과 서블릿을 작동시키는 것

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

오류 메시지가 표시됩니까? – sreeprasad

+0

요청한 리소스를 사용할 수 없습니다. – Mike

+2

그건 서블릿이 아닙니다. 그것은 struts1 액션 클래스입니다. 서블릿이 실제로 무엇인지 배우려면 서블릿 태그 wiki 페이지를 읽어보십시오 : http://stackoverflow.com/tags/servlets/info 질문 역사를 살펴보면, 기본적인 Java EE (JSP/Servlet) 개념을 많이 사용했다고 생각합니다. 잘못되었거나 혼란 스럽다. 코드를 일시 중지하고 기본 Java EE 웹 개발 자습서/책을 먼저 읽고 기본 개념을 파악하는 것이 좋습니다. 자바 EE를 단계별로 배우십시오. 큰 프로젝트에서 아직 잠수하지 마십시오. 위에서 아래로가 아니라 위에서 아래로 작업하십시오. – BalusC

답변

1

당신은 스트럿츠 1 액션 클래스 을 만든 때문에 응용 프로그램이 제대로 작동하지 않습니다하지만 당신은 스트럿츠 2 구성 XML로를 구성하려고합니다.

Struts 1 and Struts 2 are very different.

스트럿츠 1 개 구성은 struts-config.xml라고 : http://struts.apache.org/dtds/struts-config_1_3.dtd

스트럿츠 2 구성 struts.xml라고 :

http://struts.apache.org/dtds/struts-2.0.dtd 당신은 (@BalusC가 지적했듯이, 아닌 서블릿) 스트럿츠 1 액션 클래스를 생성하므로 ... struts-config.xml

또는

를 사용하여 구성해야합니다. .. struts.xml 구성과 일치하도록 Struts 2 클래스를 만들어야합니다.

+0

엄청난 양의 감각을냅니다. . . 고마워 dpb !!! – Mike

1

웹 서버 로그를 확인하고 적절한 오류, 즉 문제와 관련된 예외를 찾으십시오. 문제의 범위를 좁히는 데 도움이 될 것입니다.

관련 문제