Northwind Display Events using JSP: Step 9: Display events in a JSP

From RifidiWiki

Jump to: navigation, search

This is Step 9 in the Northwind Application Tutorial
Previous Step: Step 8: Write a Tag Location Service and JMS Listener

What You Will Learn

  • How to use the JSP standard tag library to create a dynamic web page

Create a JSP

Modify the taglocation.jsp file to the following:

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
		<title>Northwind Shipping</title>
		<meta name="generator" content="Amaya, see http://www.w3.org/Amaya/" />
	</head>

	<body>
		<table border="0"
			style="width: 80%;border-collapse: collapse;table-layout: fixed">
			<tr>
				<td valign="top">
					<table border="1"
						style="width:90%;background-color:#93D2FF;border-collapse: collapse">
						<tr>
							<td style="text-align:center">
								<h3>Dock Door</h3>
							</td>
						</tr>
						<tr>
							<td>
								<strong>EPC</strong>
							</td>
						</tr>
						<c:forEach items="${model.dockdoor}" var="tag">
							<tr>
								<td style="font-family:monospace;">${tag}</td>
							</tr>
						</c:forEach>
					</table>
				</td>
				<td valign="top">
					<table border="1"
						style="width:90%;background-color:#93D2FF;border-collapse: collapse;float:right">
						<tr>
							<td colspan="4" style="text-align:center">
								<h3>Weigh Station</h3>
							</td>
						</tr>
						<tr>
							<td>
								<strong>EPC</strong>
							</td>
						</tr>
						<c:forEach items="${model.weighstation}" var="tag">
							<tr>
								<td style="font-family:monospace">${tag}</td>
							</tr>
						</c:forEach>
					</table>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<p />
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<table border="1"
						style="width:100%;background-color:#FFA69C;border-collapse: collapse">
						<tr>
							<td>
								<h3 style="text-align:center">Alerts</h3>
							</td>
						</tr>
						<c:forEach items="${model.alerts}" var="alert">
							<tr>
								<td> Tag:<span style="font-family:monospace">${alert.tag_Id}</span>: ${alert.message}
								</td>
							</tr>
						</c:forEach>
					</table>
				</td>
			</tr>
		</table>
	</body>
</html>

Modify the Controller

Now that we have a TagLocationService which is receiving notifications from our RFID application, and we have a JSP that will display the information, we need to hook up the Controller with the TagLocationService.

We first need to modify the controller to do two things:

  1. Spring needs to inject the TagLocationService into it
  2. We need to modify the handleRequest method to use the TagLocationService.
package com.northwind.rfid.shipping.war;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.northwind.rfid.shipping.war.service.TagLocationService;

/**
 * @author Kyle Neumeier - kyle@pramari.com
 * 
 */
public class TagLocationController implements Controller {

	/** The Tag Location Service */
	private volatile TagLocationService tagLocationService;

	/**
	 * Called by Spring
	 * 
	 * @param tagLocationService
	 *            the tagLocationService to set
	 */
	public void setTagLocationService(TagLocationService tagLocationService) {
		this.tagLocationService = tagLocationService;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("dockdoor", tagLocationService.getDockDoorItems());
		model.put("weighstation", tagLocationService.getWeighStationItems());
		model.put("alerts", tagLocationService.getAlerts());

		return new ModelAndView("/WEB-INF/jsp/taglocation.jsp", "model", model);
	}
}

Modify the servlet xml

The next thing is to modify the servlet xml so that it will

  1. Create the TagLocationService
  2. Create the JMS Listener and inject the TagLocationServiceManager into it
  3. Inject the TagLocationService into the Controller.

Modify the NorthwindDemo-servlet.xml to look as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
	xmlns:amq="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	META-INF/xsd/spring-beans-2.5.xsd
    http://www.springframework.org/schema/osgi 
    http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<!-- Create the Controller -->
	<bean name="/taglocation.htm" class="com.northwind.rfid.shipping.war.TagLocationController" >
		<property name="tagLocationService" ref = "tagLocService"/>
	</bean>
	
	<!-- Create the TagLocationService -->
	<bean id="tagLocService" class="com.northwind.rfid.shipping.war.service.impl.TagLocationServiceImpl" />
	
	<!-- Create the JMS Message Receiver-->
	<bean id="messageReceiver" class="com.northwind.rfid.shipping.war.MessageReceiver">
		<property name="TLSManager" ref="tagLocService" />
	</bean>
	
	<!-- Create the topic to connect to -->
	<bean id="NorthwindTopic" class="org.apache.activemq.command.ActiveMQTopic">
		<property name="physicalName" value="com.northwind.rfid.shipping.topic" />
	</bean>
	
	<!-- JMS Connection Factory -->
	<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
		<property name="brokerURL" value="vm://externalBroker?create=false" />
	</bean>
	
	<!-- Spring Helper to listen to a JMS Destination -->
	<bean id="jmsContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="NorthwindTopic" />
		<property name="messageListener" ref="messageReceiver" />
	</bean>

</beans>

Run The Application

Now you can run the whole application. You can point your browser to the web application, start emulator and place tags on the readers. As you move tags around, you can hit the refresh button on your browser to see the tags.

Personal tools