Using c:forEach with Seam, JSF, and Facelets

After a couple of hours of scratching my head and Googling the Interwebs like a mad man I finally figured out why my c:forEach wasn’t iterating and displaying the data in my view. Come to find out I was using the wrong name space when declaring the JSTL core library.

First off here is the c:forEach code.

1
2
3
4
<c:forEach items="#{item.tags}" var="tag">
  <h:outputText value="#{tag.name}"/>
  <br/>
</c:forEach>

Here is the incorrect name space causing the issues. Notice the word jsp in the path.

1
xmlns:c="http://java.sun.com/jsp/jstl/core"

Now for the correct name space. Just remove the word jsp from the path.

1
xmlns:c="http://java.sun.com/jstl/core"

Bingo. Now everything is working. Don’t make same boneheaded mistake.



Using jQuery with Seam

If you are using RichFaces with your Seam application then jQuery is already there and ready to use. The only thing to remember is to use jQuery() function to refer to jQuery rather than using $(). $() is used to refer to prototype.js functions which RichFaces uses heavily.

Below is an example you can use to test jQuery. This should pop up a box on your page.

1
2
3
4
5
<script type="text/javascript" >
  jQuery(document).ready(function() {
      alert("hello seam: " + jQuery(window).height());
  });
</script >

For more information on jQuery and RichFaces check out the RichFaces Live Demo page.



Message Driven Beans (MDB) using Seam and EJB 3.0

In my previous post, JMS Messaging with Seam, I showed you how to setup a queue and send a message to that queue using Seam. Now it is time to read that message from the queue and do something with it. As you guessed, it is very simple to do when using Seam and EJB 3.0.

The code below shows how to read a text message from the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package osbi.achs.messaging;

import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.ejb.ActivationConfigProperty;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;

@MessageDriven(name = "SearchQueueListenerBean", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/achsSearch") })
public class SearchQueueListenerBean implements MessageListener {
  @Logger
  private Log log;

  @Override
  public void onMessage(Message message) {
    try {
      TextMessage textMsg = (TextMessage) message;
      System.out.println(textMsg.getText());
    } catch (JMSException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

If you followed the previous post you should see the “Seam ROCKS!” message.

That’s great but in the real world you will probably be working with objects and not simple text messages. Don’t worry. With Seam and EJB 3.0 this is also a breeze.

Lets assume in the previous post I sent a Person object to the queue instead of a text message. The code below shows you how to read the Person object from the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package osbi.achs.messaging;

import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.ejb.ActivationConfigProperty;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;
import osbi.achs.model.Person;

@MessageDriven(name = "SearchQueueListenerBean", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/achsSearch") })
public class SearchQueueListenerBean implements MessageListener {
  @Logger
  private Log log;

  @Override
  public void onMessage(Message message) {
   
    try {
      ObjectMessage objectMessage = (ObjectMessage)message;
      Person person = (Person) objectMessage.getObject();
      // TODO Do something with message
      System.out.println("Person = " + person.getSurName() + ", " + person.getGivenName());
    } catch (JMSException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

Messaging couldn’t be any easier.



Next