简体   繁体   English

struts2重定向到另一个页面

[英]struts2 redirect to another page

I am unable to redirect to another page using struts2 when i manually submit the form and not by form's action 当我手动提交表单而不是通过表单操作时,我无法使用struts2重定向到另一个页面

my struts.xml part 我的struts.xml部分

<action name="login" class="com.abc.csm.actions.Login">
         <result type="plainText" name="success"></result>
    </action>
<action name="home">
        <result name="success" type="redirect">/view/Welcome.jsp</result>
    </action>

on index.html i have a form with 2 fields, email and project id so i submit this form like 在index.html上,我有一个包含2个字段, emailproject id的表单,因此我像

$(function() {
    $("#loginSubmitBtn").click(submitLogin);

    function submitLogin()
    {
        if(validate())
        {
            $.post("/csm/login.action",
              { emailaddress:   document.getElementById("email_id").value,
                projectid:      document.getElementById("project_id").value },
                  function(xml)
                  {
                        if($(xml).find('isSuccess').text()=="true")
                        {
                            sessiontoken=$(xml).find('sessiontoken').text();
                            $.post("/csm/home.action", {
                                sessiontoken:    sessiontoken
                            });
                        }
                  }
            );
}
    }
});

});

In the above code i am trying to get sessiontoken from server side response, validate if login was a success and then redirect it to /csm/home.action , but i does not gets redirected, I am using fiddler to monitor, and i can see the content of Welcome.jsp (/csm/home.action is mapped to Welcome.jsp in struts.xml) in response, but no redirection happens. 在上面的代码中,我试图从服务器端响应获取sessiontoken ,验证登录是否成功,然后将其重定向到/csm/home.action ,但是我没有被重定向,我正在使用Fiddler进行监视,并且我可以请作为响应查看Welcome.jsp的内容(/csm/home.action映射到struts.xml中的Welcome.jsp),但不会发生重定向。 What is the solution here? 这里有什么解决方案? Thanks 谢谢

Updated this works, Also can you check the way I am attaching sessiontoken is it correct? 更新了此工作原理,还可以检查我附加sessiontoken方式是否正确?

  $('#submitDBtn').click(function(event){
    event.preventDefault();
    if(validate())
    {

        $.post("/csm/login.action",
          { emailaddress:   document.getElementById("email_id").value,
            projectid:      document.getElementById("project_id").value },
              function(xml)
              {
                    if($(xml).find('isSuccess').text()=="true")
                    {
                        sessiontoken=$(xml).find('sessiontoken').text();
                        /*$.post("/csm/home.action", {
                            sessiontoken:    sessiontoken
                        });*/
                        $('#launch').attr('action', '/csm/home.action?sessiontoken='+sessiontoken);
                        $('#launch').submit();
                    }
              }
        );

    }

});

try doing this 尝试这样做

upon success instead of 成功之后

$.post("/csm/home.action", {sessiontoken:sessiontoken});

try 尝试

window.location = "/csm/home.action"+sessiontoken;

abt form submit 表格提交

have a look here 在这里看看

http://jquery-howto.blogspot.com/2009/05/disable-submit-button-on-form-submit.html http://jquery-howto.blogspot.com/2009/05/disable-submit-button-on-form-submit.html

you can disable the default behaviour of form with javascript and then after some processing submit the form using jquery 您可以使用javascript禁用表单的默认行为,然后在进行一些处理后使用jquery提交表单

http://api.jquery.com/submit/ http://api.jquery.com/submit/

and about whether its a good way or not it depends but i have hear and read that one should avoid it ... 关于它是否是一个好方法,取决于它,但是我听到并读过一个人应该避免它...

EDIT about sending sessiontoken in hidden field, just before submitting the form do this 编辑有关在隐藏表单中发送sessiontoken的信息,就在提交表单之前

var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "sessiontoken");
    hiddenField.setAttribute("value", sessiontoken);
    var form = document.getElementById("yourFormId");
    form.appendChild(hiddenField);
    $('#formid').submit();

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

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