开发者

strutrs2 and ajax(Displaying dynamic value on jsp)

开发者 https://www.devze.com 2023-02-08 14:15 出处:网络
Im pretty new to struts2 and Ajax ,Actually i have a drop down menu in JSP lets say first.jsp, When user select a choice from dropdown menu,I am calling a function of Action class lets say Method1.In

Im pretty new to struts2 and Ajax ,Actually i have a drop down menu in JSP lets say first.jsp, When user select a choice from dropdown menu,I am calling a function of Action class lets say Method1.In this method i am fetching some value from DB(lets say:a,b,c) and one value from java memory lets say d.Then I am forwarding to second.jsp and display all the parameters(a,b,c and d) in tabular format.

Now problem is that the parameter d is dynamic ,this is updating by some other application and if its change then I have to show it on JSP wihout any action.

One solution is I use in second.jsp , so after interval of 10 second again Mehod1 will call and it will fetch value(a,b,c) from db and updated value of d from java memory. and disply it to second.jsp.But in this case i am unnecessary retrieving value from db while my purpose is just to get value d from memory.This is working but this is causing my application to slower.

Can any body suggetst some other solution? or can i do it using ajax and how?

Any other advice? any help is appre开发者_开发知识库ciated.try to be more clear, i'm in lack of ideas in this problem, even it sounds like a classic :I have spend hours trying to play around with this but have got nowhere


Okay... What you're asking is a little fuzzy so let me rephrase:

  • You have a user (USER1) who opens a web page and sees some data.

  • You have a second user (USER2) (who may be an application) who is able set a value from time to time.

  • When USER2 updates that value you want USER1 to see it change in their open browser window?

If this is the case you need to understand basic ajax. For that get these demo applications working:

This example uses dojo and perhaps the S2 ajax tag lib I don't remember I prefer not to use ajax tags (as they are deprecated and prefer jquery for ajax):

http://struts.apache.org/2.x/docs/struts-2-spring-2-jpa-ajax.html

This example here shows a very similar application but using jquery, no tag library, upgraded to Spring 3, it still needs polish:

http://www.kenmcwilliams.com/Downloads/

Now that you know how to get data via ajax, look at the request with firebug. You'll see that the request is just like a typical function call, the browser keeps waiting for the data to come back.

What you do is simply not return from the action until new data is provided. This is called long polling see: http://en.wikipedia.org/wiki/Comet_%28programming%29#Ajax_with_long_polling

If you have not written a simple chat program, using just terminal windows I recommend you do so. Two windows per client (client-send, client-receive windows) and you'll need a server program. I remember hacking one together in a few hours using _Thinking In Java 2nd Edition (Later books took out the networking section if I remember correctly). Anyways between understanding client server interaction and long polling will let you get things working. It would be fun to extend the simple terminal based chat application to a S2 ajax chat application. Would make an awesome tutorial! PS: This is just an application of the producer/consumer problem (If you understand that then I guess you don't need to do the fun exercise).

The interfaces would look very pretty if the server was managed by spring. I know there must be nice servers already written but I am not familiar with any, but would love to hear of one.

0

精彩评论

暂无评论...
验证码 换一张
取 消