将Java登录重定向到jsf组件(以netbeans或eclipse实时) [英] redirect java log into a jsf component (in real time as netbeans or eclipse)

查看:104
本文介绍了将Java登录重定向到jsf组件(以netbeans或eclipse实时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个等待时间太长的java方法.我想定期反馈到jsf图形组件中,作为inputTextarea

I have a java method which is too long to wait. I want to have regular feedback into a jsf graphic component as inputTextarea

这就是我的想法:将Java控制台重定向到图形组件中,以便向管理员提供有关该方法执行的常规清晰思路".他真的需要它!

Here is what I thought : "redirect java console into a graphic component to give to the admin a regular clear idea of the execution of the method". He really need it!

但是我被阻止了.

********我的inputTextarea仅在方法结束时刷新.

********My inputTextarea is refreshed only at the end of the method.

如何实时刷新它? (无论该方法是否结束)

How can I refresh it in real time ? (wheather the method is ended or not)

推荐答案

要正确回答,该问题需要分为两个问题:

To be answered properly, the question needs to be divided in two problem :

1- 重定向jsf组件中的Java日志

2- 实时显示此jsf组件

好.如果我们同意对问题的这种细分,那么我们可以统治.

ok. If we agree on this subdivision of the problem, we can reign.

I-在jsf组件中重定向Java控制台日志

对于这个示例,我选择<h:ouputText />.

For this exemple I choose <h:ouputText />.

要重定向该组件中的Java控制台日志,您必须编写自己的 OutputStream .例子:

To redirect the java console log in the component, you have to write your own OutputStream. Exemple :

package stream;

import java.io.OutputStream;
import java.io.Serializable;

import bean.managedBean.NBT;

public class StringOutputStream extends OutputStream implements Serializable {

    private static final long serialVersionUID = 1L;

    public StringOutputStream() {

    }

    @Override
    public void write(int b)  {
        NBT.appendConsole(String.valueOf((char)b));
    }

}

NBT是处理jsf页面的托管bean.

所以我们有这样的东西:

So we have something like this :

 @SessionScoped
    @ManagedBean(name = "Nbt")
    public class NBT implements Serializable{

    private static String log = ""; // The property linked to my <h:outputText /> 
//ex : <h:outputText id="logger" value="#{Nbt.log}" />

        public String getLog() {
            return log;
        }

        public void setLog(String log) {
            NBT.log = log;
        }

    public static String getConsole() {
            return log;
        }

        public static void appendConsole(String s) {
            log += s;
        }

    }

好.我们在一起.但是它将如何工作?源代码:

OK. We are together. But how will it work ? Source code :

PrintStream pstream = new PrintStream(new StringOutputStream(), true);
System.setOut(pstream);
System.setErr(pstream);

详细信息:PrintStream(yourOutputStream,为自动刷新"时为真) 因此,当您执行以下操作时: System.out.println("AB C"); 文本不会在自然控制台中打印,但会显示在<h:outputText />.这是具体情况:

Details : PrintStream(yourOutputStream, true to "refresh automatically") So when you will do : System.out.println("AB C");, the text will not be print in the natural console but it will be display in the <h:outputText />. here is what will happen in concrete terms :

yourStringOutputString.write('A');
yourStringOutputString.write('B');
yourStringOutputString.write(' ');
yourStringOutputString.write('C');

因此,您将对所有写方法执行许多appendConsole,这将更新log值;对于要显示的文本的每个字符,将调用write方法.然后,您将在jsf组件中获得结果.

so you will do many appendConsole for all write method which will update log value; For each character of the text to display, the write method will be called. And you will have the result in your jsf component.

好.第一个问题解决了.

OK. The first problem is resolved.

II-实时刷新jsf组件

这很简单.有很多方法可以做到这一点.但是对我来说,这是最简单的.源代码:

It is simple. Many ways could do that. But for me this is the most simple. Source code :

<h:outputText id="log" value="#{Nbt.log}" styleClass="thattext" />
<p:poll id="pollid" interval="2"
    update="log" widgetVar="poll"
    async="true" immediate="true" />

您的组件将每2秒升级一次. 异步"属性将使其变为异步.忽略立即属性.

Your component will be upgraded every 2 seconds. 'async' attribute will make it asynchronous. neglect immediate attribute.

这就是我的问题的答案:如何实时重定向jsf组件中的java登录?

So this is the answer to my question which was : How to redirect java log in a jsf component in real time ?

对我有用.我在<h:outputText />中看到控制台.

It works for me. I see my console in my <h:outputText />.

谢谢!

这篇关于将Java登录重定向到jsf组件(以netbeans或eclipse实时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆