简体   繁体   English

JSF页面未显示内容

[英]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. 在系统中,用户可以输入“ URL”(例如“ http://www.google.com ”),并且必须在SERVER中以及在XHTML页面中显示内容之后处理此URL。

In my XHTML i have: 在我的XHTML中,我有:

    <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: 因此,当用户键入URL并单击commandButton时,将处理以下代码:

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. 在我的方法“ processaRequisicaoOnServer”中,在返回站点内容之后,将打开URL并读取所有内容。 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. 因此,由于“ System.out ..”而在控制台中显示了URL的内容,但是h:outputText并未按我希望的那样进行更新。

I can't see your entire bean here, but here are some things that I've noticed: 我在这里看不到您的整个bean,但是这里有一些我注意到的东西:

If this is your entire form, you don't need the enctype="multipart/form-data" , so you can just remove it. 如果这是您的整个表单,则不需要enctype="multipart/form-data" ,因此只需删除它即可。

There is a strange type attribute in your first p:inputText . 您的第一个p:inputText有一个奇怪的type属性。 Didn't you mean title ? 你不是在说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: 您正在调用一个动作侦听器并传递一个ActionEvent ,它是不需要的,因此您可以更改此设置:

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: 例如,要对此进行测试,您可以将processaRequisicao更改为以下内容:

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);
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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