Difference between revisions of "Northwind Display Events using JSP: Step 9: Display events in a JSP"
From RifidiWiki
(→Putting it all together) |
|||
Line 1: | Line 1: | ||
− | This is Step 9 in the [[Northwind Tutorial|Northwind Application Tutorial]] | + | ---- |
+ | <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]]<br> | ||
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 | + | Modify the <tt>taglocation.jsp</tt> file to the following: |
− | + | <pre> | |
− | + | <%@ 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= | + | style="width: 80%;border-collapse: collapse;table-layout: fixed"> |
− | + | <tr> | |
− | + | <td valign="top"> | |
− | + | <table border="1" | |
− | style= | + | 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= | + | 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= | + | 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> | |
− | + | </pre> | |
===Modify the Controller=== | ===Modify the Controller=== | ||
Line 94: | Line 102: | ||
# 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. | ||
− | + | <pre> | |
package com.northwind.rfid.shipping.war; | package com.northwind.rfid.shipping.war; | ||
Line 129: | Line 137: | ||
public ModelAndView handleRequest(HttpServletRequest arg0, | public ModelAndView handleRequest(HttpServletRequest arg0, | ||
HttpServletResponse arg1) throws Exception { | HttpServletResponse arg1) throws Exception { | ||
− | HashMap | + | HashMap<String, Object> model = new HashMap<String, Object>(); |
− | model.put( | + | model.put("dockdoor", tagLocationService.getDockDoorItems()); |
− | model.put( | + | model.put("weighstation", tagLocationService.getWeighStationItems()); |
− | model.put( | + | model.put("alerts", tagLocationService.getAlerts()); |
− | return new ModelAndView( | + | return new ModelAndView("/WEB-INF/jsp/taglocation.jsp", "model", model); |
} | } | ||
} | } | ||
− | + | </pre> | |
===Modify the servlet xml=== | ===Modify the servlet xml=== | ||
Line 145: | Line 153: | ||
# Inject the TagLocationService into the Controller. | # Inject the TagLocationService into the Controller. | ||
− | Modify the | + | Modify the <tt>NorthwindDemo-servlet.xml</tt> to look as follows: |
− | + | <pre> | |
− | + | <beans xmlns="http://www.springframework.org/schema/beans" | |
− | xmlns:xsi= | + | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" |
− | xmlns:amq= | + | xmlns:amq="http://activemq.apache.org/schema/core" |
− | xsi:schemaLocation= | + | 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 | + | 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= | + | class="org.springframework.jms.listener.DefaultMessageListenerContainer"> |
− | + | <property name="connectionFactory" ref="connectionFactory" /> | |
− | + | <property name="destination" ref="NorthwindTopic" /> | |
− | + | <property name="messageListener" ref="messageReceiver" /> | |
− | + | </bean> | |
− | + | </beans> | |
− | + | </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. |
Revision as of 23:23, 23 November 2010
This is Step 9 in the Northwind Application Tutorial<br> 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 <tt>taglocation.jsp</tt> file to the following: <pre> <%@ 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> </pre>
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:
- Spring needs to inject the TagLocationService into it
- We need to modify the handleRequest method to use the TagLocationService.
<pre> 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); } } </pre>
Modify the servlet xml
The next thing is to modify the servlet xml so that it will
- Create the TagLocationService
- Create the JMS Listener and inject the TagLocationServiceManager into it
- Inject the TagLocationService into the Controller.
Modify the <tt>NorthwindDemo-servlet.xml</tt> to look as follows: <pre> <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> </pre>
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.