对Struts操作的Ajax调用不会返回响应

时间:2011-10-12 21:17:58

标签: ajax jquery struts

我正在尝试调用一个动作servlet createLabel.do,但看起来动作类没有被唤起。我使用firebug进行调试,看起来这个url被调用但接收没有响应 这是javascript函数:

<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>

有人可以告诉我为什么动作类没有被唤起?提前谢谢。

2 个答案:

答案 0 :(得分: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

我不知道为什么你期待这个工作。如果您正在创建一个“动作servlet”来处理正常的Struts 1请求,那么您做错了。除了最不寻常的情况之外,Struts请求由Action(你正确地子类化)处理。

action servlet捕获针对Struts的请求,并使用相应的Struts请求处理器来查找并调用请求的操作。 (连同其他相关的家务杂事。)

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

答案 1 :(得分: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;
            }

//祝你好运..