JSF2 可以在没有框架的情况下使用 bean 作为基于请求的控制器吗?
我正在为一个小组项目使用 Java Server Faces 开发 Web 应用程序。我们大多数人都有在 PHP 中使用基于请求的框架的经验,但我们在进入 JSF 开发的基于事件(?)的思维方式时遇到了问题。
在 PHP 中,您通常会有一个前端控制器(如 Faces Servlet),它将请求分派到其他控制器(与 Faces Servlet 不同)。关键是您可以完全控制来自控制器的请求。这种控制似乎是 JSF 中所缺乏的,因为 bean 基本上只是业务逻辑,在访问 URL 时不一定运行(即通过输入 .xhtml 文件的 URL,它将在不涉及任何 bean 的情况下加载) 。
我的问题是,是否有一种方法可以使所有请求都由托管 bean 处理,而不使用 Spring 或 Struts 等框架(据我所知,它们是基于请求的)?这将使 bean 更像控制器,进而使我们能够以更熟悉的方式管理身份验证/授权、重定向等内容。
我希望这是有道理的..无论如何,如果有人可以帮助我们,我和我的团队将非常感激!
此致, 埃里克
I'm working on the development for a web application using Java Server Faces for a group project. The majority of us have experience using request-based frameworks in PHP and we are having problems getting into the event-based (?) mindset of JSF-development.
In PHP you would generally have a front controller (like the Faces Servlet) that dispatches requests to other controllers (unlike the Faces Servlet). The point is that you have full control of the requests from you controllers. This control is something that seems to be lacking in JSF, since the beans are basically just business logic that is not necessarily run when accessing URLs (i.e. by entering the URL for a .xhtml file it will be loaded without the involvement of any beans).
My question is if there is a way to make all requests be handled by managed beans without using a framework such as Spring or Struts (which from I understand are request-based)? This would make the beans more like controllers and, in turn, enable us to manage such things as authentication/authorization, redirects, etc. in a more familiar way.
I hope this makes sense.. Anyhow, me and my group would be very grateful if someone could help us out!
Best regards,
Erik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
@ManagedProperty
将请求参数设置为托管 bean 属性。使用 http://example.com/page.jsf?foo=123 这将将
123
设置为foo
。您可以使用
@PostConstruct
指示在构造 bean 并设置那些托管属性后必须执行的方法。如果您想从(XHTML 页面)视图端内部控制它,那么您可以使用
。这基本上会在视图构建期间执行
bean.setFoo(request.getParameter("foo"))
。在这种情况下,您不需要这些注释,但您将无法使用@PostConstruct
功能。另请参阅:
You can use
@ManagedProperty
to set request parameters as a managed bean property.With http://example.com/page.jsf?foo=123 this will set
123
asfoo
.You can use
@PostConstruct
to indicate a method which has to be executed after the bean is been construct and those managed properties have been set.If you rather want to control this from inside the view side on (the XHTML page), then you can use
<f:metadata>
.This will basically do
bean.setFoo(request.getParameter("foo"))
during view's construction. In that case, you don't need those annotations, but you won't be able to use the@PostConstruct
functionality.See also: