Difference between revisions of "Northwind Display Events using JSP: Step 9: Display events in a JSP"

From RifidiWiki

Jump to: navigation, search
m (Reverted edits by Amesycyxa (Talk); changed back to last version by Kyle)
 
Line 1: Line 1:
----
+
This is Step 9 in the [[Northwind Tutorial|Northwind Application Tutorial]]<br>
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
+
----
+
=[http://yxiwisewava.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
+
----
+
=[http://yxiwisewava.co.cc CLICK HERE]=
+
----
+
</div>
+
This is Step 9 in the [[Northwind Tutorial|Northwind Application Tutorial]]&lt;br&gt;
+
 
Previous Step: [[Northwind TagLocationService | Step 8: Write a Tag Location Service and JMS Listener]]
 
Previous Step: [[Northwind TagLocationService | Step 8: Write a Tag Location Service and JMS Listener]]
 
===What You Will Learn===
 
===What You Will Learn===
 
* How to use the JSP standard tag library to create a dynamic web page
 
* How to use the JSP standard tag library to create a dynamic web page
 
===Create a JSP===
 
===Create a JSP===
Modify the &lt;tt&gt;taglocation.jsp&lt;/tt&gt; file to the following:
+
Modify the <tt>taglocation.jsp</tt> file to the following:
&lt;pre&gt;
+
<pre>
&lt;%@ include file=&quot;/WEB-INF/jsp/include.jsp&quot; %&gt;
+
<%@ include file="/WEB-INF/jsp/include.jsp" %>
  
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
+
<html xmlns="http://www.w3.org/1999/xhtml">
&lt;head&gt;
+
<head>
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
+
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
&lt;title&gt;Northwind Shipping&lt;/title&gt;
+
<title>Northwind Shipping</title>
&lt;meta name=&quot;generator&quot; content=&quot;Amaya, see http://www.w3.org/Amaya/&quot; /&gt;
+
<meta name="generator" content="Amaya, see http://www.w3.org/Amaya/" />
&lt;/head&gt;
+
</head>
  
&lt;body&gt;
+
<body>
&lt;table border=&quot;0&quot;
+
<table border="0"
style=&quot;width: 80%;border-collapse: collapse;table-layout: fixed&quot;&gt;
+
style="width: 80%;border-collapse: collapse;table-layout: fixed">
&lt;tr&gt;
+
<tr>
&lt;td valign=&quot;top&quot;&gt;
+
<td valign="top">
&lt;table border=&quot;1&quot;
+
<table border="1"
style=&quot;width:90%;background-color:#93D2FF;border-collapse: collapse&quot;&gt;
+
style="width:90%;background-color:#93D2FF;border-collapse: collapse">
&lt;tr&gt;
+
<tr>
&lt;td style=&quot;text-align:center&quot;&gt;
+
<td style="text-align:center">
&lt;h3&gt;Dock Door&lt;/h3&gt;
+
<h3>Dock Door</h3>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;tr&gt;
+
<tr>
&lt;td&gt;
+
<td>
&lt;strong&gt;EPC&lt;/strong&gt;
+
<strong>EPC</strong>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;c:forEach items=&quot;${model.dockdoor}&quot; var=&quot;tag&quot;&gt;
+
<c:forEach items="${model.dockdoor}" var="tag">
&lt;tr&gt;
+
<tr>
&lt;td style=&quot;font-family:monospace;&quot;&gt;${tag}&lt;/td&gt;
+
<td style="font-family:monospace;">${tag}</td>
&lt;/tr&gt;
+
</tr>
&lt;/c:forEach&gt;
+
</c:forEach>
&lt;/table&gt;
+
</table>
&lt;/td&gt;
+
</td>
&lt;td valign=&quot;top&quot;&gt;
+
<td valign="top">
&lt;table border=&quot;1&quot;
+
<table border="1"
style=&quot;width:90%;background-color:#93D2FF;border-collapse: collapse;float:right&quot;&gt;
+
style="width:90%;background-color:#93D2FF;border-collapse: collapse;float:right">
&lt;tr&gt;
+
<tr>
&lt;td colspan=&quot;4&quot; style=&quot;text-align:center&quot;&gt;
+
<td colspan="4" style="text-align:center">
&lt;h3&gt;Weigh Station&lt;/h3&gt;
+
<h3>Weigh Station</h3>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;tr&gt;
+
<tr>
&lt;td&gt;
+
<td>
&lt;strong&gt;EPC&lt;/strong&gt;
+
<strong>EPC</strong>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;c:forEach items=&quot;${model.weighstation}&quot; var=&quot;tag&quot;&gt;
+
<c:forEach items="${model.weighstation}" var="tag">
&lt;tr&gt;
+
<tr>
&lt;td style=&quot;font-family:monospace&quot;&gt;${tag}&lt;/td&gt;
+
<td style="font-family:monospace">${tag}</td>
&lt;/tr&gt;
+
</tr>
&lt;/c:forEach&gt;
+
</c:forEach>
&lt;/table&gt;
+
</table>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;tr&gt;
+
<tr>
&lt;td colspan=&quot;2&quot;&gt;
+
<td colspan="2">
&lt;p /&gt;
+
<p />
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;tr&gt;
+
<tr>
&lt;td colspan=&quot;2&quot;&gt;
+
<td colspan="2">
&lt;table border=&quot;1&quot;
+
<table border="1"
style=&quot;width:100%;background-color:#FFA69C;border-collapse: collapse&quot;&gt;
+
style="width:100%;background-color:#FFA69C;border-collapse: collapse">
&lt;tr&gt;
+
<tr>
&lt;td&gt;
+
<td>
&lt;h3 style=&quot;text-align:center&quot;&gt;Alerts&lt;/h3&gt;
+
<h3 style="text-align:center">Alerts</h3>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;c:forEach items=&quot;${model.alerts}&quot; var=&quot;alert&quot;&gt;
+
<c:forEach items="${model.alerts}" var="alert">
&lt;tr&gt;
+
<tr>
&lt;td&gt; Tag:&lt;span style=&quot;font-family:monospace&quot;&gt;${alert.tag_Id}&lt;/span&gt;: ${alert.message}
+
<td> Tag:<span style="font-family:monospace">${alert.tag_Id}</span>: ${alert.message}
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;/c:forEach&gt;
+
</c:forEach>
&lt;/table&gt;
+
</table>
&lt;/td&gt;
+
</td>
&lt;/tr&gt;
+
</tr>
&lt;/table&gt;
+
</table>
&lt;/body&gt;
+
</body>
&lt;/html&gt;
+
</html>
&lt;/pre&gt;
+
</pre>
  
 
===Modify the Controller===
 
===Modify the Controller===
Line 102: Line 94:
 
# Spring needs to inject the TagLocationService into it
 
# Spring needs to inject the TagLocationService into it
 
# We need to modify the handleRequest method to use the TagLocationService.
 
# We need to modify the handleRequest method to use the TagLocationService.
&lt;pre&gt;
+
<pre>
 
package com.northwind.rfid.shipping.war;
 
package com.northwind.rfid.shipping.war;
  
Line 137: Line 129:
 
public ModelAndView handleRequest(HttpServletRequest arg0,
 
public ModelAndView handleRequest(HttpServletRequest arg0,
 
HttpServletResponse arg1) throws Exception {
 
HttpServletResponse arg1) throws Exception {
HashMap&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
+
HashMap<String, Object> model = new HashMap<String, Object>();
model.put(&quot;dockdoor&quot;, tagLocationService.getDockDoorItems());
+
model.put("dockdoor", tagLocationService.getDockDoorItems());
model.put(&quot;weighstation&quot;, tagLocationService.getWeighStationItems());
+
model.put("weighstation", tagLocationService.getWeighStationItems());
model.put(&quot;alerts&quot;, tagLocationService.getAlerts());
+
model.put("alerts", tagLocationService.getAlerts());
  
return new ModelAndView(&quot;/WEB-INF/jsp/taglocation.jsp&quot;, &quot;model&quot;, model);
+
return new ModelAndView("/WEB-INF/jsp/taglocation.jsp", "model", model);
 
}
 
}
 
}
 
}
&lt;/pre&gt;
+
</pre>
  
 
===Modify the servlet xml===
 
===Modify the servlet xml===
Line 153: Line 145:
 
# Inject the TagLocationService into the Controller.
 
# Inject the TagLocationService into the Controller.
  
Modify the &lt;tt&gt;NorthwindDemo-servlet.xml&lt;/tt&gt; to look as follows:
+
Modify the <tt>NorthwindDemo-servlet.xml</tt> to look as follows:
&lt;pre&gt;
+
<pre>
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
+
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:osgi=&quot;http://www.springframework.org/schema/osgi&quot;
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:amq=&quot;http://activemq.apache.org/schema/core&quot;
+
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans  
+
xsi:schemaLocation="http://www.springframework.org/schema/beans  
 
META-INF/xsd/spring-beans-2.5.xsd
 
META-INF/xsd/spring-beans-2.5.xsd
 
     http://www.springframework.org/schema/osgi  
 
     http://www.springframework.org/schema/osgi  
     http://www.springframework.org/schema/osgi/spring-osgi.xsd&quot;&gt;
+
     http://www.springframework.org/schema/osgi/spring-osgi.xsd">
  
&lt;!-- Create the Controller --&gt;
+
<!-- Create the Controller -->
&lt;bean name=&quot;/taglocation.htm&quot; class=&quot;com.northwind.rfid.shipping.war.TagLocationController&quot; &gt;
+
<bean name="/taglocation.htm" class="com.northwind.rfid.shipping.war.TagLocationController" >
&lt;property name=&quot;tagLocationService&quot; ref = &quot;tagLocService&quot;/&gt;
+
<property name="tagLocationService" ref = "tagLocService"/>
&lt;/bean&gt;
+
</bean>
 
 
&lt;!-- Create the TagLocationService --&gt;
+
<!-- Create the TagLocationService -->
&lt;bean id=&quot;tagLocService&quot; class=&quot;com.northwind.rfid.shipping.war.service.impl.TagLocationServiceImpl&quot; /&gt;
+
<bean id="tagLocService" class="com.northwind.rfid.shipping.war.service.impl.TagLocationServiceImpl" />
 
 
&lt;!-- Create the JMS Message Receiver--&gt;
+
<!-- Create the JMS Message Receiver-->
&lt;bean id=&quot;messageReceiver&quot; class=&quot;com.northwind.rfid.shipping.war.MessageReceiver&quot;&gt;
+
<bean id="messageReceiver" class="com.northwind.rfid.shipping.war.MessageReceiver">
&lt;property name=&quot;TLSManager&quot; ref=&quot;tagLocService&quot; /&gt;
+
<property name="TLSManager" ref="tagLocService" />
&lt;/bean&gt;
+
</bean>
 
 
&lt;!-- Create the topic to connect to --&gt;
+
<!-- Create the topic to connect to -->
&lt;bean id=&quot;NorthwindTopic&quot; class=&quot;org.apache.activemq.command.ActiveMQTopic&quot;&gt;
+
<bean id="NorthwindTopic" class="org.apache.activemq.command.ActiveMQTopic">
&lt;property name=&quot;physicalName&quot; value=&quot;com.northwind.rfid.shipping.topic&quot; /&gt;
+
<property name="physicalName" value="com.northwind.rfid.shipping.topic" />
&lt;/bean&gt;
+
</bean>
 
 
&lt;!-- JMS Connection Factory --&gt;
+
<!-- JMS Connection Factory -->
&lt;bean id=&quot;connectionFactory&quot; class=&quot;org.apache.activemq.spring.ActiveMQConnectionFactory&quot;&gt;
+
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
&lt;property name=&quot;brokerURL&quot; value=&quot;vm://externalBroker?create=false&quot; /&gt;
+
<property name="brokerURL" value="vm://externalBroker?create=false" />
&lt;/bean&gt;
+
</bean>
 
 
&lt;!-- Spring Helper to listen to a JMS Destination --&gt;
+
<!-- Spring Helper to listen to a JMS Destination -->
&lt;bean id=&quot;jmsContainer&quot;
+
<bean id="jmsContainer"
class=&quot;org.springframework.jms.listener.DefaultMessageListenerContainer&quot;&gt;
+
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
&lt;property name=&quot;connectionFactory&quot; ref=&quot;connectionFactory&quot; /&gt;
+
<property name="connectionFactory" ref="connectionFactory" />
&lt;property name=&quot;destination&quot; ref=&quot;NorthwindTopic&quot; /&gt;
+
<property name="destination" ref="NorthwindTopic" />
&lt;property name=&quot;messageListener&quot; ref=&quot;messageReceiver&quot; /&gt;
+
<property name="messageListener" ref="messageReceiver" />
&lt;/bean&gt;
+
</bean>
  
&lt;/beans&gt;
+
</beans>
&lt;/pre&gt;
+
</pre>
  
 
===Run The Application===
 
===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.
 
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.

Latest revision as of 19:41, 26 November 2010

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