You are on page 1of 8

Required Spring 3.

0 MVC Configuration:
You need to map requests that you want the DispatcherServlet to handle, by using a URL
mapping in the web.xml file. The following is an example to show declaration and mapping for
spring3 DispatcherServlet example:
Step 1:- Configure the web.xml with DispatcherServlet and details of the application
context file location.
view plainprint?
1. <web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSc
hema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java
.sun.com/xml/ns/j2ee
2.

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

3.
4.

<display-name>Spring MVC Application</display-name>

5.
6.

<servlet>

7.

<servlet-name>spring3</servlet-name>

8.

<servlet-class>

9.

org.springframework.web.servlet.DispatcherServlet

10.

</servlet-class>

11.

<load-on-startup>1</load-on-startup>

12.

</servlet>

13.
14.

<servlet-mapping>

15.

<servlet-name>spring3</servlet-name>

16.

<url-pattern>*.*</url-pattern>

17.

</servlet-mapping>

18.
19. </web-app>
Step 2:- Configure the contextConfigLocation for the application context to be loaded.
view plainprint?
1. <web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSc
hema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java
.sun.com/xml/ns/j2ee
2.

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

3.
4.

<display-name>Spring MVC Application</display-name>

5.
6.

<servlet>

7.

<servlet-name>spring3</servlet-name>

8.

<servlet-class>

9.

org.springframework.web.servlet.DispatcherServlet

10.

</servlet-class>

11.

<load-on-startup>1</load-on-startup>

12.

</servlet>

13.
14.

<servlet-mapping>

15.

<servlet-name>spring3</servlet-name>

16.

<url-pattern>*.*</url-pattern>

17.

</servlet-mapping>

18.
19.

<context-param>

20.
21.
22.
23.
24.
25.

<param-name>contextConfigLocation</param-name><param-value>/WEBINF/spring3-servlet.xml</param-value></context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

26. </web-app>
Step 3:- Configure the spring3-servlet.xml
view plainprint?
1. <beans xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="htt
p://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/s
chema/beans" xsi:schemalocation="
2.

http://www.springframework.org/schema/beans

3.

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

4.

http://www.springframework.org/schema/context

5.

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

6.
7.

<context:component-scan base-package="com.dineshonjava">

8.

</context:component-scan>

9.

<context:annotation-config></context:annotation-config>

10.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

11.

<property name="prefix" value="/WEB-INF/jsp/"></property>

12.

<property name="suffix" value=".jsp"></property>

13.

</bean>

14.
15. </beans>
Following are the important points about spring3-servlet.xml file:

The [servlet-name]-servlet.xml file will be used to create the beans defined, overriding
the definitions of any beans defined with the same name in the global scope.

The <context:component-scan...> tag will be use to activate Spring MVC annotation


scanning capability which allows to make use of annotations like @Controller and
@RequestMapping etc.

The InternalResourceViewResolver will have rules defined to resolve the view names.
As per the above defined rule, a logical view named hello is delegated to a view
implementation located at /WEB-INF/jsp/hello.jsp .

@Controller:

Used at the class level

Tells the spring framework that the marked class acts as a controller.

view plainprint?
1. @Controller
2. public class EmployeeController{
3.
4. }<i><b>
5. </b></i>
@RequestMapping:

Can be used at the class level and method level in controllers.

Arguments:-URL[]
-HTTP Methods[]-GET, POST, DELETE, TRACE, OPTIONS, HEAD, PUTS.
Defaults method supported is GET
-params[]-used to check if a request parameter matches with a value and only if the
conditions passes the method or controller processes the request.

(eg. @RequestMapping params="myName=guest" )


-headers[]-used to check if a request header matches with a value and only if the
condition passes the method or controller processes the request
(eg. @RequestMapping headers="myheader=guestHadder")
Next section will show you how to create your actual components ie. Controller, Model and
View.

Defining a Controller
DispatcherServlet delegates the request to the controllers to execute the functionality specific to
it. The @Controller annotation indicates that a particular class serves the role of a controller. The
@RequestMapping annotation is used to map a URL to either an entire class or a particular
handler method.
view plainprint?
1. @Controller
2. @RequestMapping("/employee/*")
3. public class EmployeeController{
4.
5.

@RequestMapping("add", method = RequestMethod.POST)

6.

public String createEmployee(){}//appcontext/employee/add

7.

@RequestMapping("delete", method = RequestMethod.GET)

8.

public String deleteEmployee(){}//appcontext/employee/delete

9.

@RequestMapping("details")//By default the method is GET

10.

public String getEmployeeDetails(){}//- /appcontext/employee/details

11.
12. }
You can write above controller in another form where you can add additional attributes in
@RequestMapping as follows:
view plainprint?
1. @Controller

2. public class EmployeeController{


3.
4.

@RequestMapping("add", method = RequestMethod.POST,value=/employee)

5.

public String createEmployee(){}//appcontext/employee/add

6.

@RequestMapping("delete", method = RequestMethod.GET,value=/employee)

7.

public String deleteEmployee(){}//appcontext/employee/delete

8.

@RequestMapping("details",value=/employee)//By default the method is GET

9.

public String getEmployeeDetails(){}//- /appcontext/employee/details

10.
11. }
The value attribute indicates the URL to which the handler method is mapped and the method
attribute defines the service method to handle HTTP GET request. There are following important
points to be noted about the controller defined above:

You will defined required business logic inside a service method. You can call another
methods inside this method as per requirement.

Based on the business logic defined, you will create a model within this method. You can
setter different model attributes and these attributes will be accessed by the view to
present the final result. This example creates a model with its attribute "message".

A defined service method can return a String which contains the name of the view to be
used to render the model.

Creating JSP Views


Spring MVC supports many types of views for different presentation technologies. These include
- JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, Atom and RSS
feeds, JasperReports etc. But most commonly we use JSP templates written with JSTL. So let us
write a simple employee view in /WEB-INF/emp/employee.jsp:
view plainprint?
1. <html>
2.

<head>

3.

<title>Hello Spring MVC</title>

4.

</head>

5.

<body>

6.

<h2>

7. ${name}</h2>
8. </body>
9. </html>
Here ${name} is the attribute which we have setup inside the Controller. You can have multiple
attributes to be displayed inside your view.

Spring Web MVC Framework Examples:


Based on the above concepts, let us check few important examples which will help you in
building your Spring Web Applications:
S.N.
Example & Description
1

Spring MVC Hello World Example


This example will explain how to write a simple Spring Web Hello World application.

Spring MVC Form Handling Example


This example will explain how to write a Spring Web application using HTML forms to
submit the data to the controller and display back a processed result.

Spring Page Redirection Example


Learn how to use page redirection functionality in Spring MVC Framework.

Spring Static Pages Example


Learn how to access static pages along with dynamic pages in Spring MVC Framework.

Spring Exception Handling Example


Learn how to handle exceptions in Spring MVC Framework.

Spring 3.0 MVC with Hibernate 3.0 CRUD Example


Learn how to configure Spring 3 MVC with Hibernate 3 in Spring MVC Framework.

Spring 3 MVC Tiles Plugin with Example


Learn how to configure Spring 3 MVC with Tiles configuration with example.

Spring 3 MVC Interceptor with example


Learn how to configure Spring 3 MVC with Interceptor configuration with example.

Spring 3 MVC Internationalization & Localization with Example


In this part we will discuss about Internationalization (I18N) and Localization (L10N) in
Spring 3.0 MVC.

10

How to write RESTful Web Services using Spring 3.0 MVC


How to write RESTful Web Services using Spring 3.0 MVC

11

Spring 3.0 MVC with MongoDB CRUD Example


This is the CRUD application using the NoSQL database with Spring 3.0 MVC.

12

Spring CRUD Example using One to One Mapping of Two Tables


This is the CRUD application using the One to One Mapping in two tables of database
with Spring 3.0 MVC.

13

Spring CRUD Example using Many to One Mapping


This is the CRUD application using the Many to One Mapping in three tables of database
with Spring 3.0 MVC.

You might also like