如何将JSF与自定义servlet集成? [英] How to integrate JSF with custom servlets?

查看:123
本文介绍了如何将JSF与自定义servlet集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用JavaServer Faces,它看起来非常有吸引力。但是我想在与JSF相同的Web应用程序中使用我自己的servlet。

I'm just getting started on JavaServer Faces and it looks very attractive. However I'd like to use my own servlets in the same web application as JSF.

这可能是非常明显的,但是将JSF与a集成的最佳实践是什么? 普通基于servlet的应用程序?这包括从servlet访问JSF数据(读取和写)。

This might be really obvious, but what are the best practices for integrating JSF with a "normal" servlets-based application? This would include accessing JSF data from the servlets (read and write).

推荐答案

如果你的servlet写得很好,他们应该已经内部有任何业务逻辑,而只是纯粹的请求/响应控制/预处理/后处理逻辑。业务逻辑应该已经放在类似javabean的独立域/模型类中。数据库逻辑应该已经放在独立的DAO类中。等等。你可以在JSF中重用它们。

If your servlets are well-written, they should already not have any business logic inside, but just pure request/response controlling/preprocessing/postprocessing logic. The business logic should already be placed in standalone javabean-like domain/model classes. The database logic should already be placed in standalone DAO classes. And so on. You can just reuse them all in JSF.

也就是说,知道JSF(在Servlet API上运行时)可能会很好 - 常见情况)管理请求范围bean作为 HttpServletRequest 的属性,会话范围bean作为 HttpSession 的属性,应用程序作用域为 ServletContext 的属性。也可以通过 ExternalContext#getRequestMap() #getSessionMap()<来访问所有这些请求,会话和应用程序属性。 / code>和 #getApplicationMap()。你现在应该意识到你可以从servlet内部以通常的方式访问它们。

That said, it may be good to know that JSF (when running on top of Servlet API --the common case) manages request scoped beans as attributes of HttpServletRequest, the session scoped beans as attributes of the HttpSession, the application scoped beans as attributes of ServletContext. It may also be good to know that all of those request, session and application attributes are accessible by ExternalContext#getRequestMap(), #getSessionMap() and #getApplicationMap(). You should now realize that you can just access them the usual way from inside a servlet.

在任何情况下,当技术需要时访问 Servlet 过滤器中的 FacesContext ,然后立即停止对其进行编码,并根据上述事实重新考虑您的方法。不应该在新的托管bean中做得更好吗?或者可能是 PhaseListener

In any case, when there is technical need to access the FacesContext inside a Servlet or a Filter, then immediately stop coding it and rethink your approach based on the above facts. Shouldn't it better be done in a new managed bean? Or maybe a PhaseListener?

这篇关于如何将JSF与自定义servlet集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆