Download tomcatPluginV331.zip, unzip it in eclipse_home/dropins folder for eclipse luna and restart eclipse. Three tomcat icons will appear in the tool bar of eclipse (start, stop and restart).
This will create bare structure for web application
<?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>spring-tutorial/display-name>
<servlet>
<servlet-name>tutorial</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>tutorial</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
So whenever there is a request for *.html it goes to DispatcherServlet which is the front controller for spring MVC.
This file name should match the servlet name "tutorial" in the above web.xml file followed by -servlet.xml i.e., the file name should be *-servlet.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.tutorial.spring4" />
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
So the base-package is the package where it will scan for controller classes (which are annotted with @Controller) The method in the controller will return the view. This view file will be under /WEB-INF/jsp/ with suffix .jsp
package com.tutorial.spring4;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<div style='text-align:center;'>"
+ "<h3>Hello World, Spring MVC Tutorial</h3>"
+ "This message is coming from MyController.java</div>";
return new ModelAndView("welcome", "message", message);
}
}
Now create folder jsp under WEB-INF and then create welcome.jsp under jsp folder
<html>
<head><title>Spring MVC Tutorial</title></head>
<body>${message}</body>
</html>
As we selected server.xml for Context declaration mode in Step 1, lets configure server.xml in the folder tomcat_home/conf/
Add this line inside <host> tag
<Context docBase="/Users/darpanmhatre/spring4/workspace_tutorial/spring-tutorial/WebContent" path="/spring4"></Context>
Also make sure you select Default output folder:
spring-tutorial/WebContent/WEB-INF/classes
This is the folder where all the compiled code will be generated
Now go to eclipse tool bar Project and then click Clean. This will remove old files and recompile the project and move the compiled code under WEB-INF/classes.
Now run the server by clicking the tomcat icon in eclipse tool bar
http://localhost:8080/spring4/welcome.html