本文介绍了JSF-@PostConstruct可以使用ajax调用来阻止setter方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题很清楚这个问题.这是我的实际豆:

I think the question is clear by the title. This is my actual bean :

@ManagedBean(name="selector")
@RequestScoped
public class Selector {
    private String profilePage;

    @PostConstruct
    public void init() {
        System.out.println("I'm PostConstruct");
        if(profilePage==null || profilePage.trim().isEmpty()) {
            this.profilePage="main";
        }
    }

    public String getProfilePage() { 
        return profilePage;
    }

    public void setProfilePage(String profilePage) { 
        this.profilePage=profilePage;
        System.out.println("I'm setProfilePage");
    }
}

然后我通过使用ajax调用来更改他的值(profilePage):

And i change his value (profilePage) by using ajax call :

<h:commandButton value="Some Action">
    <f:setPropertyActionListener target="#{selector.profilePage}" value="some" />
    <f:ajax event="action" render=":profileContent"/>
</h:commandButton>

我注意到服务器上的输出从来都不是I'm PostConstruct后跟I'm setProfilePage的序列.有时I'm setProfilePage完全不存在.

I notice that my output on server is not ever a sequence of I'm PostConstruct followed by I'm setProfilePage. Sometimes I'm setProfilePage is totally absent.

我读到标有@PostConstruct批注的方法将在创建bean,注入任何资源和设置任何托管属性后调用,但在之前是实际上已进入范围.

I read that Methods marked with the @PostConstruct annotation will be invoked after the bean has been created, any resources have been injected, and any managed properties set, but before the bean is actually pushed into scope.

我想知道@PostConstruct是否可以与setter方法产生一些冲突.

I would like to know if @PostConstruct can make some conflicts with setter method.

欢呼

推荐答案

在应用请求值阶段和/或更新模型值阶段未在组件树中呈现UICommand组件时,可能会发生这种情况. IE.它或其父级之一的rendered属性此时已评估false.

That can happen when the UICommand component is not rendered in the component tree during apply request values phase and/or update model values phase. I.e. the rendered attribute of it or one of its parents has evaluated false at that point.

@PostConstruct的存在不应有任何影响.

The presence of @PostConstruct should not have any influence.

这篇关于JSF-@PostConstruct可以使用ajax调用来阻止setter方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:38