-1

First of all, I don't know if is possible or not, so I read a lot of posts before asking, how can I call Struts2 action from a JavaScript function?

Examples (Forms):

http://www.simplecodestuffs.com/example-to-call-struts2-action-from-java-script
how to call method in action class in javascript using struts2 framework?
how to redirect to struts action from java script in struts 2?
How to call a method in Struts2 Action Class method with javascript
https://coderanch.com/t/533202/framework/Calling-Action-Class-javaScript-Struts
https://coderanch.com/t/530746/framework/call-method-action-class-Struts
https://coderanch.com/t/551141/framework/call-Struts-action-javascript-function

Thanks for your help.

I put the Struts.xml file where you can see the Struts2 actions and the javascriptFunctions.js that contains de borrarPerfil() where as you can see I need to call Struts2 action

Struts.xml

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="registrar" extends="struts-default">
    <action name="registrar" class="registrar.action.Registrar"
        method="execute">
        <result name="success" type="redirect">/estructura/finRegistro.jsp
        </result>
        <result name="input" type="dispatcher">/estructura/registro.jsp</result>
    </action>
</package>

<package name="tiles" extends="struts-default">
    <result-types>
        <result-type name="tiles"
            class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="*Menu" method="{1}" class="tiles.action.MenuAction">
        <result name="inicio" type="tiles">inicio</result>
        <result name="quienes" type="tiles">quienes</result>
        <result name="servicios" type="tiles">servicios</result>
        <result name="donde" type="tiles">donde</result>
        <result name="contacto" type="tiles">contacto</result>
    </action>

    <action name="*Usuario" method="{1}" class="tiles.action.UsuarioAction">
        <result name="perfil" type="tiles">perfil</result>
        <result name="agenda" type="tiles">agenda</result>
        <result name="servicios" type="tiles">mservicios</result>
        <result name="consulta" type="tiles">consulta</result>
        <result name="ayuda" type="tiles">ayuda</result>
    </action>
</package>

<package name="contacto" extends="tiles">
    <action name="validar" class="validar.action.Validar" method="execute">
        <result name="success" type="tiles">finContacto</result>
        <result name="input" type="tiles">contacto</result>
    </action>
</package>

<package name="login" extends="tiles">
    <interceptors>
        <interceptor name="loginInterceptor" class="login.interceptor.LoginInterceptor">
        </interceptor>
        <interceptor-stack name="loginStack">
            <interceptor-ref name="loginInterceptor" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>

    <!-- login action -->
    <action name="login" class="login.action.Login">
        <result name="input" type="tiles">inicio</result>
        <result name="success" type="tiles">usuarioLayout</result>
    </action>

    <!-- logout action -->
    <action name="logout" class="login.action.Login" method="logout">
        <result name="success" type="tiles">inicio</result>
    </action>

</package>

<package name="modificar" extends="tiles">
    <action name="modificar" class="modificar.action.Modificar" method="execute">
        <result name="success" type="tiles">finModificar</result>
        <result name="input" type="tiles">menuLayout</result>
    </action>
</package>

<package name="borrar" extends="titles">
    <action name="borrar" class="borrar.action.Borrar">
        <result name="success" type="tiles">inicio"</result>    
    </action>
</package>

javascriptFunctions.js

function borrarPerfil() {
    if(confirm("Are you sure to delete this profile?")){
        alert("deleted");
       //CALL STRUTS2 ACTION borrar 

    } else {
        alert("no paso nada");
     //CALL STRUTS2 ACTION logout   
    }   
}
Community
  • 1
  • 1
StudentJavaEE
  • 33
  • 2
  • 6
  • It's not clear what you asking Struts2 reacts on http calls that has a mapping to the url. It could expose some api that you can access with any http client. – Roman C Feb 12 '17 at 18:21
  • You can get a form in js function, and then submit it to an action. What seems to be the problem? – user6332430 Feb 13 '17 at 19:40
  • Same way as you call any back-end function from JavaScript. It's not clear what specific issue you think you have. – Dave Newton Feb 14 '17 at 15:45
  • Hello, thanks for yours quickly answer but I don't need to use a form. Today when I have time will put the code to can show what I need it. – StudentJavaEE Feb 15 '17 at 05:38

1 Answers1

0

Finally, I solve how call from javascript function. It should be like this:

javascriptFunctions.js

function borrarPerfil() {
    if(confirm("Are you sure to delete this profile?")){
        alert("deleted");
        window.location = "/Barcelona_Business_World_v2/borrar";
    } else {
        alert("no paso nada");
        window.location = "/Barcelona_Business_World_v2/logout";
    }   
}

I was wrong because I used the next to call the strut2 action:

window.location = "${pageContext.request.contextPath}/logout";

I read that it's the correct way to do but I don't know why in my function fail. I will read more about contextPath.

Thanks.

StudentJavaEE
  • 33
  • 2
  • 6