strutrs2和ajax(在jsp上显示动态值)

发布于 2024-10-15 17:48:58 字数 573 浏览 3 评论 0原文

我对struts2和Ajax还很陌生,实际上我在JSP中有一个下拉菜单,比如说first.jsp,当用户从下拉菜单中选择一个选项时,我正在调用Action类的一个函数,比如说Method1。在这个方法中我正在获取DB 中的某个值(比如:a、b、c)和 java 内存中的一个值比如 d。然后我转发到 secondary.jsp 并以表格格式显示所有参数(a、b、c 和 d)。

现在的问题是参数 d 是动态的,这是由其他一些应用程序更新的,如果它发生变化,那么我必须在 JSP 上显示它,而不需要任何操作。

一种解决方案是我在 secondary.jsp 中使用,因此在 10 秒的间隔后,Mehod1 将再次调用,它将从 db 获取值(a,b,c)并从 java 内存中获取更新的 d 值。并将其显示到 secondary.jsp。但是在这种情况下,我没有必要从数据库检索值,而我的目的只是从内存中获取值 d。这是有效的,但这会导致我的应用程序变慢。

有人可以提出其他解决方案吗?或者我可以使用ajax 来做到这一点以及如何做?

还有其他建议吗?感谢任何帮助。尽量说得更清楚,我在这个问题上缺乏想法,即使它听起来像一个经典:我花了几个小时尝试解决这个问题,但一无所获

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 appreciated.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-10-22 17:48:58

好吧...您的问题有点模糊,所以让我重新表述一下:

  • 您有一个用户 (USER1),他打开网页并看到一些数据。

  • 您有第二个用户 (USER2)(可能是应用程序),他可以不时设置值。

  • 当 USER2 更新该值时,您希望 USER1 在其打开的浏览器窗口中看到它发生变化?

如果是这种情况,您需要了解基本的 ajax。为了让这些演示应用程序正常工作:

这个示例使用了 dojo,也许还使用了 S2 ajax 标签库,我不记得了,我不喜欢使用 ajax 标签(因为它们已被弃用,并且更喜欢 jquery for ajax):

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

这个例子展示了一个非常相似的应用程序,但是使用jquery,没有标签库,升级到Spring 3,它仍然需要改进:

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

现在你知道了如何通过ajax获取数据,用firebug查看请求。您会看到该请求就像典型的函数调用一样,浏览器一直等待数据返回。

您所做的只是在提供新数据之前不要从操作中返回。这称为长轮询,请参阅:http://en .wikipedia.org/wiki/Comet_%28programming%29#Ajax_with_long_polling

如果您还没有编写过仅使用终端窗口的简单聊天程序,我建议您这样做。每个客户端有两个窗口(客户端发送窗口、客户端接收窗口),并且您需要一个服务器程序。我记得使用 _Thinking In Java 2nd Edition 在几个小时内将其组合在一起(如果我没记错的话,后来的书籍删除了网络部分)。无论如何,了解客户端服务器交互和长轮询将使您的工作顺利进行。将基于简单终端的聊天应用程序扩展到 S2 ajax 聊天应用程序会很有趣。会成为一个很棒的教程! PS:这只是生产者/消费者问题的一个应用(如果您理解这一点,那么我想您不需要做有趣的练习)。

如果服务器由 spring 管理,界面看起来会非常漂亮。我知道一定已经编写了很好的服务器,但我对任何一个都不熟悉,但很想听听一个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文