1.用Eclipse创建一个工程,命名为spring2.0 并添加相应的jar包(我用的是4.0.5的版本)到 lib 包下:

spring-webmvc-4.0.5.RELEASE.jar

spring-web-4.0.5.RELEASE.jar

commons-logging-1.1.jar

spring-aop-4.0.5.RELEASE.jar

spring-context-4.0.5.RELEASE.jar

2.在web.xml中配置好DispatcherServlet。让spring mvc的小心脏跳动起来

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<!-- 默认为applicationContext.xml -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springMVC</servlet-name>
<!-- DispatcherServlet 会自动加载/wef-inf/下的springMVC-servlet.xml -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern><!-- 意思是拦截所有请求 -->
</servlet-mapping>
<!-- 配置spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

3.在 WebContent/WEB-INF 下创建两个配置文件

applicationContext.xml(暂时没有用处,但是为了方便程序启动可以先添加)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
</beans>

springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 实现支持注解的IOC 这个将会在web层容器启动时自动扫面src下的package v19961996-->
<context:component-scan base-package="v19961996" />
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

4.在src下创建一个package , 命名为v19961996

在v19961996下创建一个类 , 命名为 HelloWorldController

HelloWorldController.java

@Controller
@RequestMapping("/Hello")
public class HelloWorldController {
@RequestMapping("/World") //url将映射到这里
public ModelAndView SayHi() {
System.out.println("这是一个springmvc小例子");
return new ModelAndView("HelloWorld");
}
}

注解为@Controller是为了让Spring IOC容器初始化时自动扫描到;@RequestMapping是为了映射请求路径,因为类与方法上都有映射所以访问时应该是/Hello/World

5.在WebContent下创建视图

HelloWorld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HelloWorld</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
</head>
<body>
HelloWorld!<br>
</body>
</html>

项目图:

6.启动Tomcat,输入 http://localhost:8080/spring2.0/Hello/World

spring-mvc-两个个小例子的更多相关文章

  1. spring+spring mvc+JdbcTemplate 入门小例子

    大家使用这个入门时候 最好能够去 搜一下 spring mvc 的 原理,我放一张图到这里,自己琢磨下,后面去学习就容易了 给个链接 (网上一把,千万不能懒)    https://www.cnblo ...

  2. vuex2.0+两个小例子

    首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各核心概念的理解. 废话少说,直接上干货.这是官网上的一 ...

  3. Vuex2.0边学边记+两个小例子

    最近在研究Vuex2.0,搞了好几天终于有点头绪了. 首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各 ...

  4. 初始Spring MVC——练手小项目

    初始Spring MVC 前几天开始了我的spring学习之旅,由于之前使用过MVC模式来做项目,所以我先下手的是 Spring MVC,做个练手项目,非常简单 项目介绍: 用户输入信息 -> ...

  5. Spring MVC的Hello World例子

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-mvc-hello-world-example.html: ...

  6. php操作redis的两个个小脚本

    redis这东西,查询起来没有mysql那么方便,只能自己写脚本了.下面是工作中写的两个小脚本 第一个脚本,查找有lottery|的键,将他们全部删除|打印出来 <?php $redis = n ...

  7. libconfig第二篇----两个小例子

    本文只看粗体即可,太多catch语句.两个例子均来自libconfig包的example文件夹下面,. 例子一: #include <iostream> #include <ioma ...

  8. 学习HttpClient,从两个小例子开始

    前言 HTTP(Hyper-Text Transfer Protocol,超文本传输协议)在如今的互联网也许是最重要的协议,我们每天做的很多事情都与之有关,比如,网上购物.刷博客.看新闻等.偶尔你的上 ...

  9. Spring MVC简单的HelloWorld例子

    1.web.xml配置(主要配置Servlet)[默认情况 Spring的配置文件在WEB-INF的<servlet-name>-servlet.xml] <?xml version ...

随机推荐

  1. [CSS3] 学习笔记-HTML与CSS简单页面效果实例

    一个简单的首页的设计: html文件: <!doctype html> <html> <head> <meta charset="UTF-8&quo ...

  2. CentOS7 ssh无密码登录

    准备工作:给各个主机取个名字,如master(主节点),slave01(从节点01),slave02(从节点02) 1.修改主机名: hostname master hostname slave01 ...

  3. [UWP]依赖属性1:概述

    1. 概述 依赖属性(DependencyProperty)是UWP的核心概念,它是有DependencyObject提供的一种特殊的属性.由于UWP的几乎所有UI元素都是集成于DependencyO ...

  4. Github+Hexo搭建静态博客

    开始 在安装hexo之前,必须确认你已经安装了Node.js和Git,并且注册了一个Github账号. 1.创建Github仓库 1) 仓库名为xxx.github.io 创建一个以"用户名 ...

  5. C++学习的心路历程之心理障碍

    断断续续的C++学习已经过了1年多了,可是,我还是没有迈出可以自如输出写点什么的那一步.甚至我因为这个老是怀疑自己的智商,我是真心想学懂,因为这个关系到我的就业,直接关系到我的饭碗.我是十分的着急,可 ...

  6. php curl详细解析和常见大坑

    1. 拿来先试试手 比如我们以著名的"测试网络是否连接"的网站--百度为例,来尝试下curl <?php // create curl resource $ch = curl ...

  7. Masonry的简单使用

    #import "RootViewController.h" #import "Masonry.h" @interface RootViewController ...

  8. matlab中同一文件定义子函数的方法

    在matlab中一个.m文件中可以有多个的子函数,但仅能有一个主函数,并且M文件名必须和主函数相同在一个m文件中通常有两种定义子函数的方法: 1.嵌套定义 myfunc1会和主函数共享变量名.这种情况 ...

  9. WebSockets介绍

    Web sockets定义为在servers和clients之间的双向连接.意味着servers和clients可以同时交流并发送数据.这种协议是从底层就是双工连接.Web sockets技术上得到了 ...

  10. ArcGIS许可启动问题

    前段时间,由于360常常删除重要文件终于发生在我身上.不得已换了电脑管家,清理后再次打开License Server Administrator时,发现启动项怎么也点不动了.而打开服务管理器,却发现A ...