2011-10-12 66 views
1

我正在尝试调用操作servlet createLabel.do,但看起来像不在调用操作类。我用萤火虫调试,它看起来像这个网址被称为,但接收没有反应。
这里的javascript函数:对Struts操作的Ajax调用不会返回响应

<script type="text/javascript" charset="utf-8"> 
    $(function() { 
      $("#createLabel").click(function() { 
      $.ajax({ 
       type: "POST",  
       url: "/createLabel.do", 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: { lab_no: $("#labNum").val(),accNum: $("#accNum").val(), label: $("#label").val() }, 
       success: function() { 
        alert("success"); 
       } 
      }); 
      }); 
    }); 

</script> 

这里是我的动作类:

public class CreateLabelAction extends Action{ 

public ActionForward execute (ActionMapping mapping, HttpServletRequest request, HttpServletResponse response){ 


    String label = request.getParameter("label"); 

    String lab_no = request.getParameter("lab_no"); 
    String accNum = request.getParameter("accNum"); 

    response.setContentType("application/json"); 
    try { 
     DB db = new DB(DB.DATA); 
     Connection conn = db.GetConnection(); 
     String insertstmt = "update Info set label='"+label+"' where lab_no="+lab_no+" and accNum='"+accNum+"'"; 
     logger.info(insertstmt); 

     PreparedStatement pstmt = conn.prepareStatement(insertstmt); 
     pstmt.executeUpdate(); 
     pstmt.close(); 
     db.closeConn(); 

     logger.info("Label created successfully."); 


    } catch (Exception e){ 
     logger.error("Error inserting label into Info" + e); 
     request.setAttribute("error", "There was an error creating a label."); 

    } 

    logger.info("Label ="+label); 
    label = StringEscapeUtils.escapeJavaScript(label); 
      return mapping.findForward("complete"); 
} 

} 

这里是在struts-config.xml配置:

<action input="/labDi.jsp" name="LabelForm" path="/createLabel" scope="request" type="all.pageUtil.CreateLabelAction"> 
     <forward name="complete" path="/labDi.jsp" /> 
    </action> 

是否有人可以告诉我为什么行动班没有被唤起?提前致谢。

+0

什么类型的DOM元素是'#createLabel'?提交按钮,链接,标准按钮,div,...? –

+0

标准按钮: Sapphire

+0

此按钮是否在表单中?当你点击按钮时,你在FireBug中看到了什么?是否发送了AJAX请求? –

回答

1

你正在定义名为processRequest的方法中的行为,Struts对此一无所知(除非它是一个DispatchAction,并且包含一个令牌参数,事实并非如此)。

Struts 1的默认请求处理方法称为execute

的1.x:http://struts.apache.org/1.x/apidocs/org/apache/struts/action/Action.html

1.2:http://struts.apache.org/1.2.9/api/org/apache/struts/action/Action.html

1.1:http://struts.apache.org/1.1/api/org/apache/struts/action/Action.html

我不知道为什么你希望这个工作。如果你正在创建一个“action servlet”来处理正常的Struts 1请求,那么你做错了。 Struts的请求由Actions(你正确的子类)处理,除了最不寻常的情况。

动作servlet捕获针对Struts的请求,并使用适当的Struts请求处理器来查找和调用请求的动作。 (与其他相关的家务杂事一起)。

我建议查看一些Struts 1教程或文档,如果您确实需要使用它。

+0

好吧我现在改变了processRequest方法。请查看更改后的代码。它仍然没有工作。 – Sapphire

+0

@Sapphire仍然不是正确的签名。你需要包含你的动作映射配置。 –

+0

在?如上所示,我在struts-config.xml中包含了前缀名“complete”......如果这就是你的意思。 – Sapphire

0

//这里是工作的ajax代码,它向struts动作类发送请求。

function userExists(userid){ 
      var exists = false; 
       $.ajax({ 
        type: "POST", 
        url: "./searchUser.do", 
        data: "userid=" + userid, 
        success: function(response){ 
         exists = true; 
        }, 
        error: function(e){ 
         alert('Error: ' + e); 
        } 
       }); 
      return exists; 
      } 

//好运..

哈日