简体   繁体   English

如何从managedBean设置jsf组件属性?

[英]How to set jsf component properties from managedBean?

As said in the title, i'd like to change a component property identified by an id in a jsf page from a managed bean. 如标题中所述,我想从托管bean更改jsf页面中的ID标识的组件属性。 Here is my jsf code : 这是我的jsf代码:

<p:calendar value="#{eventBean.beginDate}" id="from" pattern="dd/MM/yyyy HH:mm" required="true"/>

It's a PrimeFaces component. 这是PrimeFaces组件。 At the initialization of the page, i've got an empty field that display by clicking in a calendar. 在页面初始化时,我有一个空字段,可通过单击日历来显示。 Choosing a date fill the field with the selected value. 选择日期,将所选值填入该字段。 My question is : how to fill the field with the current date at the initialization of my jsf page ? 我的问题是:如何在jsf页面初始化时用当前日期填充字段? I dunno if there is a possibility by using PrimeFaces calendar component properties (i've try several things that didn't work) and i'd like to know if that's possible using managed bean. 我不知道是否有可能通过使用PrimeFaces日历组件属性(我已经尝试了一些不起作用的东西),并且我想知道使用托管bean是否有可能。

Thank you ! 谢谢 !

Just set the property during bean's (post)construction. 只需在bean的(后)构造过程中设置属性。

private Date beginDate;

public EventBean() {
    // Here, in constructor.
    eventDate = new Date();
}

@PostConstruct
public void init() {
    // Or here, in a @PostConstruct method. 
    // This is invoked after any dependency and managed property injection.
    eventDate = new Date();
}

Note that this approach is not specific to the calendar property. 请注意,此方法并不特定于Calendar属性。 It applies to all kinds of properties. 它适用于各种属性。

You need to update/define default values at the bean. 您需要在Bean上更新/定义默认值。 In this case you can define the value in the constructor; 在这种情况下,您可以在构造函数中定义值。 if the value depends of injected attributes you need to use a @PostConstruct method. 如果该值取决于注入的属性,则需要使用@PostConstruct方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM