更改字符串时更新<h:inputtextarea>内容</h:inputtextarea>

时间:2012-10-14 03:52:59

标签: java ajax jsf richfaces

好吧,这是一个微不足道的问题,但在这里。我想在我的视图中使用<h:inputTextArea value="bean.status" disabled="true"/>来显示在数据库上进行的各种插入/更新的进度(使用richfaces)。 Bean是一个带有status属性的辅助bean,以及一种添加状态消息的方法:

public void addMessage(String message) {
    status = status.concat(message + "\n");
}

有一个&#34;迁移&#34;启动该过程的按钮。我的问题是文本区域仅在迁移器方法结束时更新。我需要的是每次status更新时,输入文本区域也会更新。所以......

1 - 怎么做?

2 - 有更好的方法来实现这个吗?


根据RichFaces组件参考资料,它需要:

  • 设置推送

  • 使用服务器端或客户端方法

  • 实施消息制作者

  • 处理推送消息

所以我做了:

  • 安装运行时依赖项(Atmosphere): - 使用Maven添加了氛围 - 运行时-1.0.0.beta4依赖

  • 注册Push Servlet - Servlet 3.0不需要,但在web.xml中添加了手动配置:

        推送Servlet     org.richfaces.webapp.PushServlet     1     真正

  • 选择使用推送CDI事件机制的服务器端方法来触发消息。将我的Status.java更新为消息生成器:

    import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.enterprise.event.Event; import javax.inject.Inject; import javax.inject.Named; import org.richfaces.cdi.push.Push;

    @Named @SessionScoped public class Status实现Serializable {

    @Inject
    @Push(topic = "statusPush")
    Event<String> pushEvent;
    private String status;
    
    public void addMessage(String message) {
        //Everytime the status is updated...
        status = status.concat(message + "\n");
        //... the message is sent to the topic
        sendMessage(message);
    }
    
    public void sendMessage(String message) {
        pushEvent.fire(message);
    }
    
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    

    }

  • 处理视图上的消息:

    <a4j:push address="statusPush"
        onerror="alert(event.rf.data)">
        <a4j:ajax event="dataavailable" render="statusOutput" />
    </a4j:push>
    
    
    <h:inputTextarea id="statusOutput" value="#{status.status}" cols="80" rows="20" disabled="true"/>
    

似乎已全部设置但是当我点击开始迁移过程的按钮时,我得到以下堆栈跟踪:

  

警告:StandardWrapperValve [Faces Servlet]:PWC1406:   servlet Faces Servlet的Servlet.service()引发了异常   com.google.common.collect.ComputationException:   javax.faces.FacesException:&#39; / ConnectionFactory&#39;查找失败在   SerialContext [myEnv = {java.naming.factory.initial的= com.sun.enterprise.naming.impl.SerialInitContextFactory,   java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,   java.naming.factory.url.pkgs = com.sun.enterprise.naming} at   com.google.common.collect.ComputingConcurrentHashMap $ ComputingMapAdapter.get(ComputingConcurrentHashMap.java:397)     在   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl.createTopic(JMSTopicsContextImpl.java:281)     在   org.richfaces.application.push.TopicsContext.getOrCreateTopic(TopicsContext.java:48)     在   org.richfaces.application.push.impl.SessionImpl.createSubscriptions(SessionImpl.java:190)     在   org.richfaces.application.push.impl.SessionImpl.subscribe(SessionImpl.java:185)     在org.richfaces.resource.PushResource.encode(PushResource.java:88)     在   org.richfaces.resource.UserResourceWrapperImpl.encode(UserResourceWrapperImpl.java:188)     在   org.richfaces.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:229)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:591)at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)     在   com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)     在   com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:459)     在com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:316)at   com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:87)     在   com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:171)     在   com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:143)     在   com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:102)     在com.sun.grizzly.http.TaskBase.run(TaskBase.java:193)at   com.sun.grizzly.http.TaskBase.execute(TaskBase.java:175)at   com.sun.grizzly.arp.DefaultAsyncHandler.handle(DefaultAsyncHandler.java:145)     在   com.sun.grizzly.arp.AsyncProtocolFilter.execute(AsyncProtocolFilter.java:210)     在   com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在   com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     在   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在   com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at   com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在   com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:722)引起:   javax.faces.FacesException:&#39; / ConnectionFactory&#39;查找失败在   SerialContext [myEnv = {java.naming.factory.initial的= com.sun.enterprise.naming.impl.SerialInitContextFactory,   java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,   java.naming.factory.url.pkgs = com.sun.enterprise.naming} at   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl $ 1.适用(JMSTopicsContextImpl.java:207)     在   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl $ 1.适用(JMSTopicsContextImpl.java:195)     在   com.google.common.collect.ComputingConcurrentHashMap $ ComputingValueReference.compute(ComputingConcurrentHashMap.java:355)     在   com.google.common.collect.ComputingConcurrentHashMap $ ComputingSegment.compute(ComputingConcurrentHashMap.java:184)     在   com.google.common.collect.ComputingConcurrentHashMap $ ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)     在   com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)     在   com.google.common.collect.ComputingConcurrentHashMap $ ComputingMapAdapter.get(ComputingConcurrentHashMap.java:393)     ... 39更多引起:javax.naming.NamingException:查找失败   for&#39; / ConnectionFactory&#39;在   SerialContext [myEnv = {java.naming.factory.initial的= com.sun.enterprise.naming.impl.SerialInitContextFactory,   java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,   java.naming.factory.url.pkgs = com.sun.enterprise.naming} [Root   异常是javax.naming.NameNotFoundException:] at   com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)     在   com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)     在   com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:654)     在javax.naming.InitialContext.lookup(InitialContext.java:415)at   javax.naming.InitialContext.lookup(InitialContext.java:415)at   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl $ JMSTopicContext.createConnection(JMSTopicsContextImpl.java:98)     在   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl $ JMSTopicContext.start(JMSTopicsContextImpl.java:123)     在   org.richfaces.application.push.impl.jms.JMSTopicsContextImpl $ 1.适用(JMSTopicsContextImpl.java:199)     ... 45更多引起:javax.naming.NameNotFoundException:at   com.sun.enterprise.naming.impl.TransientContext.resolveContext(TransientContext.java:310)     在   com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:218)     在   com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)     在   com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)     在   com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)     ... 52更多

结论:JMS(我认为)无法找到statusPush主题。

更新:在web.xml中启用了JMS,更改为

<context-param>
    <param-name>org.richfaces.push.jms.enable</param-name>
    <param-value>true</param-value>
</context-param>

现在stacktrace是:

  

警告:StandardWrapperValve [AutoRegisteredPushServlet]:PWC1406:   servlet AutoRegisteredPushServlet的Servlet.service()抛出   exception java.lang.RuntimeException:java.lang.NullPointerException     在   org.atmosphere.handler.ReflectorServletProcessor.onRequest(ReflectorServletProcessor.java:171)     在   org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:248)     在   org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:166)     在   org.atmosphere.container.GrizzlyCometSupport.service(GrizzlyCometSupport.java:119)     在   org.atmosphere.container.GlassFishWebSocketSupport.service(GlassFishWebSocketSupport.java:110)     在   org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:1257)     在   org.atmosphere.cpr.AtmosphereServlet.doPost(AtmosphereServlet.java:293)     在   org.atmosphere.cpr.AtmosphereServlet.doGet(AtmosphereServlet.java:279)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:668)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:770)at at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)     在   com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)     在   com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:459)     在com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:316)at   com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:87)     在   com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:171)     在   com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:143)     在   com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:102)     在com.sun.grizzly.http.TaskBase.run(TaskBase.java:193)at   com.sun.grizzly.http.TaskBase.execute(TaskBase.java:175)at   com.sun.grizzly.arp.DefaultAsyncHandler.handle(DefaultAsyncHandler.java:145)     在   com.sun.grizzly.arp.AsyncProtocolFilter.execute(AsyncProtocolFilter.java:210)     在   com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在   com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     在   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在   com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at   com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在   com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:722)引起:   java.lang.NullPointerException at   org.richfaces.webapp.PushHandlerFilter.doFilter(PushHandlerFilter.java:90)     在   org.atmosphere.util.AtmosphereFilterChain.doFilter(AtmosphereFilterChain.java:154)     在   org.atmosphere.util.AtmosphereFilterChain.invokeFilterChain(AtmosphereFilterChain.java:131)     在   org.atmosphere.handler.ReflectorServletProcessor $ FilterChainServletWrapper.service(ReflectorServletProcessor.java:310)     在   org.atmosphere.handler.ReflectorServletProcessor.onRequest(ReflectorServletProcessor.java:168)     ......还有40多个

1 个答案:

答案 0 :(得分:0)

您必须定期调用服务器并更新textarea。这可以使用a4j:poll组件来实现,该组件定期触发ajax请求并更新表单上的特定字段。可以找到一个示例here