how to redirect to struts action from javascript in struts 2?

前端 未结 3 1299
北海茫月
北海茫月 2021-01-19 10:44

how to redirect to struts action from java script?

if condition in the script got success then i need to invoke one action in the config xml, otherwise no action inv

相关标签:
3条回答
  • 2021-01-19 11:22

    try is:

    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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-19 11:37

    you can call action controller with parameter on 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(); 
    });
    
    0 讨论(0)
  • 2021-01-19 11:40

    try it

    window.location='youractionname'
    

    This will redirect your window into your destination

    If you want to sent your values to other page, then follow this method

    <%@ 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 modified is, changed the function calling from button to form submit. And removed the unwanted things from function

    0 讨论(0)
提交回复
热议问题