Dears,

This blog is moved to a new blog at:

The new update will be found at that new blog, please follow it!
This blog is no more updated.
Thanks for your visit!

Monday, October 7, 2013

HelloWorld - a Web application based on Spring framework

Objective
We will learn how to create a simple Web application with Spring framework with Eclipse. The Web application has two JSP pages:
- hello.jsp: It displays a welcome phrase to Spring framework!

Web application creation with Eclipse
  - Step 1: start the Eclipse J2EE environment, click on New project -> Dynamic Web project



- Step 2: type the project name as spring-helloworld, then click Finish

- Step 3: create folders, files in project directory as follow:
 - in the src folder, create a package control. In the package control, create a java class of name HelloControl.
 - in the WebContent folder, create a JSP page: hello.jsp.
- in the WebContent/WEB-INF/ folder, create two files: web.xml and spring-helloworld-servlet.xml



 - Step 4: Download all Spring library files (from: http://sourceforge.net/projects/springframework/) and then add them into the WebContent/WEB-INF/lib folder:



 - Step 5: define all java classes, jsp files and .xml files

 HelloControl.java
 package control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
public class HelloControl{

   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Web Framework!");

      return "hello";
   }
}

 hello.jsp
 <%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

 web.xml
 <web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Web Application</display-name>
   <servlet>
      <servlet-name>spring-helloworld</servlet-name>
      <servlet-class>  org.springframework.web.servlet.DispatcherServlet </servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-helloworld-servlet.xml</param-value>
        </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>spring-helloworld</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping> 
</web-app>

spring-helloworld-servlet.xml
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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-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="control" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

 Results

 

No comments:

Post a Comment