Introduction:
Spring is an open source light weight framework for developing Java Application.
Spring provide many modules like, Beans, context, application, Core, Data access, Spring MVC etc.
For creating simple Java web application we use Spring MVC(Model View Controller) module.
Here we will see how to configure spring hello world application using eclipse.
Requirements:
1. Eclipse
2. Tomcat server
3. JDK_1.5.0 or later version
4. Spring jars(these are spring 3 jars you can have spring 4 jars also)
slf4j-api-1.1.0-beta0.jar
spring-asm-3.0.1.RELEASE.jar
spring-context-3.0.1.RELEASE.jar
spring-beans-3.0.1.RELEASE.jar
spring-core-3.0.1.RELEASE.jar
spring-expression-3.0.1.RELEASE.jar
spring-instrument-3.0.1.RELEASE.jar
spring-oxm-3.0.1.RELEASE.jar
spring-web-3.0.1.RELEASE.jar
spring-webmvc-3.0.1.RELEASE.jar
You can download these here
Project structure will be like this image in eclipse
Follow the given steps for creating simple application in Eclipse.
Step1:
Open Eclipse>File>New>Dynamic web project>'enter name of project'
click on 'Finish' button
Here my project name is FirstSpring
Step2:
Now create index.html file inside WebContent like this
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="hello.html">Click Here</a> |
</body>
</html>
Step3:
Now create web.xml file inside WEB-INF folder like this
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FirstSpring</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
</web-app>
If you noticed we are using DispatcherServlet in web.xml, this is the front end controller of the spring
which is responsible for handling coming request and displaying response in UI, This controller provided by spring only.
Step4:
Create one spring-servlet.xml file inside WEB-INF folder for view resolver and spring basic configuration like this.
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tcs.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
here we are using InternalResourceViewResolver class for view resolver or in other word we can say DispatcherServlet try to find
output .jsp file or .html to display response.
prefix and suffix is the getter setter of InternalResourceViewResolver class, here prefix is used to resolved folder name and
suffix is used to resolved file extension.
Step5:
Now create jsp folder inside WEB-INF folder and create hellopage.jsp file inside jsp folder for displaying response like this.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
message is: ${message}
</body>
</html>
here $ work like getAttribute in servlet which set by controller in java file
and 'message' is the output string which is set by the controller once we create controller class then you will see.
Step6:
Now finally we are creating 'HelloWorldController.java' for testing something like this
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public ModelAndView helloWorld()
{
String message="Hello you are in spring" ;
return new ModelAndView("hellopage", "message", message);
}
}
Here we are using some annotation like
'@controller' when you use this annotation with class, That class will treat as as controller class in spring.
DispatcherServlet will map your request into this class.
'@RequestMapping' this is use for request mapping which will come from client in our case request is coming from index.html.
if you noticed that in index.html when you click on 'hello spring' internally request will go as 'hello' in controller class.
'RequestMethod.GET' this indicate your coming request is using http GET method
'ModelAndView' this a class which we are returning as a response, This class provide a constructor which has three parameters.
first parameters is your output jsp page which will be inside jsp folder as I discussed in step5
second parameter is output string where you are setting your output which will be retrieve 'hellopage.jsp'
Third parameter is your actual output which is what you want to return.
Here I am returning String message="Hey you are in spring application";
Step7: RUN APPLICATION
Now we have done all configuration its time to start server and right click in your project and select run as> run on server
Output will like this
you will able to see hello.html page with 'Hello Spring' link now click on this link you will get output like this
This was very basic spring MVC application which can give you basic idea about spring configuration.
Thanks for reading this tutorial.
No comments:
Post a Comment