jqueryajaxjspstruts2struts-action

how to call a particular method of action of Struts2 through jquery ajax


I am new to jquery and ajax. I want to call a particular method "logout" of action 'LoginAction' in Struts2. I am getting an error

There is no Action mapped for namespace / and action name logoutLoginAction.

My ajax code is:

function signout(){
     $.ajax({   
         type: "POST",
         assync:false,
         url: "logoutLoginAction.action",
         success: function(messageResponse) {                   
         response=messageResponse;
         alert(response);
     }});        
}

Solution

  • What you are trying to achieve is called Wildcard mapping.

    In your case it should be something like:

    <action name="/*LoginAction" class="foo.bar.LoginAction" method="{1}">
        <result>{1}.jsp</result>
    </action>
    

    If you will call logoutLoginAction, logout will be used as {1} and then the logout() method will be called and the logout.jsp file will be returned.

    Alternatively, you can define an action for every method of your Java file, as described here.