【问题标题】:Calling an action using Ajax URL in Struts 2在 Struts 2 中使用 Ajax URL 调用动作
【发布时间】:2013-12-16 08:42:10
【问题描述】:

我正在尝试通过在 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..!!")
    });         
}

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

我的struts.xml

<action name="selectstate.action" class="com.thirdtask.actions.SelectAction" method="selectstate">
 <result name="success">selecttag.jsp</result> 
</action>

【问题讨论】:

  • 你没有selectedCountry id的元素。
  • 你尝试过双选,是 struts 还是 jQuery?
  • 即使我尝试保留 selectedCountry ID ,但没有结果@Aleksandr M
  • 不,我没试过。我一周前刚开始学习struts。 @罗马C
  • 首先,您需要编写一个要连接的操作。之后将其映射到定义的 URL。通过在浏览器中输入来测试它。然后解释它不工作的原因,如果你有错误,你可以发布一个带有根本原因的堆栈跟踪。

标签: java jquery ajax jsp struts2


【解决方案1】:

要将操作映射到方法,您应该执行类似的操作

<action name="selectstate" class="com.thirdtask.actions.SelectAction" method="selectstate">
  <result>/selecttag.jsp</result> 
</action>

动作名称应该没有动作扩展名,结果默认命名为“成功”,这里JSP的路径应该是绝对的。

调用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..!!");}
});         

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 2014-11-09
    • 2018-08-04
    • 2012-02-15
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多