【问题标题】:What to put into applicationContext and faces-config?在 applicationContext 和 faces-config 中添加什么?
【发布时间】:2017-09-18 00:43:52
【问题描述】:

我创建了一个 JSF 应用程序,但我不确定应该在 faces-config 中添加什么?

我的一个 ManagedBeans 如下所示:

@ManagedBean(name = "ProfileBean")
@ViewScoped
public class ProfileBean implements Serializable

我的应用程序上下文

<context:annotation-config />

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

<bean id="QuestionDao" class="code.elephant.dao.QuestionDao"></bean>

<bean id="QuestionService" class="code.elephant.service.QuestionService">
    <constructor-arg ref="QuestionDao"/>
</bean>

<bean id="QuestionBean" class="controller.QuestionBean">
    <constructor-arg ref="QuestionService"/>
</bean>

<bean id="UserDao" class="code.elephant.dao.UserDao"></bean>
<bean id="UserService" class="code.elephant.service.UserService">
    <constructor-arg ref="UserDao"/>
</bean>

<bean id="LoginBean" class="controller.LoginBean">
    <constructor-arg ref="UserService"/>
</bean>

<bean id="ProfileBean" class="controller.ProfileBean">
    <constructor-arg ref="UserService" />
    <property name="_LoginBean" ref="LoginBean"></property>
</bean>

我的面孔配置

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

我看到了一些在java-faces.config 中定义&lt;managed-beans&gt; 的示例,但我已经在java 类中使用了@ManagedBean 注释。这有必要吗?我的 jsf spring 设置是否正确?我是否还应该在faces-config 中定义托管bean?

【问题讨论】:

    标签: spring jsf jsf-2


    【解决方案1】:

    您在 faces-config 中配置的 SpringBeanFacesELResolver 的目的是让您可以使用 Spring beans 而不是旧式 JSF 托管 bean 或 CDI 依赖注入。

    ProfileBean 类中删除@ManagedBean 注释。您不需要它,因为您使用的是 Spring 而不是 JSF 的旧托管 bean 机制。

    @ManagedBean 注释是 JSF 旧版本的残余;如果您使用的是较新版本的 JSF,请不要使用它。当前版本的 JSF 使用 CDI(用于依赖注入的标准 Java EE API),但您使用的是 Spring,因此您应该以 Spring 方式配置 bean(您已经在这样做了,因为您已经在 Spring 中定义了 ProfileBean XML 配置)。

    【讨论】:

    • 谢谢,postconstruct 和 cleanup 可以与 spring 4.0.1 一起使用吗?
    • @Briefkasten 是的,你的 bean 是一个普通的 Spring bean,所以像这些 Spring 特性应该像任何 Spring 应用程序一样工作。
    • 另外,我想问一下,当我的 ManagedBean 使用 @SessionScoped 时,我将在我的应用程序上下文中使用 &lt;bean scope="session" .. /&gt;。我可以为@ViewScoped 使用什么?没有&lt;bean scope="view" .. /&gt;
    • @Briefkasten 不幸的是,基于 Spring 的 JSF 视图范围似乎不是开箱即用的;见this open issue。搜索一下,您会在网上找到一些解决方案。例如this one.
    猜你喜欢
    • 2012-04-28
    • 2012-03-08
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多