【问题标题】:Is there a way to re execute the postconstract init methode after an action?有没有办法在操作后重新执行 postconstruct init 方法?
【发布时间】:2017-03-08 02:32:10
【问题描述】:

我有一个从 DB 填充的数据表,该数据表使用具有 postconstruct annotaion 的 init 函数,问题是当我删除表中的一行时,即使该实例实际上已从 DB 中删除,表数据也不会改变,所以我是想知道我是否可以再次执行 init 函数,以便它从 DB 中获取新数据并使用 ajax 将它们放入 datTable 中。

@ManagedBean
@SessionScoped
public class Compte implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7177239517089845251L;
	
	private int id_compte;
	private int id_agence;
	private int id_client;
	private String num_compte;
	private double solde;
	private String date_creation_compte;
	public ArrayList<Compte> comptes;
	
	dao d = new dao();
	
	public Compte() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	@PostConstruct
    public void init() {
		
		ExternalContext ec =
				FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);
		if(session.getAttribute("idagence")!=null){
			comptes=d.getComptesAgc((String)session.getAttribute("login"));
		}else{
		comptes=d.getComptes((String)session.getAttribute("login"));}
    }

 <ui:composition template="./WEB-INF/template/template.xhtml">
         <ui:param name="titre" value="Liste de Comptes"/>
         <ui:define name="content">
        
            <ice:form id="form">
            <style type="text/css">
                .ui-datatable-odd {
                    background-color:lightgray !important;
                    background-position-y: -1400px; 
                }
            </style>
			<ace:dataTable id="cptTable" value="#{compte.comptes}"
				var="cpt" paginator="true" paginatorPosition="bottom" rows="10">
        ...
        </ace:dataTable>

【问题讨论】:

  • 解决方案是更新你的dataTable,而不是brings the new data from DB and put them in the datTable using ajax
  • uhhmmmm 通过调用 postConstruct 注释方法?

标签: jsf icefaces


【解决方案1】:

我建议将用于填充数据表的代码放入它自己的方法中,以便在必要时由 @PostConstruct、操作方法和/或 Ajax 方法调用。

【讨论】:

  • 是的,但是如果 OP 想要调用的代码是完整的构造后注释方法,那将是矫枉过正
  • 因此我不建议从另一个方法调用完整的@PostConstruct 方法。
  • 想解释一下为什么?有什么危险?因为我没有看到任何风险/危险/问题,然后我们会有两个相互竞争的(有意见的)答案,这不是 StackOverflow 的意义所在......意见......它是关于准确、具体(一个直截了当)的答案
  • 您应该将@PostConstruct 视为第二个构造函数。它的存在是因为当 bean 执行您的构造函数时,它无法访问任何依赖注入。处理完您的构造函数后,它将注入任何依赖项,然后执行您的 @PostConstruct 方法,以便可以访问它。出于同样的原因,从同一个类中的另一个方法调用构造函数没有意义,从另一个方法调用@PostConstruct 方法也没有太大意义。这是关于明确定义方法的目的。
猜你喜欢
  • 2021-09-29
  • 2013-05-12
  • 1970-01-01
  • 2019-09-07
  • 2018-02-25
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
相关资源
最近更新 更多