在 url ?p=2 和托管 bean 中使用参数的最佳性能实践
我在我的项目中使用 JSF 2.0、Icefaces
和 Glassfish
,我想尽我所能优化这个应用程序,并且我想知道什么是最优化的方法发送参数。
第一个: 在网址中使用 /BackOffice/test.xhtml?id=7
和 String a=(String) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("id");
在带有 @RequestScoped
的 bean 中 并
<f:metadata>
<f:viewParam name="id" value="#{id}">
</f:viewParam>
</f:metadata>
在 jsf
或 第二个: 将支持 bean 与 @SessionScoped
一起使用
I am using JSF 2.0 and Icefaces
and Glassfish
for my project and I want to optimise this application the best I can, and I want to know what is the most optimized method to send parameter.
first 1:
using /BackOffice/test.xhtml?id=7 in url
and String a=(String) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("id");
in the bean with @RequestScoped
and
<f:metadata>
<f:viewParam name="id" value="#{id}">
</f:viewParam>
</f:metadata>
in the jsf
or
the second :
using backing bean with @SessionScoped
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
具体怎么优化呢? SessionScoped beans 将保留持久内存,如果您有大量用户,这可能会很糟糕。 RequestScope 将要求每个请求传递 id,这两种方式都不会太昂贵,但确实需要每个请求创建一个请求范围的 bean。
我通常更喜欢请求参数而不是会话参数,除非值确实需要在用户访问网站的整个过程中持续存在。
Optimized how exactly? SessionScoped beans are going to hold persistent memory which may be bad if you have a high number of users. RequestScope is going to require the id to be passed per request which isn't over expensive either way but does require a request scoped bean to be created per-request.
I generally favor Request params over session params unless the value really needs to persist for the duration of the user's time on the site.
不存在“尽我所能优化”这样的事情。优化就是做出权衡。您可以用 cpu 换内存,用内存换带宽,用带宽换 cpu 等等。
传递 Id 的方法可能是您的问题中最不重要的。忘记它吧。如果您想优化,首先问问自己是将视图状态保留在客户端还是服务器上。这将产生巨大的差异(不,这里没有“最佳”选择,这是一种权衡)。接下来的事情就是——如何处理数据库。您是根据请求加载数据还是将它们保留在视图或会话中?如果使用JPA,是否启用二级缓存?这些是你应该问的问题。没有关于请求参数的详细信息。
There is no such thing as "optimize the best I can". Optimizing is about making trade-offs. You trade cpu for memory, memory for bandwith, bandwith for cpu and so on.
The method of passing Id is probably the least important of your problems. Just forget it. If you want to optimize, first ask yourself whether you are keeping your view state on client or on server. This is going to make a huge difference (and no, there is no "optimal" choice here, it is a trade-off). The next thing will be - how to handle the database. Do you load your data per-request or do you keep them with your view or session? If you use JPA, do you enable the second-level cache? These are the questions you should be asking. Not a detail about request params.
如果你想优化,你应该从分析开始,我非常怀疑这两者之间的差异是否有任何意义。
请求参数应作为请求参数传递,否则会破坏书签、浏览器历史记录、搜索引擎索引和缓存。
If you want to optimize you should start with profiling, I highly doubt the difference between these two will be of any significance.
Request parameters should be passed as request parameters as you will otherwise break bookmarks, browser history, search engine indexing and caching.