You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by fireapple <ta...@gmail.com> on 2009/05/18 23:30:16 UTC

how to use struts 2 to redirect jQuery Ajax request

I created a login page with jQuery, I'm sending Ajax request by .getJSON(),
Also, I want to use struts 2's controller to assign to different pages based
on the returned results of action (in this case, if username and password
doesn't match, use an ajax reminder; if match, redirect to another page, say
selectBook.jsp)

in struts.xml, I used 

[code]          <action name="Login" class="tutorial.LoginDataAction">
            <result name ="success"
type="json">/pages/selectBook.jsp</result>
            <result name="failed" type="json"/>
        </action>  
 [/code]

However, it doesn't work (means if the username and password match, it
wouldn't go to page selectBook.jsp).

With google, I know I can solve it in this way: In login.jsp, 
[code]    		function login()
		{
			var unameval = $("#username").val();
			var pwordval = $("#password").val();
			$.getJSON("Login.action", {username:unameval, password:pwordval},
getData);
		}

		function getData(json) 
		{
			var data = json;
	        if(data.redirect == 1)
	            top.location.href="/Test/pages/selectBook.jsp";//redirection
	        else
	        	$("#status").html(data.comment);;			
		}
[/code]

The problem is, in this way, the controller doesn't determine the redirect
page, which is not what struts 2 should be.

Any suggestions about this issue, instead of the method above(change
callback function of login.jsp based on json data)? Thanks!
-- 
View this message in context: http://www.nabble.com/how-to-use-struts-2-to-redirect-jQuery-Ajax-request-tp23605756p23605756.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: how to use struts 2 to redirect jQuery Ajax request

Posted by fireapple <ta...@gmail.com>.
So there is no way to put the control of determining the result to Struts
instead of Javascript?


Steven Yang wrote:
> 
> Actually you went off Struts' scope by using AJAX.because javascript is on
> the client side. by using XHR you have put the control of determining the
> result to javascript and not Struts.
> one other way you can do it is you can let server send the url along with
> other information to client and client decide based on the result to
> redirect or stay on the same page.
> but still the job of determining the result is on your javascript
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-use-struts-2-to-redirect-jQuery-Ajax-request-tp23605756p23616595.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: how to use struts 2 to redirect jQuery Ajax request

Posted by Steven Yang <ke...@gmail.com>.
Actually you went off Struts' scope by using AJAX.because javascript is on
the client side. by using XHR you have put the control of determining the
result to javascript and not Struts.
one other way you can do it is you can let server send the url along with
other information to client and client decide based on the result to
redirect or stay on the same page.
but still the job of determining the result is on your javascript

Re: how to use struts 2 to redirect jQuery Ajax request

Posted by dusty <du...@yahoo.com>.
There is no reason why Struts can't figure out the URL and give it back.  I
have not seen the JSON result used the way you have it configured. 
Typically, you see the JSON result with a root parameter.  The root is the
action property you want to be the root of your JSON object graph.

So what I would do is create an action member called targetUrl and create a
getter getTargetUrl().  Then I would set the value of this member to
whatever, targetUrl = "/pages/selectBook.jsp".  If the username is a match
then you return SUCCESS.  You don't even have to set the root since it will
return the action as the root by default.  In your data you just look for
data.targetUrl and you should see what you need.

There are other patterns: you could use another property for errorMessages
when the login is no good, or use the same string.  You can use a
collection, etc.





fireapple wrote:
> 
> I created a login page with jQuery, I'm sending Ajax request by
> .getJSON(), Also, I want to use struts 2's controller to assign to
> different pages based on the returned results of action (in this case, if
> username and password doesn't match, use an ajax reminder; if match,
> redirect to another page, say selectBook.jsp)
> 
> in struts.xml, I used 
> 
> [code]          <action name="Login" class="tutorial.LoginDataAction">
>             <result name ="success"
> type="json">/pages/selectBook.jsp</result>
>             <result name="failed" type="json"/>
>         </action>  
>  [/code]
> 
> However, it doesn't work (means if the username and password match, it
> wouldn't go to page selectBook.jsp).
> 
> With google, I know I can solve it in this way: In login.jsp, 
> [code]    		function login()
> 		{
> 			var unameval = $("#username").val();
> 			var pwordval = $("#password").val();
> 			$.getJSON("Login.action", {username:unameval, password:pwordval},
> getData);
> 		}
> 
> 		function getData(json) 
> 		{
> 			var data = json;
> 	        if(data.redirect == 1)
> 	            top.location.href="/Test/pages/selectBook.jsp";//redirection
> 	        else
> 	        	$("#status").html(data.comment);;			
> 		}
> [/code]
> 
> The problem is, in this way, the controller doesn't determine the redirect
> page, which is not what struts 2 should be.
> 
> Any suggestions about this issue, instead of the method above(change
> callback function of login.jsp based on json data)? Thanks!
> 

-- 
View this message in context: http://www.nabble.com/how-to-use-struts-2-to-redirect-jQuery-Ajax-request-tp23605756p23624519.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org