Well, i'm using a ConversationScoped and i hope the PostConstruct is called just one time in begin of conversation, see:
@Named("disciplinaDetalheMB")
@ConversationScoped
public class DisciplinaDetalheMBImpl {
private static final long serialVersionUID = 1L;
@Inject
private Conversation conversation;
@Inject
@AnBasicBO
private BasicBO boPadrao;
@PostConstruct
public void postConstruct() {
logger.debug("Iniciando PostConstruct...");
init();
beginConversation();
}
public String salvarAndRedirecionar() {
salvar();
if (!FacesContext.getCurrentInstance().isValidationFailed()) {
return goToLastPage() + "?faces-redirect=true";
} else {
return "";
}
}
private void beginConversation() {
if (!conversation.isTransient()) {
endConversation();
}
conversation.begin();
if (conversation.isTransient()) {
throw new RuntimeException("A conversão não foi iniciada corretamente");
}
SessionContext.getInstance().setAttribute("cid", conversation.getId());
}
public BasicBO getBoPadrao() {
return boPadrao;
}
public void setBoPadrao(BasicBO boPadrao) {
this.boPadrao = boPadrao;
}
}
So, when my backing bean is created, the conversation is initialized and CID is stored in session to be used after. I have a commandButton "save" in my XHTML and when this button is called the PostConstruct is called again i don't know why:
<h:commandLink
action="#{managedBeanName.salvarAndRedirecionar()}"
styleClass="btn btn-info pull-right" value="Salvar">
<f:ajax execute="@form" />
</h:commandLink>
I noted the generated HTML is:
<a id="formManterDisciplina:j_idt44:j_idt46" href="#" onclick="mojarra.ab(this,event,'action','@form',0);return false" class="btn btn-info pull-right" name="formManterDisciplina:j_idt44:j_idt46">Salvar</a>
So, I understand the "href=#" avoid the onlick to be executed. I think this is the problem but i dont't know how to fix. Remenber: The salvarAndRedirecionar() method is never called because postConstruct is always called before.
2) I have another question: If i start a conversation and don't end, there is some problem ? Sometimes i don't want to end conversation manually because i just have ONE PAGE, i just start.