开发者

How should I go about reading the data from DB inside the managed beans?

I need to read/write data of a post(in a forum website). For reading the data of post, I need to read title of post, content Of Post & post owner. My question is how do I go about fetching the data f开发者_高级运维or these fields from DB. I am making a single function called fillNow() which shall fill in the Data read from DB. Thus making the data available for all getProperty() methods of post inside the bean. Thus all the data from DB is read at once. Is there a better way ?


The best approach is to create a method annotated with @PostConstruct inside the bean: @PostConstruct in being invoked only once when the bean is being created.

@ManagedBean
@ViewScope
public class PostBean{

  PostData post;

  @PostContruct
  public void initBean(){
    post = dbManager.getPostData(id);

  }
  public PostData getPost(){
  return post;
  }
  public void setPost(PostData post){
  this.post = post;
  }
}

PostData - should be your screen elements. add getters and setters to the method dbManager - will be your manager/service to bring the data from db

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜