简体   繁体   中英

JSF page is not showing the content

In system user can type a "URL", like " http://www.google.com " and this URL must be processed in SERVER and after the content is showed in XHTML page.

In my XHTML i have:

    <h:form id="formNavegador" enctype="multipart/form-data">
        <p:inputText value="#{navegadorMB.url}" required="true"
            requiredMessage="A url é obrigatória"
            type="Digite a url para navegar. Ex: http://www.google.com.br" />

        <h:outputText value="#{navegadorMB.htmlContent}" escape="false"
            id="htmlContent" />

        <p:commandButton id="commandButtonProcessar" value="Ir"
            update=":formNavegador:htmlContent" icon="ui-icon-play"
            actionListener="#{navegadorMB.processaRequisicao}" />


    </h:form>

So, when user type the URL and click in commandButton, the code bellow is processed:

public void processaRequisicao(ActionEvent event){
        if (url.isEmpty()){
            addErrorMessage("Você precisa digitar um endereço");
            FacesContext.getCurrentInstance().validationFailed();
        }else{
            htmlContent = boPadrao.processaRequisicaoOnServer(url);
            System.out.println(htmlContent);
        }
    }

In my method "processaRequisicaoOnServer" the URL is opened and all content is read, after the content of site is returned. See:

public String processaRequisicaoOnServer(String url) {
        URL urlObj;
        try {
            urlObj = new URL(url.trim().toLowerCase());
            BufferedReader conteudo = new BufferedReader(new InputStreamReader(urlObj.openStream()));
            String linha = "";
            StringBuffer sb = new StringBuffer();
            while((linha = conteudo.readLine()) != null)
            {
                sb.append(linha);

            } 

            return sb.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new BOException(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new BOException(e.getMessage());
        }
    }

So, the content of URL is showed in console because of "System.out.." but the h:outputText is not updated as i hope.

I can't see your entire bean here, but here are some things that I've noticed:

If this is your entire form, you don't need the enctype="multipart/form-data" , so you can just remove it.

There is a strange type attribute in your first p:inputText . Didn't you mean title ?

<p:inputText type="Digite a url para navegar. Ex: http://www.google.com.br" />

Also, you don't need to specify the parent of a component when the component is already a child of this parent, so you could change this:

<p:commandButton id="commandButtonProcessar" value="Ir" update=":formNavegador:htmlContent" icon="ui-icon-play" actionListener="#{navegadorMB.processaRequisicao}" />

to this:

<p:commandButton id="commandButtonProcessar" value="Ir" update="htmlContent" icon="ui-icon-play" actionListener="#{navegadorMB.processaRequisicao}" />

And you are calling an action listener and passing an ActionEvent , which you don't need to, so you could change this:

public void processaRequisicao(ActionEvent event) { ...

to this:

public void processaRequisicao() { ...

Furthermore, in order to test it all, you could go in steps, creating a mock method first and checking if things are working properly, and then adding your business stuff.

For instance, to test this you could change your processaRequisicao to something like:

public void processaRequisicaoMock() { 
    htmlContent = "Funcionou!";
}

And then call it, and check if the view is working properly. If it is, you can go on, adding the business layer and all.

I hope it helps.

Instead of

 <p:commandButton id="commandButtonProcessar" value="Ir"
        update=":formNavegador:htmlContent" icon="ui-icon-play"
        actionListener="#{navegadorMB.processaRequisicao}" />

Try

 <p:commandButton id="commandButtonProcessar" value="Ir"
        update="htmlContent" icon="ui-icon-play"
        actionListener="#{navegadorMB.processaRequisicao}" ajax="false"/>

Also, change your method to

public void processaRequisicao(){
    if (url.isEmpty()){
        addErrorMessage("Você precisa digitar um endereço");
        FacesContext.getCurrentInstance().validationFailed();
    }else{
        htmlContent = boPadrao.processaRequisicaoOnServer(url);
        System.out.println(htmlContent);
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM