Struts2 Form submission with asynchronous jQuery ajax not working

I am trying to make a simple asynchronous Struts2 form submission using jQuery ajax feature. However, whatever i try, the jquery is not getting triggered.

Normal form submission is working smoothly but i need an asynchronous call which can fetch result from server without reloading page.

I have already tried normal forms, submission with JSON plugin and everything else that i could get my hands on from youtube, internet, stackoverflow etc. Please help, I am really unable to understand what i am doing wrong.

Using jQuery 1.3.2, Struts2: 2.3.14

P.S : I am using jQuery, AJAX call with Struts2 for first Time.

			$(document).ready(function() {
				
				$("#updtTslcDL").mousedown(function(){
					
					$.ajax({
						type: 'POST',		
						data: {
							tslcText : $('#txtTslcDL').val(),
							userid : <% session.getAttribute("userid").toString(); %>,
							username : 'c'							
						},
						dataType:'json',
						url: 'updateTSLC.wht',
						success:function(response){							
							$('#msgTslcDL').html(response.out);
						},
				        error: function(msg) {
				            if (msg.statusText != "OK") {
				                alert("Alert", "Error occured while retriving data.");
				            }
				        }
				       //Finish $.ajax()
					});
					event.preventDefault();
				//Finish $('#updtTslcDL').click()
				});
			//Finish $(document).ready()
			});		
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<body>
	
		<div>
							
			<s:form id="frmUpdtTslcDL_s" theme="simple">&nbsp;&nbsp;&nbsp;&nbsp; 
			  	<s:textarea id="txtTslcDL" name="tslcText" rows="1" cols="7" value="2222:45"/>					
				<%-- <s:hidden id="userid" name="userid" value="%{#session.userid}"/>
				<s:hidden id="username" name="username" value="%{#session.username}"/>	 --%>	
				<s:submit id="updtTslcDL" name = "updtTslcDL" cssClass="button" value="Update" onclick="return false;"/>					
			</s:form>
			<!-- <%-- 
			<form id="frmUpdtTslcDL" action="updateTSLC.wht" method="post">
				<div>
					<textarea id="txtTslcDL" name="tslcText" rows="1" cols="7">123</textarea>
					<input type="hidden" id="iptuserid" name="userid" value=<% session.getAttribute("userid").toString(); %> />
					<input type="hidden" id="iptusername" name="username" value=<% session.getAttribute("username").toString(); %> />
					<input type="submit" id="iptupdtTslcDL" name = "updtTslcDL" class="button" value="Update"/>
				</div>
			</form>
			 --%> -->
			<div id="msgTslcDL"/>
		</div>
	</body>

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.action.extension" value="wht" />
    <package name="default" namespace="/" extends="struts-default, json-default">
        <action name="validateAuthorization" class="test.ValidateUserAction" method="validateUser">
            <result name="success" type="redirect">/test.jsp</result>
            <result name="error" type="redirect">/error.jsp</result>
        </action>

        <action name="updateTSLC" class="test.TestAction" method="updateTSLC">
            <result name="success" type="json">
                <param name="root">jsonResponse</param>
                <param name="location">/test.jsp</param>
            </result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

Action Class:

package test;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request = null;

    private String tslcText;
    private String userid;
    private String username;

    private Map<String, String> jsonResponse=new HashMap<String, String>();

    public String updateTSLC(){

        System.out.println(getTslcText()+getUserid()+getUsername());
        jsonResponse.put("out", "test: "+getTslcText()+", userid: "+getUserid()+", username: "+getUsername()+"_response");

        return SUCCESS;
    }

    public String getTslcText() {
        return tslcText;
    }

    public void setTslcText(String tslcText) {
        this.tslcText = tslcText;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Map<String, String> getJsonResponse() {
        return jsonResponse;
    }

    public void setJsonResponse(Map<String, String> jsonResponse) {
        this.jsonResponse = jsonResponse;
    }

    @Override
    public void setServletRequest(HttpServletRequest inRequest) {
        request = inRequest;
    }

}


Source: jquery

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.