【问题标题】:Why does SpringBeanFacesELResolver cancel UIComponent binding?为什么 SpringBeanFacesELResolver 取消 UIComponent 绑定?
【发布时间】:2015-07-21 06:15:30
【问题描述】:

我有一个使用 Primefaces 的网络应用。 我将一些 facelet 组件绑定到支持托管 bean 中的 UIComponent 类型字段。像这样的:

 <p:panel id="loginPanel"
             rendered="false"
             binding="#{loginBean.loginPanel}"
             style="border: none">

并且 bean 中没有额外的注释,就像这样:

@ManagedBean
public class LoginBean {

    String name;
    String password;
    UIComponent loginPanel;
//...}

我决定使用 Spring Security 进行身份验证管理。所以我实现了

用户详细信息服务

接口,在spring security config中指定的实现被注入到我的托管bean中:

<!-- Set MemberDetailsService class as the authentication Manager -->
   <sec:authentication-manager alias="authenticationManager">
          <sec:authentication-provider user-service-ref="memberDetailsService">
                 <sec:password-encoder hash="plaintext"/>
          </sec:authentication-provider>
   </sec:authentication-manager>

   <!-- Inject authentication Manager to our LoginBean -->
   <beans:bean id="loginBean" name="loginBean" class="com.mycompany.managed.LoginBean" scope="prototype">
          <beans:property name="authenticationManager" ref="authenticationManager"/>
   </beans:bean>

我还在 web.xml 中添加了 Spring 的过滤器和侦听器——没什么特别的。 我期待通过在 LoginBean 中注释我的新字段,例如:

@ManagedProperty("#{authenticationManager}")
AuthenticationManager authenticationManager;

工作将完成。但是 AuthenticationManager 在运行时没有注入它导致 NullPointerException。我用过这个solution

并创建 faces-config.xml 仅:

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

身份验证现在工作正常。 问题是 UIComponent 绑定不再起作用我的 LoginBean 中的 UIComponent 字段在运行时为空。具有绑定但没有注入托管属性的 Bean 没有这个问题。 到底是怎么回事?我真的不明白其中的联系......

谢谢。

【问题讨论】:

  • 我怀疑那个东西是否曾经工作过,因为你从未初始化过变量。你什么时候有UIComponent loginPanel= new Panel();
  • 绝对没有。只是一个没有初始化的属性声明,一个 setter 和一个 getter 以及 xhtml 页面中的绑定。它有效,并且仍然适用于同一页面中的其他表单,但此表单由另一个托管 bean 支持。 UIComponent 不会仅在 Spring 注入的 AuthenticationManager 的 bean 中初始化。
  • 我无法解释这种行为;初始化面板并查看结果

标签: jsf primefaces spring-security uicomponents


【解决方案1】:

正如in this answer 解释的那样。

我的登录页面支持 bean 最初由 JSF 管理。 JSF 一直在初始化 UIcomponent 属性。当我添加一个新的托管属性并在 spring 安全配置中配置它的注入时,我实际上让我的 bean 由 Spring 管理。因此,先前已由 JSF 初始化的属性变为空。

作为一个 w\a,我决定将 bean 中的功能拆分为两个单独的 bean。其中一个由 Spring 管理,另一个由 JSF 管理。

【讨论】:

    猜你喜欢
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多