SpringMVC实战(一)-快速入门

Spring MVC是一个轻量级的MVC框架,它是Spring框架最重要的的模块之一,它以强大的Spring IoC容器为基础,并充分利用容器的特性来简化它的配置。本文中我们将使用Spring MVC框架构建一个入门级web应用程序。

开发环境

  • JDK 1.7+
  • Eclipse Mars
  • Apache Tomcat 8.0.33

简单示例

本文使用Spring MVC 4.2.5.RELEASE 来创建Spring官网提供的Greeting示例demo。
1、创建Maven web project
在Eclipse中新建Maven Project,在archetype类型中,选择“maven-archetype-webapp“。
SpringMVC实战(一)-快速入门_第1张图片

2、配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.ricky.codelabgroupId>
    <artifactId>webappartifactId>
    <packaging>warpackaging>
    <version>0.0.1version>

    <name>webapp demoname>
    <url>http://maven.apache.orgurl>

    <properties>
        <java.version>1.8java.version>
        <spring.version>4.2.5.RELEASEspring.version>
        <maven.compiler.version>3.1maven.compiler.version>
        <junit.version>4.12junit.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>${junit.version}version>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>${maven.compiler.version}version>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <testSource>${java.version}testSource>
                    <testTarget>${java.version}testTarget>
                configuration>
            plugin>
        plugins>
    build>
project>

3、web.xml
在WEB-INF目录下新建web.xml,内容如下:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Servlet 3.0 Web Application</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

4、springmvc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    
    <context:component-scan base-package="com.ricky.codelab.webapp.controller" />

    
    <mvc:default-servlet-handler />

    
    <mvc:annotation-driven />

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

beans>

5、新建Controller
新建一个Java类命名为GreetingController.java

package com.ricky.codelab.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}

6、创建/WEB-INF/jsp/greeting.jsp
在/WEB-INF/下面创建jsp目录,然后新建greeting.jsp


<html>
<head>
    <title>Getting Started: Serving Web Contenttitle>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
head>
<body>
    <p>Hello, ${name}!p>
body>
html>

7、部署项目
选中webapp 项目,鼠标右键 Run As -> Run on server,如下图:
SpringMVC实战(一)-快速入门_第2张图片

点击Finish 按钮 开始部署。

8、验证结果
在浏览器中,输入访问的URL:http://localhost:8080/webapp/greeting?name=Ricky,显示Hello Ricky!则表示成功啦。

你可能感兴趣的:(Spring,MVC)