In this part, I’ll show how to get started with STS (SpringSource Tool Suite) and Spring MVC. We’ll only use the default project that STS generates, but it’s a good start to any development effort with Spring MVC you might get into.
STS is the easiest IDE to use when developing Spring, mainly because it bundles together almost everything you need to get started, and knows where to get the rest.
If you don’t have STS installed, just download it from SpringSource’s website here and install it on your machine.
When you get STS up and running for the first time, it will ask you (as all Eclipse distributions do) which workspace to use, and then open the welcome screen. You don’t need the welcome screen. What you want is the dashboard.
From the dashboard screen choose Spring Template Project. This will open the following view:

Choose a Spring MVC project.
The first time you do it, STS might ask you to download some extra elements from the Internet. Let it download – it will not bother you again.
You’ll need to give your project a name and a top-level package:
Click on finish – and STS will create the project for you. You can run the project by right-clicking it and selecting ‘run on server’. This will open the following dialog:
The Fabric tc server was created for you with STS. Later you might want to change it to Tomcat or another server – but for now this is alright.
Choose the server and click on next – you’ll see the following:
This dialogue lets you choose which projects you want to deploy on the server. As GoodProject is the only project in the workspace – it will be the only one here, and automatically selected for deployment because you selected it to run on the server.
Click on “Finish” to launch the server runtime. STS will ask you if you want to use Spring Insight. You can activate it if you like, but most of the times I tried that – you get an error and the server fails to start. To fix this error you’ll need to delete the server in STS and create a new one – so you might as well not activate it.
If everything went smoothly, STS will open the home page for GoodProject, Which displays an hello world message with the current server time.
Edit:See Asif’s comment at the bottom if you got “HTTP Status 404 – The requested resource () is not available”. You can either clean the project, or change and save the home.jsp file (e.g. add a space somewhere and save) – this will trigger a rebuild, and the error should go away.

Ok, so at this point – you know that you have everything working. Granted – you didn’t really have to do much – all you did is create a default Spring MVC project in STS and run it.
The New Spring MVC project template created all the things you need to have a Spring MVC project running. This comprises a few files, and the rest of this tutorial is dedicated to reviewing them.
home.jsp
The first file I want to discuss is the home.jsp file. This file is the default view generated by the new Spring MVC project wizard. Currently, this is the only thing a user will see when they access your application on the server, so it is a good place to start.
home.jsp is a JSP file. In Spring MVC, you don’t have to use JSP as your view technology. Most projects I worked on actually use JSF, but it doesn’t matter much as far as the Spring framework is concerned.
This is what home.jsp contains:
On the left side of the screenshot you can see the code in the JSP file. On the right side – you see how this code is displayed in a browser.
The first two lines of the generated JSP page are general JSP declarations: setting up the standard tag library, and declaring that the page does not use the HTTP session. Those lines are not important for us right now.
In fact, the entire JSP page is just some plain HTML which should be quite straight-forward to read. The only piece that’s interesting is the part that reads ${serverTime}.
As you can see on the right, Spring replaces the ${serverTime} phrase with a date and time string. Don’t go looking for it through the JSTL reference though – it’s not a tag. We’ll touch more on this when we talk about the controller, but for the completeness of the home.jsp discussion – it’s enough to say that serverTime is a property set by the controller and used by the view. (Just like CakePHP and other MVC frameworks, Spring MVC lets your controllers pass values to use in your views).
That’s it really for home.jsp. The only other thing worth noting at this point is that it doesn’t have a link to a css file. That’s alright.
Ok, so now you know what the home.jsp file contains. It is a simple view, that uses a value given to it by the controller. It is now time to look at this controller and see what it does.
HomeController.java
package com.duckranger.goodproject;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! the client locale is "+ locale.toString());
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
The first thing to note about the Controller is that it is really just a simple class. In Spring MVC 3 – you don’t need to make the controller extend or implement anything. However, you have to annotate it with the @Controller annotation. (line 17)
This annotation lets Spring know that this class is actually a controller, and set it up correctly at deployment time.
Line 20 shows you how to acquire a handle to a logger. This is a normal slf4j logger that Spring provides you with. Don’t fret about it.
Line 26 is where the interesting part begins. The generated controller has only one method – the home() method. There’s no mandated relationship between the name of the controller (HomeController) and the name of the method, or its return value (which we’ll get to soon) – Inside this class – all of them are more or less arbitrary. The return value could be any other String (however – you need to make sure it can be matched to a view. We’ll see in a bit how the “home” return value is mapped to the home.jsp).
The method home() is annotated with a @RequestMapping annotation. This tells Spring that this method handles requests coming in from the user. The @RequestMapping tells Spring that this method should be used to serve requests coming in to the ‘/’ context (or the home/root context for the application), but – it is restricted to HTTP GET requests only.
This restriction isn’t very important for the default home.jsp page, as there’s no way to change the server’s data status with this page. However, sometimes you’ll want to restrict specific methods to serve only POST requests (so, for example, to make sure the user fills out a form rather than craft a GET request to your website) – and that’s where this option comes in handy.
The home method generated by the template takes two arguments: A locale and a Model. Note that this is not set in stone. Just as the name of the method is flexible – so are the arguments and even the access modifier (it’s true. Try removing the Locale argument or changing the method to be private – your application will still deploy).
To understand why this works – we need to delve deep into the way Spring-MVC does things, and it’s probably a good subject for a different blog post altogether. For now it is enough to understand that because we don’t actually call this method directly (the container calls it for us when the relevant request comes in) – then it is the container’s responsibility to match the call it makes to the method’s signature. If we define a locale – the container (using the DispatcherServlet and its adapters) will find the locale sent by the HTTP headers, and pass the home() method a matching Locale object. If we were to change the method signature to only take a model – well, the Spring container will not do that (you’ll still be able to find the Locale, by the way, but if you need it inside the method – might as well add it to the signature).
So, that’s settled then, for now. Let’s agree that there are a multitude of possible method signatures that are able to serve as controller methods.
The first thing the home() method does (line 27) is log the Locale it discovered. You should be able to see this logged in the server’s console window inside STS:
As you can see, the last line in the server log is the INFO log message, detailing the locale I used to access the home() method.
Lines 29-32 create a formatted date String. This String is actually what the home.jsp page displays using the ${serverTime} directive we saw before. How does it do that?
Well – line 34 actually takes care of that:
model.addAttribute("serverTime", formattedDate );
Spring-MCV is (surprise?) an MVC framework. We’ve already seen the view (home.jsp) and the controller (HomeController.java) – surely you’d expect Spring-MVC to provide a model as well.
Remember home()’s method signature – it had a Model object inside it. This Model is supplied by the container to the controller method. The method uses this model to communicate with the view.
Model is a Spring interface:
* Copyright 2002-2009 the original author or authors.
package org.springframework.ui;
import java.util.Collection;
import java.util.Map;
/**
* Java-5-specific interface that defines a holder for model attributes.
* Primarily designed for adding attributes to the model.
* Allows for accessing the overall model as a <code>java.util.Map</code>.
*
* @author Juergen Hoeller
* @since 2.5.1
*/
public interface Model {
/**
* Add the supplied attribute under the supplied name.
* @param attributeName the name of the model attribute (never <code>null</code>)
* @param attributeValue the model attribute value (can be <code>null</code>)
*/
Model addAttribute(String attributeName, Object attributeValue);
/**
* Add the supplied attribute to this <code>Map</code> using a
* {@link org.springframework.core.Conventions#getVariableName generated name}.
* <p><emphasis>Note: Empty {@link java.util.Collection Collections} are not added to
* the model when using this method because we cannot correctly determine
* the true convention name. View code should check for <code>null</code> rather
* than for empty collections as is already done by JSTL tags.</emphasis>
* @param attributeValue the model attribute value (never <code>null</code>)
*/
Model addAttribute(Object attributeValue);
/**
* Copy all attributes in the supplied <code>Collection</code> into this
* <code>Map</code>, using attribute name generation for each element.
* @see #addAttribute(Object)
*/
Model addAllAttributes(Collection<?> attributeValues);
/**
* Copy all attributes in the supplied <code>Map</code> into this <code>Map</code>.
* @see #addAttribute(String, Object)
*/
Model addAllAttributes(Map<String, ?> attributes);
/**
* Copy all attributes in the supplied <code>Map</code> into this <code>Map</code>,
* with existing objects of the same name taking precedence (i.e. not getting
* replaced).
*/
Model mergeAttributes(Map<String, ?> attributes);
/**
* Does this model contain an attribute of the given name?
* @param attributeName the name of the model attribute (never <code>null</code>)
* @return whether this model contains a corresponding attribute
*/
boolean containsAttribute(String attributeName);
/**
* Return the current set of model attributes as a Map.
*/
Map<String, Object> asMap();
}
As you can see, the Model interface as far as the code is concerned, is a Map of attributes. The home() method uses the Model in line 34 and adds an attribute to it, using “serverName” as the key, and the formatted date string as the value.
This model is available to the home.jsp view we discussed earlier. In the view, you can use the ${…} notation to ask Spring to replace the attribute’s key with its value.
The JSP is processed on the server side before being sent to the client’s browser. During this processing, the server finds the ${serverTime} directive, and replaces it with the Model value corresponding to the serverTime key.
You can verify that this is what actually happens by trying to change the key to a non-existent one. You don’t have to redeploy the application if you only change a JSP. Just refresh the page, and see for yourself:
If you change ${serverTime} to something else – e.g. ${noTime} – the JSP processor will not find the key in the Model, and will silently fail – the time string is still there on the Model, but it isn’t found, and isn’t displayed on the page. Now change the bad value back to ${serverTime} and refresh the page – the time String should re-appear.
The last line in the home() method (line 36) returns a String to the caller: “home”. This String is used by the
container to decide which view it is supposed to render for the user, using the model.
So, to sum up so far: you define a controller using the @Controller annotation, and inside this controller you define a method that handles a specific request (an HTTP GET of the ‘/’ context).
Because of these annotations, when a request comes in to this URL, the Spring container knows that it needs to serve it using this controller and the specific method inside it.
The method does some work, and then returns a String. This String (“home”) lets the container know where to go to after the controller finished processing the request. In the case of the generated code, the container will use this String to decide which view (JSP file) to render back to the client.
Let’s see how this works then – the servlet-context.xml file.
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.duckranger.goodproject" />
</beans:beans>
servlet-context.xml is used by Spring-MVC to configure navigation, beans and view resolvers. The file name does not have to be servlet-context, but this is the default configuration and it’s good enough for this tutorial.
The full picture is this: Spring-MVC uses a special servlet called DispatcherServlet to control and serve all requests to your application. To integrate with the servlet container, this DispatcherServlet is configured in the normal web.xml file like so:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
As you can see, the servlet is configured very much like any other servlet you’d configure in a normal web application. It is given a default name: appServlet, and it is configured to serve all requests coming to the ‘/’ (root) context.
The org.springframework.web.servlet.DispatcherServlet is configured with an init-param that points to the location of its configuration file. This file Is the servlet-context.xml.
When the application is deployed, your servlet container scans web.xml for (among other things) servlets to load. It finds the DispatcherServlet with the highest priority for load-on-startup (load-on-startup tells the server container in what order it should initialize the servlets it encounters. The lower the number – the earlier the initialization will happen. The lowest meaningful value here is 1).
When the DispatcherServlet is initialized by the servlet container, it looks at its init-param, and finds its contextConfigLocation, which points to the servlet-context.xml file. As I mentioned before – you may change the name (or the location) of this file. As long as you configure it correctly in your web.xml – you should be fine.
Ok, back to servlet-context.xml:
There are a few important things to note in this configuration file:
- The <annotation-driven /> element.
This element tells the DispatcherServlet that it shouldn’t expect controllers to be configured in XML, rather – it will get its components via another method. This method can be found further down the file: <context:component-scan base-package=”com.duckranger.goodproject” /> - <context:component-scan base-package=”com.duckranger.goodproject” /> tells the Spring container where it can find components. In this case, the directive tells Spring to look for components in the com.duckranger.goodproject package. Spring will scan this package and will register as a component any class it finds annotated with the relevant annotations (e.g. @Controller).
So, together with <annotation-driven/> – these two directives ensure that Spring finds the HomeController class and registers it as a controller in the Spring-MVC application. - The final element worth noticing in this file is the definition of a ViewResolver:
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean>A ViewResolver is another Spring interface, which is responsible for directing a client to the right view in the application.
A bit about view resolvers
Generally, a controller method should not return just a String. It should actually return a ModelAndView object, which contains both the model we discussed earlier, and the view to resolve (or rather – a way to find the view).
However, as you can see, in our generated Spring-MVC project, the controller method returns only a String, which contains the view name. This is alright, as long as we tell the container how it should resolve the view and serve the correct page to the client.
The InternalResourceViewResolver does exactly that. It is configured as a bean, and since it is the only view resolver configured, the Spring container knows that this is the one it should use. In essence, the InternalResourceViewResolver receives the result of the controller method (the String returned) – and is then in charge of mapping this result to an actual view.
In the definition above, we define two properties on this resolver bean: a prefix and a suffix. The InternalResourceViewResolver concatenates the prefix, controller-return-value and suffix to come up with the actual view name to display. In our case, this will be /WEB-INF/views/home.jsp.
Using this information, it finds the right JSP page to load, processes it using the model object (same Model object in the home() method) – and sends a response back to the client.
You can verify that this is the way the InternalResourceViewResolver works by changing a few values and watching the result. Try the following:
Change the return value of the home() method to be “duckranger”, save the HomeController class, wait a few seconds until you see the server has redeployed (It will echo INFO: Reloading Context with name [/GoodProject] is completed in the console) – and refresh the STS browser page – it should show the error:
Which makes sense, as there’s no duckranger.jsp page.
You can also try and change the prefix or suffix in servlet-context.xml (you may have to manually redeploy for this change to take effect, though). If you change the suffix from value=”.jsp” to value=”.duck” and try to access your application, you’ll find that the server complains.
In the server log (STS console) you’ll see the following:
WARN : org.springframework.web.servlet.PageNotFound – No mapping found for HTTP request with URI [/GoodProject/WEB-INF/views/home.duck] in DispatcherServlet with name ‘appServlet’
So, now that we’re satisfied that the view resolver indeed resolves views – there are only a couple of files left in the project – the log4j.xml and pom.xml configuration files. These are your maven build and log4j configuration files, which are not strictly a part of the Spring-MVC and are beyond the scope of this tutorial. We don’t care much for them right now.
Part I Summary
In this part you just went through the real basics of Spring MVC 3.0. Set up the STS development environment, created the template Spring MVC project, and investigated the files that got created.
Generally speaking, the control structure of a Spring-MVC application is quite simple:
- You start by hooking up Spring’s DispatcherServlet as the main entry point to your servlet container. This is done in web.xml, like you define any other servlet, with the exception of specifying a configuration file in DispatcherServlet’s init-param.
- The DispatcherServlet’s configuration file tells the Spring container where it can find its controllers, and configures a view resolver to handle the values returned from the controller methods, and resolve them into the right views to display.
- Once you have this sorted, all that’s left to get a simple application up and running is to create a simple controller and a simple view.
- The DispatcherServlet finds the controller by its @Controller annotation, and by it being a part of the relevant package configured with the component-scan element in its configuration file.
-
Using the @RequestMapping annotation, the DispatcherServlet knows which controller method it needs to call when a request comes in to the ‘/’ context.
The DispatcherServlet calls this method – (home() in the code above) – and gives it a Model to use for passing data to the view. - The home() method then returns a String – “home” – that’s used by the view resolver to resolve into a JSP page to render and return to the client.












That was a very great explanation/tutorial of the subject. I just started the whole Spring MVC/Maven/etc. nowadays, and I could say that this article is very handy. It contains all the information I need, not more nor less.
Great Tutorial. Thank you!
Is there any documentation that you know of that explains the use of the different Spring Template Projects. For instance if I wanted to persist an object that was updated through Spring MVC, how should that be accomplished (what is the best practice)? Should I add the capability to the MVC project that would allow persistence? How do I do that? Or should I create a JPA project that the MVC project will utilize to persist objects?
Thanks for your time!
@joe – thanks! glad to have helped.
@Tom – I am not aware of any such documentation (Granted, I didn’t look much..) – For what you want, I would have created an MVC template project and just add JPA to it.
I think it is possible to create a separate JPA project, but I don’t think this is necessarily best practice as such, More a flavour of how you want to manage your code.
I have seen many software projects where the persistence code is separated out to a stand-alone Eclipse project, and sometimes it makes source reuse and management easier, but for smaller projects I don’t think the trouble is worth your time. I hope I didn’t confuse you… bottom line – I think for future proofing your application – create a separate JPA template project, and use it in your MVC project.
Alternatively, you can create a new STS template that fits your needs. Have a look here
Cheers.
A great tutorial that actually works, unlike other tutorials I have tried on other websites. Thanks a million.
Good Job
Simple and clear explanation … waiting for the next part!
Very nice. Like IM – I’m looking forward to the next part, however, I won’t be waiting – I’ve got work to do! But it is well done and I appreciate it.
its simple and clear explanation….great job for beginners.:)
it is very clear explanation for starting spring project in STS..great job..
Probably the ebst spring web mvc tutorial I have read, explain everything in great detail. i loved it, this is like the 15th spring tutorial I have read, and it is by far the best one.
There is just one missing thing with the tutorial which will make it perfect. I followed the tutorial step by step and when I tried to run it on the server, it failed saying that “HTTP Status 404 – The requested resource () is not available.” I researched a little it seems to be a common problem. The solution will be to go to ‘Project’->’Clean…’ then choose this project to clean. Once cleaned, the project runs fine as suggested.
@Asif – good one – I’ll add it in. I didn’t get it ever – BTW. So I went and installed STS on a new machine – and actually got it!
Finally, the information between step 1 and 2, along with WHY. Thank you!
thanks for this good tutorial..
One great thing about this tutorial is that is a great start up point for MVC using STS.
Greak work !
Hi well done for the tutorial, but unfortunately, I am still having the error:“HTTP Status 404 – The requested resource () is not available.” I tried both solutions; the project clean and edit & save of home.jsp… but did not manage to fix it. will keep on searching, and keep you posted if I manage
After creating a sample template I was struggling to understand how whole thing works. This tutorial hit the nail right on the head. Very informative and nicely explained. Thanks a bunch !
Excellent tutorial! Short and to the point with the proper level of detail for a beginner. I will be assigning this to my team as a reference as we just inherited some spring mvc apps.
Thanks!
Great work with the tutorial. As for the 404 error stef was getting, have you checked the url? For me it was long as heck as default, pointing me to a wrong place. So after the localhost, just enter the project name.
Cheers.
@Amorphist – I actually haven’t seen these errors happen with long urls – do you have a copy of it by any chance? Maybe I can try and trace why it happened.
Sure,
http://localhost:8080/springMvcTest/WEB-INF/classes/net/mypackage/fi/HomeController.java
So yeah, I got it working simply by deleting all starting from web-inf.
This is bizzare. You got this simply by ‘run on server’? what version STS are you using?
Simply like that, yes. I’m running on STS version 2.9.0.
I am using STS 2.9 version, but while using the Spring MVC template and following the guidelines mentioned in the tutorial i am not able to run up my set up project.
I am getting the below error when my server starts
INFO: Initializing Spring FrameworkServlet ‘appServlet’
INFO : org.springframework.web.servlet.DispatcherServlet – FrameworkServlet ‘appServlet’: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext – Refreshing WebApplicationContext for namespace ‘appServlet-servlet’: startup date [Sun Jun 24 03:35:50 IST 2012]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader – Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
Jun 24, 2012 3:35:50 AM org.apache.catalina.loader.WebappClassLoader findResourceInternal
SEVERE: Resource read error: Could not load org.springframework.context.annotation.ComponentScanBeanDefinitionParser.
java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1200(ZipFile.java:31)
at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:459)
at java.util.zip.ZipFile$1.fill(ZipFile.java:242)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)
at org.apache.catalina.loader.WebappClassLoader.findResourceInternal(WebappClassLoader.java:3157)
at org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader.findResourceInternal(TomcatInstrumentableClassLoader.java:113)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2824)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1170)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
at org.springframework.context.config.ContextNamespaceHandler.init(ContextNamespaceHandler.java:37)
at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.resolve(DefaultNamespaceHandlerResolver.java:130)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1414)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1585)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
ERROR: org.springframework.web.servlet.DispatcherServlet – Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is org.springframework.beans.FatalBeanException: Invalid NamespaceHandler class [org.springframework.context.config.ContextNamespaceHandler] for namespace [http://www.springframework.org/schema/context]: problem with handler class file or dependent class; nested exception is java.lang.NoClassDefFoundError: or
Please help me so that I can started with using Spring.
@Naveen – this seems like a corrupt jar file (as java can’t unzip it) – can you delete spring-context jar and re-download it (if you’re following the tutorial then maven does it, so just remove it from the repository)
Thanks it worked for me as per your guidelines.
The maven repository was directed into default folder which was read only,
changed it to a new location and it worked…
As i did not had STS, i tried the tutorial using the STS plugin for eclipse. But, the project did not work there. Then i downloaded STS and created the project in it, and it worked fine.
I got the following error while creating the spring mvc project
Errors occurred during the build.
Errors running builder ‘Maven Project Builder’ on project ‘HelloWorld’.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
@inigoarulraj look here: http://stackoverflow.com/questions/8834806/m2eclipse-error
I’ve been reading tutorials on Spring for about a month now, and have yet to find one as good as this. cheers.
Great tutorial, wish I had seen this days ago!
As for the 404 error, when I try to run the jsp, I get the long url, and the error.
When I run the project, it shows the short url, and works fine.
Excellent for beginners!! thank you!!
It’s very useful for Beginners.I am expection next level of you like this same….
Great tutorial and well explained….
Millions Thank.
Oh man, we need more stuff like this!
Thanks.
Oh, look – the rss button.
Thank you Very much. I’m a trainee(My basic knowledge was J2SE only). Now I’m working on spring framework to build web services. It is really helpful. Thanks again….:-)
Hi,This tutorial is useful to understand the spring 3.1 MVC very easily. I started learning Spring latest version features and I am confusing where to start.So If possible could you please suggest me what are the advanced concepts and where to start. Thanks.
It is an excellent article/tutorial on Spring MVC for beginners. I am very thankful!
Highly recommend for Spring MVC beginner. very good tutorial which has cleared a lot of my question when I start to learn java Spring MVC. Thank !
nicely done. Head over heals better than the “Getting Started with Spring & SpringSource Tool Suite (STS)” video tutorial on spring’s site. This gets right to the point starting from the ground up explaining all the pieces in a clear manner.
Novice to spring, looked around for a week to get a good starting point, by far the best I got to understand spring MVC. Thanks a ton and a very a happy new year to you!!
Great tutorial. As many have said, best start to Spring MVC on the web. Thank you very much DR.
Duck,
It would be exactly what I need, if it worked, I think it’s maven configuration problem:
in java build path/lib: org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER: access rules: no rules, native library location: (None)
(would be great if gave a screenshot)
HomeController.java: org.slf4j and org.springframework – cannot be resolved.
Home.jsp: Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”
Spring installation was complete with no warning.
Thanks!
Duck,
Thanks so much, am learning spring mvc and this has helped a great deal.
stop calling yourself Duck, you are a serious guy
thanks so much again
Have eclipse juno, installed STS plugin from eclipse marketplace.
Was following your instructions i get an error when i select spring mvc project=> need to insgtall m2e.core. When i continue the template code is generated but with errors.
Then i installed maven plugin from eclipse market place, compile errors went away.
but when i ran the project on server (weblogic in my case) i get following error.
target\m2e-wtp\web-resources” does not exist.
Trace
=====
<Failure occurred in the execution of deployment request with ID "1364094455403" for task "0". Error is: "weblogic.application.ModuleException:
Any help is greatly appreciated.
@rvs – this would be a WTP issue. Look it up. From memory – you’ll need to disable maven generation of some resources.
Thanks for your response… resolved this issue (had the wrong maven plugin installed).
Not pretending i understand the root cause(uninstalled maven integration for eclipse, and installed maven integration for eclipse WTP). Someday when i get the basics correct, i will try to understand the difference in the 2 plugins.
I am running into a different problem now.
in a jsp i have
when running this i am getting an error
No tag library could be found with this URI. Possible causes could be that the URI is incorrect, or that there were errors during parsing of the .tld file.
Would greatly appreciate any pointers to resolve this(and if i have exceeded my allowable quota of questions i do apologize for that).
my jsp has
taglib prefix=”f” uri=”http://www.springframework.org/tags/form”
that line got deleted from my post above (special character filtering maybe)
@rvs – this could be a bug. can you change the jsp (even add a space somewhere) and save it again? This will trigger a refresh and may solve your issue
Thanks again… Tried it. Still not working. I am deploying on weblogic.
Not sure if that is a factor.