在Struts 1中通过Ajax调用方法

时间:2014-09-09 10:51:18

标签: ajax struts-1

我有来自调度的动作类。我想使用ajax从那里调用一个方法。当我点击显示代码的按钮时,我想去LeaveAction的添加方法。 错误是" Request [/ InvitationAction]不包含名为method的处理程序参数。" 提前谢谢。

public class InvitationAction extends DispatchAction{

public ActionForward add(ActionMapping mapping, ActionForm form, 
        HttpServletRequest request, HttpServletResponse response){
    InvitationForm invForm = (InvitationForm)form;
    Person newPerson = new Person();
    newPerson.setFirstName(invForm.getFirstName());
    newPerson.setLastName(invForm.getLastName());
    invForm.getPersonMap().put(invForm.getKey(), newPerson);
    invForm.reset(mapping, request);
    return mapping.findForward("success");
}

public ActionForward delete(ActionMapping mapping, ActionForm form, 
        HttpServletRequest request, HttpServletResponse response){
    InvitationForm invForm = (InvitationForm)form;
    invForm.getPersonMap().remove(invForm.getKey());
    return mapping.findForward("success");
}

public ActionForward edit(ActionMapping mapping, ActionForm form, 
        HttpServletRequest request, HttpServletResponse response){
    InvitationForm invForm = (InvitationForm)form;
    Person person = invForm.getPersonMap().get(invForm.getKey());
    invForm.setFirstName(person.getFirstName());
    invForm.setLastName(person.getLastName());
    return mapping.findForward("success");
}

}

.jsp side

function doAjaxPost() {  

  $.ajax({  
    type: "POST",  
    url: "/AjaxWithStruts1/InvitationAction.do?action=edit",
    success: function(response){  
      // we have the response  
      $('#info').html(response);  
    },  
    error: function(e){  
      alert('Error: ' + e);  

    }  
  });  
}  

<input type="button" value="add" onclick="doAjaxPost()"><br/>

struts-config.xml side

<action-mappings>

    <action name="invitationForm" path="/invitation" type="com.raistudies.actions.InvitationAction" 
    scope="session" input="/index.jsp" parameter="action">
        <forward name="success" path="/index.jsp" redirect="true"/>
    </action>

</action-mappings>

1 个答案:

答案 0 :(得分:0)

对于Dispatch Action,在ajax调用中使用URL,您需要指定映射的路径( /invitation.do )而不是操作( /InvitationAction.do )。

function doAjaxPost() {  
  $.ajax({  
   type: "POST",  
   url: "/AjaxWithStruts1/invitation.do?action=edit",
   success: function(response){  
   // we have the response  
   $('#info').html(response);  
 },  
  error: function(e){  
   alert('Error: ' + e);  

 }  
 });  
} 

在映射中将验证视为FALSE     

<action name="invitationForm" path="/invitation" type="com.raistudies.actions.InvitationAction" 
   scope="session" input="/index.jsp" parameter="action" validation="false">
    <forward name="success" path="/index.jsp" redirect="true"/>
</action>