简体   繁体   English

如何从 JavaScript 函数调用 Struts2 操作?

[英]How to call a Struts2 action from a JavaScript function?

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?首先,我不知道是否可能,所以我在问之前阅读了很多帖子,如何从 JavaScript 函数调用 Struts2 操作?

Examples (Forms):示例(表格):

http://www.simplecodestuffs.com/example-to-call-struts2-action-from-java-script http://www.simplecodestuffs.com/example-to-call-struts2-action-from-java-script
how to call method in action class in javascript using struts2 framework? 如何使用struts2框架在javascript中调用动作类中的方法?
how to redirect to struts action from java script in struts 2? 如何从struts 2中的java脚本重定向到struts动作?
How to call a method in Struts2 Action Class method with javascript 如何使用javascript调用Struts2 Action Class方法中的方法
https://coderanch.com/t/533202/framework/Calling-Action-Class-javaScript-Struts 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/530746/framework/call-method-action-class-Struts
https://coderanch.com/t/551141/framework/call-Struts-action-javascript-function 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 文件放在你可以看到 Struts2 动作的地方,以及包含 de borrarPerfil() 的 javascriptFunctions.js,你可以看到我需要调用 Struts2 动作

Struts.xml 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 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   
    }   
}

Finally, I solve how call from javascript function.最后,我解决了如何从 javascript 函数调用。 It should be like this:应该是这样的:

javascriptFunctions.js 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:我错了,因为我用 next 来调用 strut2 动作:

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.我将阅读有关 contextPath 的更多信息。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM