STEP 1:- Open Eclipse and Create Dynamic Web Project named SpringMVCExample
STEP 2:- Make sure you use Target Runtime as Apache Tomcat 7.0.
STEP 3:- copy below jars to WEB-INF/lib folder.
- commons-logging-1.2.jar
- spring-aop-4.1.4.RELEASE.jar
- spring-beans-4.1.4.RELEASE.jar
- spring-context-4.1.4.RELEASE.jar
- spring-core-4.1.4.RELEASE.jar
- spring-expression-4.1.4.RELEASE.jar
- spring-web-4.1.4.RELEASE.jar
- spring-webmvc-4.1.4.RELEASE.jar
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.tutorialsdesk.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>In the above dispatcher-servlet.xml configuration file, we have defined a tag
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SpringMVCExample</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>One thing to note here is the name of servlet in
- Package: com.tutorialsdesk.controller
- Filename: SpringMVCHelloWorld.java
package com.tutorialsdesk.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class SpringMVCHelloWorld { @RequestMapping("/index") public ModelAndView helloWorld(){ String message = "<br><div style='text-align:center;'>" + "<h3>********** Hello World, Spring MVC Example by TutorialsDesk**********</h3>This message is coming from SpringMVCHelloWorld.java</div><br><br>"; return new ModelAndView("welcome", "message", message); } }Note that we have annotated the SpringMVCHelloWorld class with @Controller and @RequestMapping("/index"). When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /index in the URL path. That includes /index/* and /index.html.
<html> <head> <title>Spring MVC Example by TutorialsDesk - Hello World Spring MVC Example</title> </head> <body>${message} <br> <br> <div style="font-family: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align:center;"> Spring MCV Tutorial by <a href="http://www.tutorialsdesk.com/">TutorialsDesk</a>. Click <a href="http://www.tutorialsdesk.com/search/label/Java" target="_blank">here</a> for all Java and <a href="http://www.tutorialsdesk.com/search/label/Spring-MVC" target='_blank'>here</a> for all Spring MVC, Web Development examples.<br> </div> </body> </html>STEP 8 :- Run your project enter below URL in your browser
Blogger Comment
Facebook Comment