Search code examples
javajqueryajaxjspstruts

How to pass values (which is not a form values) to action in struts 1.3 by using ajax


I have the following jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>  
    <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Get the best deal on Qnatas Partner credit cards</title>

<script type="text/javascript">

 function doAjaxPost() {
                    // get the form values
                    var name = $(#name).val();

                    $.ajax({
                        type: "POST",
                        url: "/cardselector/mailAction.do", // this is my action call
                        data: "name=" + name,
                        success: function(response){
                            // we have the response
                            $('#info').html(response);
                        },
                        error: function(e){
                            alert('Error: ' + e);
                        }
                    });
                }
</script>
    </head>
    <body>
    <p>Welcome to card selector</p>
    <html:form action="/cardsList" target="cardsListForm">
        <html:text name="cardsListForm" property="message" maxlength="250"/>
        <div id="name">sample</div>
        <input type="text" id="name">myname</input>

        <input type="button" value="Say Hello"><br/>
    <div id="info" style="color: green;"></div>
    </html:form>

    </body>
    </html>

and this is my form

import java.util.List;

import org.apache.struts.action.ActionForm;

public class CardsListForm extends ActionForm{
    private static final long serialVersionUID = 1L;

    private String message;    

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }   

}

And this is my action

public class MailAction extends Action {

    private CardManagementService cardManagementService;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {   
                 // Need to get the value of name from jsp input text

        } catch (Exception e) {
            System.out.println("Error:" + e.getMessage());
            e.printStackTrace();
        }

        return mapping.findForward("success");
    }


}

Now I want to pass the value of myname from jsp tp my action class by using ajax on clicking of a button. please note this name is not an property of my form class.

Is it possible please help !


Solution

  • Load jquery and pass data to you action through a POST request on click of your button like this , hope this helps

    HTML :

     <input type="button" id="button" value="Say Hello">
    

    JS:

     $(document).ready(function(){
          $("#button").click(function (){
    
              var name = $('#name').val(); //your code has  $(#name) which is not selecting required input element!
             $.post("/cardselector/mailAction.do",{"name" : name},function(response) 
              {
                    alert("done");
                    console.log(response); //see what response you are getting        
             });
          })
    

    ; });