In this article, I am building hello world application using spring 3 (mvc) framework. Follow the given steps and learn the basics. I have used JSTL for writing the view.
Setup requirements

  • Maven
  • Eclipse juno
  • Tomcat 7

Step 1) Create blank project in eclipse workspace using maven. For this, open command prompt and make current working directory to eclipse workspace. Now execute below command.

$mvn archetype:generate -DgroupId=com.howtodoinjava.mvc -DartifactId=firstSpringApplication
-DarchetypeartifactId=maven-archetype-webapp -DinteractiveMode=false

Now run below given commands. This will convert the created project into eclipse web project.

cd firstSpringApplication

mvn eclipse:eclipse -Dwtpversion=2.0

Step 2) Update maven project dependencies. For this, update the pom.xml file.

<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>

<!-- Spring 3 dependencies -->
<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupid>javax.servlet</groupid>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupid>taglibs</groupid>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
    <scope>runtime</scope>
</dependency>

Now execute below command again. This will download all dependencies to maven local repository and include them in project build.

mvn eclipse:eclipse -Dwtpversion=2.0

Step 3) Add servlet handler. For this, you will need to write this file (spring-mvc-servlet.xml) inside WEB-INF folder. You can name it at your will, but keep remember that it must match the servlet name you will declare in web.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="com.howtodoinjava.web" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

Step 4) Update web.xml for servlet mapping and spring config location.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>www.howtodoinjava.com</display-name>

  <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

Step 5) Add the request handler. This request handler is written in java. It will receive all requests to a particular URL and return the model data to view handler configured.

package com.howtodoinjava.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/access")
public class DemoController
{
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model)
    {
        model.addAttribute("message", "Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com");
        return "helloWorld";
    }
}

Step 6) Create the view. Our view is a jsp file, which will be rendered in browser when related view will be returned from server.

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>

<html>
    <head>
        <title>My first example using Spring 3 MVC</title>
    </head>
    <body>
        <h1>Welcome message : <c:out value="${message}"></c:out></h1>
    </body>
</html>

Step 7) Run the application. To run the application, configure tomcat in eclipse juno. Now, right click on project and choose run as. On sub menu, choose Run on server.

After this, the application should be deployed in tomcat instance.

Now type on browser: http://localhost:8080/firstSpringApplication/access

You should be able to see the message “Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com” in browser.

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐