简体   繁体   中英

How do you create a FacesMessage from a class that is not a JSF Bean?

I am new to JSF and struggling with a problem. I am hoping that someone can help me.

Problem:

  • A handler that is not a bean detects a message from a TCP/IP pipe.
  • The handler needs to create a new FacesMessage to display the message in a message box.
  • Since the handler is not a bean, FacesContext returns null , therefore the handler cannot write the FacesMessage .

Questions:

  1. Is it somehow possible to do what I am trying to do?
  2. What is the best way to propagate an external message from a Java class that is not a bean to a FacesMessage so that it is displayed on the UI?

Any ideas on how to resolve this?

The FacesContext.getCurrentInstance() method only returns a valid faces context if you're in an actual faces session. I think what you should be looking at doing is architecting this such that your class that does the TCP/IP message handling provides a means for a faces managed bean to get information about the messages to the front end, and then accessing that method from a managed bean.

Depending on what type of application server you're using, you may want to set this whole thing up to interact through an EJB session bean. But really any backing bean or faces logic should absolutely be separate from logic unrelated to your UI.

If you're using a JEE6 compliant application server (like Glassfish) you could set up an @Singleton EJB to hold a set of messages (be sure to use a data structure that supports concurrency) and something like JCA-sockets (http://code.google.com/p/jca-sockets/) to handle the socket communication.

No, it is not possible, alternative way is to create backing bean either by @ManagedBean annotation or by having entry in faces-config.xml like

  <managed-bean>
    <managed-bean-name>bean</managed-bean-name>
    <managed-bean-class>com.test.bean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

and now you can get facesContext and create FacesMessage both, hope this helps.

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