How to redirect struts action from java script to struts 2?

how to redirect struts action from java script?

If the condition in the script succeeds, then I need to call one action in config xml, otherwise no actions invoking the control should remain on the same page? please help with this?

Java script

function displayDate() { var x=document.getElementsByName("userName") if(x = "shan") { alert("shankarasd"); document.myForm.action ="/setUpForInsertOrUpdate"; document.myForm.submit(); } } 

HTML

  <html> <body> <s:form action="HelloWorld" > <s:textfield name="userName" label="User Name" /> <s:submit onclick="displayDate()" /> </s:form> </body> </html> 

config.xml

  <struts> <package name="default" extends="struts-default"> <action name="HelloWorld" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS">/success.jsp</result> </action> <action name="setUpForInsertOrUpdate" method="setUpForInsertOrUpdate" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS1">/success1.jsp</result> </action> <action name="back" method="back" class="vaannila.HelloWorld.HelloWorld"> <result name="SUCCESS2">/success.jsp</result> </action> </package> </struts> 
+4
source share
3 answers

try

 window.location='youractionname' 

This will redirect your window to your destination.

If you want to send your values ​​to another page, follow these steps:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript"> function displayDate() { var x=document.getElementsByName("userName") if(x =="shan") { alert("Redirecting"); return true; } else{ alert("Not Redirecting"); return false; } } </script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:form action="HelloWorld" onsubmit="displayDate();"> <s:textfield name="userName" label="User Name" /> <s:submit onclick="displayDate()" /> </s:form> </body> </html> 

What I changed, changed the call function from the button to form submit. And removed unnecessary things from the function

+5
source

you can call the action controller with a parameter in the click event

 $('#loginSubmit').click(function(){ var Username= take the value for text fields var Password =take the value for text fields var url = "login"; var FormD = '<form method="post" action="' + url + '" id="frmSubmit" autocomplete="off">'; FormD += '<input type="hidden" name="Username" value="' + Username+ '" />'; FormD += '<input type="hidden" name="Password" value="' + Password+ '" />'; FormD += '</form>'; $("body").append(FormD); $("#frmSubmit").submit(); }); 
+2
source

try:

 function displayDate(){ var x=document.getElementsByName("userName") if(x = "shan") { alert("shankarasd"); document.myForm.action ="/setUpForInsertOrUpdate"; document.myForm.submit(); location.href = "nameaction.action?parameter1="+value1+"&parameter2="+value2; } } 
+1
source

Source: https://habr.com/ru/post/1398233/


All Articles