首先,我不知道是否可能,所以我在询问之前阅读了很多帖子,如何从JavaScript函数调用Struts2动作?
示例(表单):
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
感谢您的帮助。
我把Struts.xml文件放在哪里可以看到Struts2动作和包含de borrarPerfil()的javascriptFunctions.js,你可以看到我需要调用Struts2动作
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
}
}
答案 0 :(得分:0)
最后,我解决了如何从javascript函数调用。它应该是这样的:
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";
}
}
我错了,因为我使用了下一个来调用strut2动作:
window.location = "${pageContext.request.contextPath}/logout";
我读到这是正确的方法,但我不知道为什么我的功能会失败。我将阅读有关contextPath的更多信息。
感谢。