English 中文(简体)
1. 假定背书的变数(视范围而定)
原标题:Initializate a variable in a backing bean (to be used in view scope)

i have a backing bean managed by spring, and it s scope is view and i have a users page that display all users and i want to pass to the datatable the list of users variable, and i want to initialize this variable on construction of the page (and use this variable as long as i am still in the page), and i am confused about the best way to initialize the list of users, i have 3 ways on my mind:

1. 通过<>建筑/建筑>启动:

@Component("user")
@Scope("view")
public class MyBean {

private List<User> usersList;


public MyBean() {

   usersList=userService.getUsers();

}

}

通过<>前活动启动:

@Component("user")
@Scope("view")
public class MyBean {

private List<User> usersList;


public void preRender(ComponentSystemEvent event){
  if(usersList!=null)
   usersList=userService.getUsers();
}

}

最初通过@PostConstruct

@Component("user")
@Scope("view")
public class MyBean {

private List<User> usersList;

@PostConstruct
public void init() {

   usersList=userService.getUsers();

}


}

请就如何在观点范围方面实现初步化提供意见,即希望一度启动变量,并在数据表中使用完全相同的变量,只要该变量仍然在同一页。

最佳回答

只有在以下情况下,才有可能在建筑构造中启动:userservice不是注入性依赖。 页: 1

如果userservice的确是一种注入性依赖,那么@PostConstruct是唯一的正确方式,因为扶养被注入了<<> subsequently/em> Construction,因此在构造者中将成为null

<代码>preRender/code>方法在every前发出回复。 这并非只是一次。

问题回答

暂无回答




相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...