2

I have the following html page

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

          <h:head>

            </h:head>

       <h:body  onload="#{login.activateAccount()}" >
        <h:form  id="formRegister">
         <p:panel  style="border:none;  width: 0px; height: 0px; margin-left:-15px ; " id="panelMesaje"> </p:panel>
        </h:form>
       </h:body>   
    </html>

When the page is loaded the following method is called and everything works fine.

  public String activateAccount() {

        try {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

            String random = request.getParameter("random");

            log.error("method called" + random);
            addActivatedInfoMessage("Your account has been activated.");

        } catch (Exception ex) {
            log.error("unexpected error", ex);
            addErrorMessage("An unexpected errors has occured.Please try again a bit later.");
        }

        return "OK";
    }

In this method a primefaces dialog will be rendered if everything is ok.

  private void addActivatedInfoMessage(String text) {
        UIComponent panel = ComponentUtils.findComponent("panelMesaje");
        Dialog dialogx=(Dialog) ComponentUtils.findComponent("infoMessageDialog");
        if (ComponentUtils.findComponent("infoMessageDialog") != null) {
        panel.getChildren().remove(dialogx);

        }
            int panelHeight=text.trim().length()/2;
            Dialog dialog = new Dialog();
            dialog.setStyleClass("clasaDialogInfo");
            dialog.setResizable(false); 
            dialog.setDynamic(true);
            dialog.setClosable(false);
            dialog.setWidgetVar("infoMessageDialog");  
            dialog.setId("infoMessageDialog");
            dialog.setModal(true);           



          log.info(" panel height este "+panelHeight);

            ScrollPanel panelScroll = new ScrollPanel();
            panelScroll.setStyle("text-align: center;border:none;width:100%;heigth:"+panelHeight+"px;font-size:20px !important");
//            HtmlPanelGroup hpg=new HtmlPanelGroup();

            HtmlOutputLabel  hit = new HtmlOutputLabel();
            hit.setValue(text);

            hit.setStyle("text-align: center;vertical-align: middle;margin-top: 0px");
            hit.setId("inputhtmlmesajeInfo");
//            hpg.getChildren().add(hit);
            panelScroll.getChildren().add(hit);
            Panel panelButon = new Panel();
            panelButon.setStyle("border:none;text-align: center;height: 37px;margin-bottom: 2px");
            HtmlCommandButton button = new HtmlCommandButton();
            button.setValue("Log in");
            button.setStyle("text-align: center;width:40%;height: 33px;margin-bottom: 2px;font-size:20px !important");



            button.setActionExpression(createMethodExpression("#{login.redirectToLogin}"));;

            panelButon.getChildren().add(button);
            dialog.getChildren().add(panelScroll);
            dialog.getChildren().add(panelButon);
            panel.getChildren().add(dialog);


        RequestContext.getCurrentInstance().execute("PF('infoMessageDialog').show()");
        RequestContext.getCurrentInstance().update(panel.getClientId());

    }  

Next when i click the command button added to that dialog it will try to redirect to login using

public void  redirectToLogin (){
        try {
            ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("login.xhtml");
        } catch (IOException ex) {
           log.error("eroare la redirect ",ex);
        }
         }

All works fine ,but body onload method is called again before redirecting.I tried to put a jsf command button directly in the page to see if that happens but didn't. The question is why when button is added dynamic the onLoad is called again before redirect and when its added directly to html (like this <h:commandButton action="login.xhtml" value="login.xhtml"/> ) wont be called? I am using following jsf version

 compile ('com.sun.faces:jsf-api:2.2.8')   { transitive = false }
compile ('com.sun.faces:jsf-impl:2.2.8')   { transitive = false }
Uta Alexandru
  • 324
  • 4
  • 11

0 Answers0