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
my struts.xml part
<action name="login" class="com.abc.csm.acti开发者_JS百科ons.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
$(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. What is the solution here? Thanks
Updated this works, Also can you check the way I am attaching sessiontoken
is it correct?
$('#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
you can disable the default behaviour of form with javascript and then after some processing submit the form using jquery
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
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();
精彩评论