Java 在 Struts 2 中使用 Ajax URL 调用操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20606540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling an action using Ajax URL in Struts 2
提问by kumarc
I am trying to connect to my action class by using URL as below in Ajax. But its not going into my action class and even it is not showing the selected value by using $("#selectedCountry").val()
.
我正在尝试通过在 Ajax 中使用如下 URL 连接到我的操作类。但它不会进入我的操作类,甚至它也没有通过使用$("#selectedCountry").val()
.
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 as follows:
我的JSP代码如下:
<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
:
我的struts.xml
:
<action name="selectstate.action" class="com.thirdtask.actions.SelectAction" method="selectstate">
<result name="success">selecttag.jsp</result>
</action>
采纳答案by Roman C
To map an action to the method you should do something like
要将操作映射到方法,您应该执行以下操作
<action name="selectstate" class="com.thirdtask.actions.SelectAction" method="selectstate">
<result>/selecttag.jsp</result>
</action>
action name should be without action extension and result by default is named "success", the path to JSP should be absolute here.
动作名称应该没有动作扩展名,结果默认命名为“成功”,JSP的路径在这里应该是绝对的。
Calling ajax
调用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..!!");}
});