如何在Spring 3.1应用程序中声明JSF管理的bean? [英] How to declare a JSF managed bean in a Spring 3.1 application?

查看:99
本文介绍了如何在Spring 3.1应用程序中声明JSF管理的bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在基于Java EE架构开发应用程序时的第一次经验,我正在使用

  • JSF 2.0
  • Spring 3.1
  • JPA 2.0

我想问您一个我有点困惑的问题.我想声明我的JSF托管Bean,但是有许多解决此约束的解决方案:

  1. 在faces-config.xml中声明被管理的bean.
  2. 使用@ManagedBean注释覆盖我的控制器.
  3. 使用@Controller注释覆盖我的控制器.

我的问题是:这三种解决方案之间有什么区别?我应该对ApplicationConfig.java或/和web.xml进行哪些更改?关于Spring,我应该在我的web.xml中放入某种侦听器吗?!

我确实试图阅读许多教程和示例,但是每次我发现自己被封锁时!

解决方案

在集成Spring和JSF时,您真的不能成为这两种技术的初学者,因为它们不能很好地协同工作.我给您的第一条也是最好的建议是,在尝试将JSF和Spring集成在一起之前,先获得一些书,并分别理解它们.

话虽这么说,JSF是一个基于组件的Web框架,重点是MVC. Spring是不依赖于Web应用程序的依赖注入和控制反转框架.

如果您不理解这三个术语,则为:

  • 基于组件的Web框架

  • 依赖注入

  • 控制权反转

那么我的建议是,您只是停止正在做的事情,然后立即开始阅读.

整合这两件事的主要问题是两个框架之间的责任有一些重叠,需要解决.作为独立框架的JSF无需单独的DI框架即可维护其自己的托管bean的范围.但是,当引入Spring时,自然会有冲突. Spring除了JSF之外还管理自己的Bean,因此要引用这些ManagedBeans并适当地将业务对象或DAO注入其中以供使用,JSF ManagedBeans必须成为Spring Controllers.

您可以使用@Controller注释声明一个JSF ManagedBean. Spring 3非常聪明,足以识别它是一个JSF托管bean,并且该bean名称将与为ManagedBean声明的名称相同.

@Controller
@Scope("session")
@ManagedBean(name="testBean")

现在可以解决此问题,下一个问题是JSF实现随附的讨厌的EL Resolver. EL Resolver基本上就是这样做的,它可以解决XHTML/JSF页面上遇到的EL表达式.但是,在引用testBean时,它将无法正确解析此名称,因为它正在使用该名称引用JSF托管Bean,并且将无法找到具有所需的所有Spring注入依赖项的Spring Controller. /p>

Spring 3通过为您提供一个自定义的EL Resolver来解决此问题,该EL Resolver可以代替JSF实现附带的自定义EL Resolver.您可以声明要在faces-config.xml

中使用

<application> 
   <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application>

现在终于:

我应该在我的web.xml中放入某种侦听器吗?!

如果您只是在集成JSF + Spring而不需要任何其他Spring控制的Servlet或不需要Spring Security集成,那么<​​strong>否,您在web.xml中不需要任何其他内容.您只需要声明FacesServlet及其上下文参数,以及您情况可能需要的任何其他第三方组件库Servlet.

This my first experience in developing applications based on a Java EE architecture and I am using

  • JSF 2.0
  • Spring 3.1
  • JPA 2.0

I want to ask you please about an issue which I am a little bit confused with. I want to declare my JSF managed bean but there's many solutions to deal with this constraint:

  1. Declare the manged bean in the faces-config.xml.
  2. Override my controller with @ManagedBean annotation.
  3. Override my controller with @Controller annotation.

My question is : what's the diffrence between these three solutions ?! and what changes should i make on my ApplicationConfig.java or/and web.xml ?! And concerning Spring , should i put some kind of listeners in my web.xml ?!

I really tried to read many tutorials and examples but each time i find myself blocked !

解决方案

When integrating Spring and JSF you really can't be a beginner in either technology, because they don't play well together. My first and best advice to you is to get a few books on JSF and Spring and really understand both separately before attempting to integrate them.

With that being said, JSF is a component based web framework with an emphasis on MVC. Spring is a Dependency Injection and Inversion of Control framework that is not exclusive to web applications.

If you don't understand these three terms are:

  • Component based web framework

  • Dependency Injection

  • Inversion of Control

Then my suggestion is that you just stop what you are doing and immediately begin reading.

The main problem with integrating these two things is that there is some overlap in responsibilities between the two frameworks that need to be addressed. JSF as a standalone framework maintains the scope of its own managed beans without the need for a seperate DI framework. When introducing Spring however then there are naturally going to be conflicts. Spring manages its own Beans apart from JSF, so to reference these ManagedBeans and have business objects or DAO's be properly injected into them for use, the JSF ManagedBeans need to become Spring Controllers.

You can declare a JSF ManagedBean with the @Controller annotation. Spring 3 is smart enough to recognize that it is a JSF managed bean and the bean name will be whatever the name is declared as for the ManagedBean.

@Controller
@Scope("session")
@ManagedBean(name="testBean")

Now that this is handled, the next problem is that pesky EL Resolver that came with your JSF implementation. The EL Resolver does basically just that, it resolves EL expressions encountered on your XHTML/JSF page. When referencing testBean however it will not be able to resolve this name correctly as it is referring to a JSF managed bean by that name, and will not be able to find the Spring Controller with all the Spring injected dependencies that you need.

Spring 3 solves this problem by providing you a custom EL Resolver to use in place of the one that comes bundled with your JSF implementation. You can declare it to be used in faces-config.xml

<application> 
   <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application>

Now finally:

should i put some kind of listeners in my web.xml ?!

If you are just integrating JSF + Spring without the need for any other Spring controlled Servlets or without the need for Spring Security integration then no you do not need anything additional in your web.xml. You would only need to declare the FacesServlet and its context params, plus any other third party component library servlets that may be necessary for your situation.

这篇关于如何在Spring 3.1应用程序中声明JSF管理的bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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