Home >> Spring Framework
In this Page I am going to show and describe an example
using SpringFramework's Web and MVC related functionality.
To start with I am not going to write any custom class or POJOs
and not use any annotations, but I am going to use Spring's
own implementations for URL handling/mapping and Controller.
I assume that you as a reader have already know besics about
Spring core, web modules and know how to use these.
In this example/page I am not going to explain / provide
definitions for all these components/modules.
Here I am going to use some of the modules/JARs from
SpringFramework as follows:
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.aspects-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
These JAR files along with some other files placed in the
WEB-INF/lib folder, as follows:
commons-logging-1.1.1.jar
log4j-1.2.15.jar
There are two main configuration/descriptor files such as
"web.xml" and "
example-web-controller-servlet.xml"
The section in bold red in the file name is same as that
of the entry in web.xml file as follows:
/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>example-web-controller</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example-web-controller</servlet-name>
<url-pattern>*.go</url-pattern>
</servlet-mapping>
</web-app>
|
As you might have noticed that the web.xml file is having
declaration for the web app as web-app_2_5.xsd, so I have tested
this example on Tomcat web server version 6.0.18.
So the web.xml descriptor file is having a Spring's DispatcherServlet
configured to be load on web server startup.
Spring Framework requires a *-servlet.xml file for automatically
using this file for context related operations. So this file
example-web-controller-servlet.xml is having some tags used
such as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<bean id="exampleController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="exampleHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="test.go" value="exampleController"/>
</map>
</property>
</bean>
</beans>
|
As mentioned above, I am going to use
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping as
handler mapping implementation and
org.springframework.web.servlet.mvc.UrlFilenameViewController as
controller.
The URL that I used to test this application is :
http://localhost:8080/example-spring-web/test.go
After completing the above example, I moved on to a slightly
different approach, whereby I created a custom/user defined
controller by implementing Spring's Controller interface
and providing implementation for the abstract handleRequest
method, as shown below:
ExampleController.java
package example.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ExampleController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return new ModelAndView("/test.jsp");
}
}
|
It does nothing apart from just returning the ModelAndView
instance along with the destination JSP file.
Now there is a little bit of change in the *-servlet.xml file as well:
example-web-controller-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="exampleController"
class="example.controller.ExampleController">
</bean>
<bean id="exampleHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="test.go" value="exampleController"/>
</map>
</property>
</bean>
</beans>
|
These changes works pretty the same way it is expected to.
On exploring the following URL, it is observed to show the output
from the test.jsp file as expected.
http://localhost:8080/example-spring-web/test.go
Then I moved onto another phase of this example by exploring
some other ways of making this example work. I managed to do
this by changing/using some other type of handler mapping from
SpringFramework WEB module.
I used
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
As per the official documentation from Spring, this handler mapping
automatically picks up bean that is defined with a name matching with
that of the URI, for this example, the bean that is required to be defined
with a name as "/test.go" and the class I used as that of the
ExampleController, as shown in the following section:
example-web-controller-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="exampleHandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<bean name="/test.go" class="example.controller.ExampleController"/>
</beans>
|
http://localhost:8080/example-spring-mvc/test.go
So the URL has the URI as /test.go, and it is mapped to the ExampleController.
Thus ExampleController returns the view as "/test.jsp".
Hope this helps.
If anything missed out , please let me know at
techienjoy at yahoo . com