Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Hi all,
in this article i ll show u how to write a simple Spring MVC application printing 'Hello World' after we
submit a form.
ok lets have a look over the directory structure that i used to build the application
WEB-INF : This directory holds all the tlds,*.jar files and *.xml files
lib : This directory holds all the jar files that are necessary for running this application.Just copy the
spring.jar,commons-logging.jar and servlet.jar files into this lib directory.
Now lets have a look over all these xml files that are required to configure the application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
The servlet specified here is DispatcherServlet, the same as ActionServlet we use for Struts.
Hava a look over the servlet name, the sping config xml files must start with hello as like this 'helloservlet.
xml' .
hello-servlet.xml
This xml file specifies the bean which should be invoked for the action passed from index.jsp.
Here we are setting the greeting property with 'Hello World', this is the message that will be viewed on
success.jsp page .
The view resolver bean specifies the mapping details for the passed reference from the controller. When
used InternalResourceViewResolver class it check for the files with the name that matches when
returning the ModelAndView object.
This also includes which type of files to check out, Here we specified to check over all *.jsp files
matching the parameter that is passed from the controller.
applicationContext.xml
<beans>
</beans>
as we are not writing any service logic beans this can be kept empty.
The only class we specified here is HelloController in the src directory with package
'com.springmvc.action' .
HelloController.java
package com.springmvc.action;
import javax.servlet.http.*;
import org.springframework.web.servlet.mvc.*;
import org.springframework.web.servlet.*;
public class HelloController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws Exception{
return new ModelAndView("success","message",greeting);
}
private String greeting;
public void setGreeting(String greeting){
this.greeting = greeting;
}
}
This class implements the main Interface Controller, which is Spring's MVC main Interface .
The ModelAndView class is the one which is used to bind model and view . The object passed would be
including the mapping path for the view,
and the greeting variable is binded with 'messages' attribute. This attribute is set into the request
object.
So thats it, now u have all the java code stuff, all the .xml files and .jsp files.
just build ur application using ant. You can see two directorys created 'dist' and 'war' as shown below
Now just restart your tomcat and type the url ' http://localhost:8080/springmvc/ '
you should be able to see the screen as such if the application runs fine..
and when submitted the form on click the button, we get should see the 'Hello World' as such in
success.jsp
Just have a look over the java code and xml files after running the application.
Detailed explanation is not given here as this is just a basic application and can be understood by the
ones who had an overview of all
Struts MVC Framework.
Ok thats all with this example. I just did this one in order to have a pratice over all of this framework. If
at all you get any errors, just copy the exception thrown and search in google, I tried the same way and
atlast i got the result. If all the JAR files are placed properly you wouldnt get any error.
ok thnx folks for reading my post,
Regards,
Rohit Tayal
tayalr@gmail.com