JSF 中的 HIt 计数器
我想向我的 JSF Web 应用程序添加一个点击计数器。就像在 PHP 中一样,我们可以使用外部文本文件来存储计数和增量,并将其写入每个请求。如何在JSF中使用?
I want to add a hit counter to my JSF web app. Like in PHP we can use an external text file to store the count and increment and write it on every request. How to use in JSF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议为此使用
Filter
。相应地实现javax.servlet.Filter
并将其映射到web.xml
中感兴趣的url-pattern
上。例如(半伪):
Java IO 教程 可能是帮助如何读取和写入文件内容。
为此,您不一定需要 JSF。 JSF 只是构建在 Servlet API 之上,您可以使用过滤器。否则,您必须创建一个 bean,并且必须记住确保每个视图在每个请求上以某种方式调用该 bean。
I would suggest to just use a
Filter
for this. Implementjavax.servlet.Filter
accordingly and map this inweb.xml
on anurl-pattern
of interest.E.g. (semi pseudo):
The Java IO tutorial may be of help how to read and write file contents.
You don't necessarily need JSF for this. JSF is just built on top of Servlet API and you can use Filters as good with it. Otherwise you have to create a bean and you have to remember to ensure that every view calls this bean somehow on every request.
你可以这样做,但我能想到的唯一情况就是做作业。不管怎样,只要有一个支持 Bean 来读取/递增/写入文件即可。
查看 Apache Commons IO FileUtils一些好的辅助方法。不过,如果这是家庭作业,你自己做作业无疑会得到更好的分数;-)
You could do this, but the only situation that I can think for it would be for homework. Anyway, just have a Backing Bean that reads/increments/writes the file.
Look at Apache Commons IO FileUtils for some good helper methods. Although if this is homework you will no doubt get better marks for rolling your own ;-)