Sapphire
Sapphire

Reputation: 1169

Ajax call to a Struts action doesn't return a response

I'm trying to call an action servlet createLabel.do but looks like the action class is not being evoked. I used firebug to debug and it looks like this url is called but receiving is no response.
Here's the javascript function:

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

Here's my action class:

   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");
}

}

Here's the configuration in struts-config.xml:

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

Can someone please tell me why the action class is not being evoked? Thanks in advance.

Upvotes: 1

Views: 13776

Answers (2)

HariKrishna
HariKrishna

Reputation: 59

//Here is the working ajax code, which sends request to struts action class .

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

//Good Luck..

Hari

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160171

You're defining behavior inside a method called processRequest, which Struts knows nothing about (unless it's a DispatchAction and you include a token parameter, which it isn't, and you don't).

Struts 1's default request handling method is called 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

I don't know why you're expecting this to work. If you're creating an "action servlet" to handle normal Struts 1 requests, you're doing it wrong. Struts requests are handled by Actions (which you correctly subclass) in all but the most unusual situations.

The action servlet captures requests intended for Struts and uses the appropriate Struts request processor to look up and invoke the request's action. (Along with other related housekeeping chores.)

I'd recommend checking out some Struts 1 tutorials or documentation, if you really need to work with it.

Upvotes: 1

Related Questions