如何发送参数以用于支持bean的@PostConstruct方法? [英] How can i send a parameter to be used in the @PostConstruct method of a backing bean?
问题描述
我需要预加载一些数据,以便在页面加载时显示.初始化步骤是在@PostConstruct注释的方法上执行的,但是现在我需要使用参数才能获取数据.
I need to preload some data to be displayed when the page loads. The initialization steps are performed on a @PostConstruct-annotated method but now i need to use a parameter in order to get the data.
我要做什么:
@PostConstruct
public void init()
{
List data = getDataFromDB(parameter) /*Need to read a parameter created somewhere else*/
}
有没有办法做到这一点?
Is there a way to achieve this?
预先感谢
推荐答案
很难说出在其他地方设置的参数"的含义.我将假设其他地方"的意思是通过HTTP从浏览器发送".在这种情况下,您应该在托管bean中创建一个标准属性,并:
It's kind of hard to say what you mean by "a parameter set somewhere else". I will assume that "somewhere else" means "sent from browser by HTTP". In such case you should create a standard property in your managed bean and:
- 在JSF 2.0中,您可以使用@ManagedProperty(#{param.nameOfParameterToRead}"))对其进行注释
- 在JSF 1.2及更低版本中-在bean描述(faces-config.xml)中使用托管属性元素.
赞:
@ManagedBean
@RequestScoped
class MyManagedBean {
@ManagedProperty("#{param.id}")
public Integer id;
@PostConstruct
public void init(){
data = getDataFromDB(id)
}
// setters and getters (mandatory, even though annotation is on an attribute!!!)
}
小心:注入属性不使用JSF转换器,因此最好注入字符串并在自己的代码中进行转换.
Careful: injecting properties does not use JSF converters, so it is best to inject strings and take care of conversion in your own code.
这篇关于如何发送参数以用于支持bean的@PostConstruct方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!