File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes how to use struts 2 to redirect jQuery Ajax request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to use struts 2 to redirect jQuery Ajax request" Watch "how to use struts 2 to redirect jQuery Ajax request" New topic
Author

how to use struts 2 to redirect jQuery Ajax request

will zhang
Ranch Hand

Joined: Sep 11, 2008
Posts: 46
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



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,


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!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The answer given to you on struts-user is correct: you're making the request and receiving the response "sideband".

You need to determine how to handle the Ajax request results in JavaScript. You could send back data indicating what to do and use that in the JavaScript, but that's just how Ajax requests work.

You *might* be able to play some games with the response header, but I've never done anything like that--you'd have to search the web for that.
will zhang
Ranch Hand

Joined: Sep 11, 2008
Posts: 46
David Newton wrote:The answer given to you on struts-user is correct: you're making the request and receiving the response "sideband".

You need to determine how to handle the Ajax request results in JavaScript. You could send back data indicating what to do and use that in the JavaScript, but that's just how Ajax requests work.

You *might* be able to play some games with the response header, but I've never done anything like that--you'd have to search the web for that.


Thank you. Actually I returned data.redirect from JavaBeans to JavaScript to determine if redirect is necessary.

Also, the data was transfered between Javascript and JavaBeans(Action) by JSON (in this case {username:unameval, password:pwordval}) instead of passing data by OGNL. I wonder if this is the right way to do it? Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's no "right" way to do it, it just depends on how you prefer to do things.
will zhang
Ranch Hand

Joined: Sep 11, 2008
Posts: 46
David Newton wrote:There's no "right" way to do it, it just depends on how you prefer to do things.


Thank you, David.

So, in this case, there is no way to put the control of determining the result to Struts instead of Javascript?
Did you have similar scenario and what did you do? Is there any similar example? Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

As far as *I* know there isn't, although the answer on struts-user about sending back the URL mirrors what I (tried, anyway) to say above.

Ajax request responses don't result in the browser doing things (with the caveat that I don't know what'll happen if you play games with the response header--it might be worth trying and reporting back). The whole point of Ajax is for requests to happen without the entire page updating. If you want the browser to do something based on the response then it needs to be handled using JavaScript.

(Unless the headers thing works. Although when I think about it it seems like if you got a redirect back if anything the Ajax request would be made again.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use struts 2 to redirect jQuery Ajax request
 
Similar Threads
how to display invalid message on the same jsp page
Help: Getting Error Message "Not able to load configuration for specified action"
Can't get RequestDispatcher forward() method or response.sendRedirect() to work
struts2: howto correctly deligate actions on the struts.xml's include files
Struts2-json plugin not serializing action class to json