Spring 3 MVC: Create Hello World application in Spring 3.0 MVC
2013-10-28 00:09
711 查看
Welcome to the Part 2 of Spring 3.0 MVC Series. In
previous article we went through the Introduction of Spring MVC 3.0 framework, its request processing lifecycle and architecture diagram. In this article, let us create a simple Hello World application in Spring MVC 3.0.
For creating the hello world demo application, we will use Eclipse IDE.
Part 2: Create Hello World Application in Spring 3.0 MVC
Part 3: Handling Forms in Spring 3.0 MVC
Part 4: Spring 3 MVC Tiles Plugin Tutorial with Example in Eclipse
Part 5: Spring 3 MVC Internationalization & Localization Tutorial with Example in Eclipse
Part 6: Spring 3 MVC Themes in Spring-Tutorial with Example
Part 7: Create Spring 3 MVC Hibernate 3 Example using Maven in Eclipse
Spring 3 MVC Interceptor tutorial
Related:
Spring MVC: Save / Retrieve BLOB object with Hibernate
How to change spring-servlet.xml filename
Spring MVC: Multiple Row Form Submit using List of Beans
Spring 3 MVC – Autocomplete with JQuery & JSON example
Spring MVC + FreeMarker (FTL) Integration example
Spring MVC HashMap Form Integration example
Spring MVC Multiple File Upload example
JDK 1.5 above (download)
Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
Eclipse 3.2.x above (download)
Spring 3.0 MVC JAR files:(download). Following are the list of JAR files required for this application.
commons-logging-1.0.4.jar
jstl-1.2.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
Note that depending on the current version of Spring MVC, the version number of above jar files may change.
hello which will display a message “Hello World, Spring 3.0!”.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
After selecting Dynamic Web Project, press Next.
Write the name of the project. For example Spring3MVC. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
Once the project is created, you can see its structure in Project Explorer.
Now copy all the required JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists.
Create a class called HelloWorldController in net.viralpatel.spring3.controller package and copy following content into it.
File: net.viralpatel.spring3.controller.HelloWorldController
Note that we have annotated the HelloWorldController class with
The helloWorld() method returns
resolve to
The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed
on our view. Here we are simply passing a string.
File: WEB-INF/jsp/hello.jsp
The above JSP simply display a message using expression
Also we will need an index.jsp file which will be the entry point of our application. Create a file index.jsp under WebContent folder in your project and copy following content into it.
File: WebContent/index.jsp
Open web.xml file which is under WEB-INF folder and copy paste following code.
File: WEB-INF/web.xml
The above code in web.xml will map DispatcherServlet with url pattern *.html. Also note that we have define index.jsp as welcome file.
One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name
File: WEB-INF/spring-servlet.xml
In the above xml configuration file, we have defined a tag <context:component-scan>. This will allow Spring to load all the components from package
/WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name “hello”. This will be resolved to path /WEB-INF/jsp/hello.jsp.
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.
To run the project, right click on Project name from Project Explorer and select
Run as > Run on Server (Shortcut: Alt+Shift+X, R)
Change spring-servlet.xml Filename (Spring Web Contenxt
Configuration Filename)
Spring 3 MVC – Introduction to Spring 3 MVC Framework
Spring 3 MVC: Handling Forms in Spring 3.0 MVC
Spring 3 MVC: Themes in Spring-Tutorial with Example
Spring MVC HashMap Form Integration example
Spring 3 MVC: Internationalization
& Localization Tutorial with
previous article we went through the Introduction of Spring MVC 3.0 framework, its request processing lifecycle and architecture diagram. In this article, let us create a simple Hello World application in Spring MVC 3.0.
For creating the hello world demo application, we will use Eclipse IDE.
Spring 3.0 MVC Series
Part 1: Introduction to Spring 3.0 MVC frameworkPart 2: Create Hello World Application in Spring 3.0 MVC
Part 3: Handling Forms in Spring 3.0 MVC
Part 4: Spring 3 MVC Tiles Plugin Tutorial with Example in Eclipse
Part 5: Spring 3 MVC Internationalization & Localization Tutorial with Example in Eclipse
Part 6: Spring 3 MVC Themes in Spring-Tutorial with Example
Part 7: Create Spring 3 MVC Hibernate 3 Example using Maven in Eclipse
Spring 3 MVC Interceptor tutorial
Related:
Spring MVC: Save / Retrieve BLOB object with Hibernate
How to change spring-servlet.xml filename
Spring MVC: Multiple Row Form Submit using List of Beans
Spring 3 MVC – Autocomplete with JQuery & JSON example
Spring MVC + FreeMarker (FTL) Integration example
Spring MVC HashMap Form Integration example
Spring MVC Multiple File Upload example
Things We Need
Before we starts with our first Hello World Spring MVC Example, we will need few tools.JDK 1.5 above (download)
Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
Eclipse 3.2.x above (download)
Spring 3.0 MVC JAR files:(download). Following are the list of JAR files required for this application.
commons-logging-1.0.4.jar
jstl-1.2.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
Note that depending on the current version of Spring MVC, the version number of above jar files may change.
Our Goal
Our goal is to create a basic Spring MVC application using latest 3.0 version. There will be an index page which will display a link “Say Hello” to user. On clicking this link, user will be redirected to another pagehello which will display a message “Hello World, Spring 3.0!”.
Getting Started
Let us start with our first Spring 3.0 MVC based application.Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
After selecting Dynamic Web Project, press Next.
Write the name of the project. For example Spring3MVC. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
Once the project is created, you can see its structure in Project Explorer.
Now copy all the required JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists.
The Spring Controller Class
We will need a spring mvc controller class that will process the request and display a “Hello World” message. For this we will create a packagenet.viralpatel.spring3.controllerin the source folder. This package will contain the Controller file.
Create a class called HelloWorldController in net.viralpatel.spring3.controller package and copy following content into it.
File: net.viralpatel.spring3.controller.HelloWorldController
@Controllerand
@RequestMapping("/hello")on line 7 and 10. When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The
@RequestMappingannotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes
/hello/*and
/hello.html.
The helloWorld() method returns
ModelAndViewobject. The ModelAndView object tries to resolve to a view named “hello” and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will
resolve to
"/WEB-INF/jsp/hello.jsp". We will discuss this shortly how the logical name “hello” which is return in ModelAndView object is mapped to path /WEB-INF/jsp/hello.jsp.
The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed
on our view. Here we are simply passing a string.
The View: Create JSP
To display the hello world message we will create a JSP. Note that this JSP is created in folder /WEB-INF/jsp. Createhello.jspunder WEB-INF/jsp directory and copy following content into it.
File: WEB-INF/jsp/hello.jsp
${message}. Note that the name “message” is the one which we have set in
ModelAndViewobject with the message string.
Also we will need an index.jsp file which will be the entry point of our application. Create a file index.jsp under WebContent folder in your project and copy following content into it.
File: WebContent/index.jsp
Mapping Spring MVC in WEB.xml
As discussed in the previous article (Introduction to Spring 3.0 MVC), the entry point of Spring MVC application will be the Servlet define in deployment descriptor (web.xml). Hence we will define an entry oforg.springframework.web.servlet.DispatcherServletclass in web.xml.
Open web.xml file which is under WEB-INF folder and copy paste following code.
File: WEB-INF/web.xml
One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name
[servlet-name]-servlet.xmlin WEB-INF folder of web application. In this example, the framework will look for file called
spring-servlet.xml.
Spring configuration file
Create a file spring-servlet.xml in WEB-INF folder and copy following content into it.File: WEB-INF/spring-servlet.xml
net.viralpatel.spring3.controllerand all its child packages. This will load our
HelloWorldControllerclass. Also we have defined a bean
viewResolver. This bean will resolve the view and add prefix string
/WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name “hello”. This will be resolved to path /WEB-INF/jsp/hello.jsp.
That’s All Folks
You may want to run the application now and see the result. I assume you have already configured Tomcat in eclipse. All you need to do:Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.
To run the project, right click on Project name from Project Explorer and select
Run as > Run on Server (Shortcut: Alt+Shift+X, R)
Download Source Code
Click here to download source code (9.05kb)Moving On
In this tutorial we created a small Hello World application using Spring 3.0 MVC framework. Also we learned about the spring configuration and different annotations like@Controllerand
@RequestMapping. In next article we will see how easy it is to handle form data using Spring 3.0 MVC.
Related Posts
Spring 3 MVC Interceptor tutorial with exampleChange spring-servlet.xml Filename (Spring Web Contenxt
Configuration Filename)
Spring 3 MVC – Introduction to Spring 3 MVC Framework
Spring 3 MVC: Handling Forms in Spring 3.0 MVC
Spring 3 MVC: Themes in Spring-Tutorial with Example
Spring MVC HashMap Form Integration example
Spring 3 MVC: Internationalization
& Localization Tutorial with
相关文章推荐
- Spring 3 MVC: Create Hello World application in Spring 3.0 MVC
- Spring 3 MVC: Create Hello World Application In Spring 3.0 MVC
- Spring 3 MVC: Create Hello World Application In Spring 3.0 MVC(reprint)
- Spring3MVC:Create Hello World application in Spring 3.0 MVC
- Spring MVC3.0, Maven2, Tomcat in Eclipse for Hello World Web Application
- Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat
- Spring 3 mvc hello world application with maven and jstl
- Spring 4 MVC HelloWorld 纯注解方式(带源码)
- SpringMVC Hello World
- Spring,Ajax and jQuery Tutorial Hello World Application
- Maven+SpringMVC整合之helloworld
- (二).SpringMVC 学习-SpringMVC Hello World!
- Gradle – Spring 4 MVC Hello World Example
- Spring 3 MVC hello world example – Annotation
- SpringMVC HelloWorld(1)
- Spring MVC Hello World Example
- (二).SpringMVC 学习-SpringMVC Hello World!
- SpringMVC helloworld (xml版本)
- Gradle – Spring 4 MVC Hello World Example – Annotation
- Spring MVC Hello World Example(转)