Java – invoking an action using an Ajax URL in struts 2
•
Java
I'm trying to connect to my action class using the URL in Ajax But it didn't enter my action class and didn't even use $("#selectedcountry") Val() displays the selected value
function getstates(){ alert($("#selectedCountry").val()); $.ajax({ type : "GET",url : "/ThirdTask/selectstate.action",dataType : 'text',data : "name="+$("#selectedCountry").val(),success : function(){ $('statesdivid').html(); },error : alert("No values found..!!") }); }
My JSP code is as follows:
<s:select name="selectedCountry" list="{'india','china'}" onclick="getstates();"/></div> <div id="statesdivid"> <s:if test="%{#request.selectedstatenames != null}"> <s:select list="#request.selectedstatenames" name="selectedState"> </s:select> </s:if> </div>
My struts xml:
<action name="selectstate.action" class="com.thirdtask.actions.SelectAction" method="selectstate"> <result name="success">selecttag.jsp</result> </action>
Solution
To map an operation to a method, you should perform a similar operation
<action name="selectstate" class="com.thirdtask.actions.SelectAction" method="selectstate"> <result>/selecttag.jsp</result> </action>
The action name should have no action extension. By default, the result name is "success", and the path of JSP should be absolute
Call Ajax
$.ajax({ type : "GET",url : "<s:url action='selectstate'/>",dataType : 'text/javascript',data : {'name' : $("#selectedCountry").text()},success : function(result){ if (result != null && result.length > 0){ $("statesdivid").html(result); } },error : function(xhr,errmsg) {alert("No values found..!!");} });
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码