Simple Spring MVC application
I was hearing about Spring MVC application since quite a time time now , recently i had the fortune to have a go at it .At first what looked too be a too horrendous application resolved into one of the simplest app that i have coded in recent times .The files involved for getting a bare bone MVC application working are as follows :
- web.xml
- dispatcher-servlet.xml
- HelloController.java(controller)
- index.jsp,mypage.jsp
- Library files as usual
Lets start with each component step by step :
1.web.xml
This file contains the as usual the servlet stuff and the mapping stuff required for forwarding to any particular servlet .The intercept in the web.xml file for my app is as follows :
<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>*.htm</url-pattern>
</servlet-mapping><welcome-file-list>
<welcome-file>
/WEB-INF/pages/index.jsp
</welcome-file>
</welcome-file-list>
Well this file explains itself and states necessity of the .htm extension for forwarding to the servlet .
2.dispatcher-servlet.xml
Spring by default looks for this file under the WEB-INF directory , so this lies in parallel to the web.xml file . This file contains the controller names to which any particular request would be mapped based upon the url .This file also lists the view resolvers which are helpful in mapping and forwarding the views from the controller .The main intercept of a basic dispatcher-servlet.xml file is as follows :
<bean id=”viewResolver”>
<property name=”prefix”>
<value>/WEB-INF/pages/</value>
</property><property name=”suffix”>
<value>.jsp</value>
</property>
</bean>
<bean name=”/normal.htm”/>
3.HelloController.java(controller)
This is the main part of the application , this is where your java code exist .Controller performs the business logic (if any or delegates it further) and forward to a view (ideally ViewResolver which further renders view).The intercept for a bare bone controller is as follows :
package com.mvcapp.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;public class HelloController extends AbstractController {
private String message;@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
Thread.sleep(500);
return new ModelAndView(“mypage”,”mymessage”, message);
}public void setMessage(String message) {
this.message=message;
}}
In this file the final return statement hints toward the view that we intend to render . The first parameter “mypage” is the view name and the ViewResolver would resolve it to “/WEB-INF/pages/mypage.jsp” with help of the definition in the in the dispatcher-servlet.xml file .The second parameter “mymessge” is the view parameter and can be accessed on the view .
4. index.jsp,mypage.jsp
These are the jsp’s that i have used for the demonstation purposes . mypage.jsp is the final jsp that you are rendering through your controller , while index.jsp is the jsp file use for redirecting to your controller url .The intercept for the index.jsp file is as follows :
<a href=”normal.htm”>Click here for normal dispatcher</a><br/>
The pages of course need to be placed under “WEB-INF/pages” directory .
5. Library files
I m not too sure about the exact library files required , but the ones that i required are as follows :
- spring.jar
- spring-webmvc.jar
- spring-web.jar
- spring-core.jar
- spring-context.jar
- spring-context-support.jar
- spring-beans.jar
- spring-aop.jar
- commons-logging-1.1.jar
This is the minimum that you are required to have to get your spring MVC app working (you could have also used annotations , but this did the trick for me ) .
