<web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>spring3</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>*.*</url-pattern> </servlet-mapping> </web-app>Step 2:- Configure the contextConfigLocation for the application context to be loaded.
<web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>spring3</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>*.*</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring3-servlet.xml</param-value></context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>Step 3:- Configure the spring3-servlet.xml
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.dineshonjava"> </context:component-scan> <context:annotation-config></context:annotation-config> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>Following are the important points about spring3-servlet.xml file:
@Controller public class EmployeeController{ }@RequestMapping:
@Controller @RequestMapping("/employee/*") public class EmployeeController{ @RequestMapping("add", method = RequestMethod.POST) public String createEmployee(){}//appcontext/employee/add @RequestMapping("delete", method = RequestMethod.GET) public String deleteEmployee(){}//appcontext/employee/delete @RequestMapping("details")//By default the method is GET public String getEmployeeDetails(){}//- /appcontext/employee/details }You can write above controller in another form where you can add additional attributes in @RequestMapping as follows:
@Controller public class EmployeeController{ @RequestMapping("add", method = RequestMethod.POST,value=/employee) public String createEmployee(){}//appcontext/employee/add @RequestMapping("delete", method = RequestMethod.GET,value=/employee) public String deleteEmployee(){}//appcontext/employee/delete @RequestMapping("details",value=/employee)//By default the method is GET public String getEmployeeDetails(){}//- /appcontext/employee/details }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:
<html> <head> <title>Hello Spring MVC</title> </head> <body> <h2> ${name}</h2> </body> </html>Here ${name} is the attribute which we have setup inside the Controller. You can have multiple attributes to be displayed inside your view.
S.N. | Example & Description |
---|---|
1 | Spring MVC Hello World Example This example will explain how to write a simple Spring Web Hello World application. |
2 | 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. |
3 | Spring Page Redirection Example Learn how to use page redirection functionality in Spring MVC Framework. |
4 | Spring Static Pages Example Learn how to access static pages along with dynamic pages in Spring MVC Framework. |
5 | Spring Exception Handling Example Learn how to handle exceptions in Spring MVC Framework. |
6 | Spring 3.0 MVC with Hibernate 3.0 CRUD Example Learn how to configure Spring 3 MVC with Hibernate 3 in Spring MVC Framework. |
7 | Spring 3 MVC Tiles Plugin with Example Learn how to configure Spring 3 MVC with Tiles configuration with example. |
8 | Spring 3 MVC Interceptor with example Learn how to configure Spring 3 MVC with Interceptor configuration with example. |
9 | 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. |
Labels: Spring MVC, Spring3.0