Spring MVC Exercise 1: Build and run "springMVCskeleton" sample application Acknowledgment: This exercise is based on the "Basic Spring Web MVC Example" tutorial from memestorm.com. Here’s a simple project, which doesn’t use all of Spring’s features, but serves as a good helloworld-like sample application. When you’re putting together a simple web application, you need a few essentials: A dispatcher servlet, which will intercept incoming requests. A configuration that instructs the web container to route requests to the dispatcher servlet (ie. a servlet mapping) Controllers (the C in MVC) that can perform the business logic and route to particular views (the V) perhaps conveying information models (the M). A way for the dispatcher servlet to know which URLs to map to which controllers - called a url mapping A way to determine, when a controller has done its job, how to get to the view - called a view resolver The simple application here, springMVCskeleton, doesn’t have a model at all, has a single controller (DispatchController), and a single view (myview.jsp) (1.1) Open, build, and run "springMVCskeleton" sample JSP application 0. Start NetBeans IDE. 1. Open springMVCskeleton NetBeans project. Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears. Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory. o Windows: If you unzipped the 4918_springmvc.zip file under C:\ directory, the directory to which you want to browse down should be C:\springmvc\samples. o Solaris/Linux: If you unzipped the 4918_springmvc.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/springmvc/samples. Select springMVCskeleton. Click Open Project Folder.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Spring MVC
Exercise 1: Build and run "springMVCskeleton" sample application
Acknowledgment: This exercise is based on the "Basic Spring Web MVC Example" tutorial from memestorm.com.
Here’s a simple project, which doesn’t use all of Spring’s features, but serves as a good helloworld-like sample application.
When you’re putting together a simple web application, you need a few essentials:
A dispatcher servlet, which will intercept incoming requests. A configuration that instructs the web container to route requests to the dispatcher
servlet (ie. a servlet mapping) Controllers (the C in MVC) that can perform the business logic and route to particular
views (the V) perhaps conveying information models (the M). A way for the dispatcher servlet to know which URLs to map to which controllers -
called a url mapping A way to determine, when a controller has done its job, how to get to the view - called
a view resolver
The simple application here, springMVCskeleton, doesn’t have a model at all, has a single controller (DispatchController), and a single view (myview.jsp)
(1.1) Open, build, and run "springMVCskeleton" sample JSP application
0. Start NetBeans IDE. 1. Open springMVCskeleton NetBeans project.
Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory.
o Windows: If you unzipped the 4918_springmvc.zip file under C:\ directory, the directory to which you want to browse down should be C:\springmvc\samples.
o Solaris/Linux: If you unzipped the 4918_springmvc.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/springmvc/samples.
Select springMVCskeleton. Click Open Project Folder.
Observe that the springMVCskeleton project node appears under Projects tab window.
2. Remove unresolved references and add Spring framework library files and commong logging library files to the project.
You probably don't need all the library files in this project. But for the sake of simplicity of the project, you are adding every library file to the project.
Right click spingMVCskeleton project and select Properties. Observe that Project Properties dialog box appears.
The configuration in the web.xml specifies the following - Create the servlet for me, call it dispatch, and map all incoming URLs that start with send to the dispatch servlet. So for example, a URL such as http://.../skeleton1/send/foobar would activate the dispatch servlet.
The rest of the configuration is done in the Spring MVC application configuration file. Spring automatically looks for it using the name of the servlet as a hint. Since our servlet is called dispatch, it looks for a file called dispatch-servlet.xml in your WEB-INF directory. This configuration file will have three bits to it: the controller(s), the url mapping and the view resolver.
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - A simple root application context. See dispatch-servlet.xml for the - Web context.--><beans> <bean id="a" class="com.memestorm.A"/> <bean id="b" class="com.memestorm.B"> <property name="a" ref="a"/> </bean> </beans>
(1.3) Control flow
1. index.jsp is accessed
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html> <head> <title>Spring Skeleton</title> </head> <body>
Try accessing the controller by going to <a href="send/actionName">this link</a>. </body></html>
When you click the link in the index.jsp, the control goes to send/actionName, which will be handled by the DispatchServlet of the Spring MVC framework.
2. The actionName() method of the DispatchController class is now called to handle the request according to the configuration in the dispatch-servlet.xml.
public class DispatchController extends MultiActionController { public ModelAndView actionName(HttpServletRequest request, HttpServletResponse response) throws Exception { Log log = LogFactory.getLog(this.getClass()); log.info("Controller calling Earth. Come in Earth."); String msg = ((B) (WebApplicationContextUtils .getWebApplicationContext(getServletContext()).getBean("b"))) .getA().foo(); ModelAndView mav = new ModelAndView("myview"); mav.addObject("message", msg); return mav; } }
DispatchController.java
The actionName() method selects myview as the View to display. It also creates model object in which it added message. The viewResolver selects /WEB-INF/views/myview.jsp as the view to display based on the configuration below.
Display the context senstive Javadoc on MultiActionController.
Trouble-shooting: If you don't see the Javadoc as shown above, you have not properly configured the Javadoc of the Spring framework to the NetBeans. Please see here for instruction on how to configure Spring framework Javadoc to the NetBeans.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome!</title> </head> <body> Hello, you're in myview.jsp! <br /> The controller sent me some data, here it is: <b> <%=request.getAttribute("message") %> </b> </body></html>
Note that it retrieves the value of the message attribute in the request scope and displays it.Summary
In this exercise, you learned how a controller is selected based on mapping configuration. You also learned how the controller selects a view.
return to the top
Exercise 2: Build and run "springMVCskeleton2" sample application
The "springMVCskeleton2" sample application is a slightly modified version of the springMVCskeleton application. It uses the 2nd controller.
(2.1) Open, build, and run "springMVCskeleton2" sample JSP application
1. Open springMVCskeleton2 NetBeans project.
Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory.
Select springMVCskeleton2. Click Open Project Folder. The springMVCskeleton2 project node appears
under Projects tab window.
2. Resolve a reference problem as described above.3. Build and run springMVCskeleton2 project.
Right-click project node and select Run Project. The browser gets displayed. Clicdk this link of the 2nd line.
Try accessing the 1st controller by going to <a href="send/actionName">this link</a>.<p>Try accessing the 2nd controller by going to <a href="send/actionName2">this link</a>.
When the link for send/actionName2 is clicked, the displatchController2 is used to handle the request according to the configuration below.
public class DispatchController2 extends MultiActionController { public ModelAndView actionName2(HttpServletRequest request, HttpServletResponse response) throws Exception { Log log = LogFactory.getLog(this.getClass());
log.info("Controller calling Earth. Come in Earth."); String msg = ((B) (WebApplicationContextUtils .getWebApplicationContext(getServletContext()).getBean("b"))) .getA().foo(); ModelAndView mav = new ModelAndView("myview2"); mav.addObject("message2", msg); return mav; } }
The actionName2() method selects myview2 as the view to display. The viewResolver will select /WEB-INF/views/myview2.jsp as the page to display accoring to the configuration below.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome!</title> </head> <body> Hello, you're in myview2.jsp! <br /> The controller sent me some data, here it is: <b> <%=request.getAttribute("message2") %> </b> </body></html>
Summary
In this exercise, you learned how to use two different controllers.
return to the top
Exercise 3: Build and run "springMVCbanking" sample application
Acknowledgment: This exercise is based on the "Spring Series, Part 3: Swing into Spring MVC" tutorial written by Naveen Balani.
(3.1) Open, build, and run "springMVCbanking" sample JSP application
Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory.
Select springMVCbanking. Click Open Project Folder. The springMVCbanking project node appears
under Projects tab window.
2. Resolve a reference problem as described above.
3. Build and run springMVCbanking project.
Right-click project and select Run Project. Observe that the login page is displayed. For User id field, enter admin. For Password field, enter password. Click login button.
Observe that the Account Details page is displayed.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:forward page="login.html" /> </body></html>
index.jsp
The control is forwarded to login.html, which will be handled by DispatcherServlet. Because of the following mapping configuration, it will be handled, it will be handled by the loginBankController.
In this exercise, you have built and run a ready-to-build-and-run "SpringInjectionNaming" sample application.
return to the top
Exercise 4: Build and run "JumpIntoSpringMVC" sample application
Acknowledgment: This exercise is based on the sample applications provided by Spring MVC abd Wev Flow book written by Seth Ladd, Darren Davison, Steven Devijver, and Colin Yeates. The book is highly recommended.
(4.1) Open, build, and run "JumpIntoSpringMVC" sample JSP application
1. Open JumpIntoSpringMVC NetBeans project.
Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory.
Select JumpIntoSpringMVC. Click Open Project Folder. The JumpIntoSpringMVC project node appears
under Projects tab window.
2. Resolve a reference problem as described above.3. Build and run JumpIntoSpringMVC project.
Right-click project and select Run Project. Observe the result is displayed in the Output window.
<p> You searched for flights leaving ${flightSearchCriteria.departFrom} on or about ${flightSearchCriteria.departOn}, heading to ${flightSearchCriteria.arriveAt}, returning on or about ${flightSearchCriteria.returnOn}. </p> <table> <thead> <tr> <th>Number of Legs</th> <th>Total Travel Time</th> <th>Total Cost</th> </tr> </thead> <tbody> <c:forEach items="${flights}" var="flight"> <tr> <td>${flight.numberOfLegs}</td> <td>${flight.totalTravelTimeHours}</td> <td>$${flight.totalCost}</td> </tr> </c:forEach> </tbody> </table> </body></html>
Summary
In this exercise, you have built and run a ready-to-build-and-run "JumpIntoSpringMVC" sample application.
return to the top
Exercise 5: Build and run "ExpertSpringMVC" sample application
Acknowledgment: This exercise is based on the sample applications provided by Spring MVC abd Wev Flow book written by Seth Ladd, Darren Davison, Steven Devijver, and Colin Yeates. The book is highly recommended.
(5.1) Open, build, and run "ExpertSpringMVC" sample JSP application
1. Open ExpertSpringMVC NetBeans project.
Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/samples directory.
Click Open Project Folder. The ExpertSpringMVC project node appears under Projects tab window.
2. Remove broken references and add libaries.
You probably don't need all the library files in this project. But for the sake of simplicity of the project, you are adding every library file to the project.
Right click spingMVCskeleton project and select Properties. Observe that Project Properties dialog box appears.
Select Libraries under Categories on the left. Remove all broken references on the left by selecting them all and select
Remove on the right.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib/Spring-framework-1.2.9 directory.
Select all the files and click Open button.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib/dom4j-1.6.1 directory.
Select all the files and click Open button.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib/logging-log4j-1.2.14 directory.
Select all the files and click Open button.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib/struts-1.3.8 directory.
Select all the files and click Open button.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib/jstl directory.
Select all the files and click Open button.
Browse down to <LAB_UNZIPPED_DIRECTORY>/springmvc/lib2 directory. Select all the files and click Open button.
Click OK button of the Project Properties dialog box to close it.
3. Build and run ExpertSpringMVC project.
Right-click project and select Run Project. Observe the result is displayed in the Output window.