Spring3 MVC 介绍:

1. Spring MVC 是Spring 框架的Web组件,能够开发WEB工程

2. 能与其它框架(Struts2)很好的集成

3.  Spring MVC 是以servlet为中心,通过DispatcherServlet把请求分发给控制器

4.  DispatcherServlet 是Spring IOC容器的完全集成,能使用Spring其他功能

5.  Spring3 MVC 支持注解技术

6. Spring3 能很好的支持JAP2.0

那接下来我们就写一个例子:

1.准备所需工具和jar包:

(1). JDK 1.7

(2). apache-tomcat-7.0.52

(3). Eclipse -JavaEE 版本的

(4). 所需要jar:

commons-logging-1.1.1.jar

jstl-1.2.jar

org.springframework.asm-3.0.0.RELEASE.jar

org.springframework.beans-3.0.0.RELEASE.jar

org.springframework.context-3.0.0.RELEASE.jar

org.springframework.core-3.0.0.RELEASE.jar

org.springframework.expression-3.0.0.RELEASE.jar

org.springframework.web-3.0.0.RELEASE.jar

org.springframework.web.servlet-3.0.0.RELEASE.jar

  jar下载地址:http://pan.baidu.com/s/1eQvXlom

2.创建一个动态web工程(Dynamic Web Project),并选择服务器,选择servlet的版本(2.5)

3.把所需jar拷贝到WebContent---WEB - INF> lib文件夹中

4.Spring控制器类

创建一个Spring MVC的一个控制类,并处理请求,打印一句话‘Spring MVC示例 ’,那我们先创建包com.li.controller,然后在这个包下面创建一个类HelloController.java,在这个类中加入代码。在HelloController类中注明@Controller和@RequestMapping("/test")。@Controller:当spring扫描包的时候,将表示为处理请求的一个Bean。@RequestMapping("/test"):应该处理请求URL地址

 package com.li.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloController { @RequestMapping("/test")
public ModelAndView test(){
String str="spring MVC 示例";
return new ModelAndView("message","str",str);
}
}

HelloController.java

5.创建JSP

创建一个JSP发出请求:index.jsp

创建一个JSP显示消息:message.jsp

用index.jsp里面的超链接发出一个请求到HelloController,并返回到message.jsp 显示str的信息

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test.do">Spring MVC 示例</a>
</body>
</html>

index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${str}
</body>
</html>

message.jsp

6.Spring MVC的映射Web.xml中

主要定义:org.springframework.web.servlet.DispatcherServlet

需要在工程里面web.xml配置文件中加入下面的配置:

   <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>

7.Spring配置文件

注意上一个步骤中的<servlet-name>标签在web.xml中的servlet的名称。

DispatcherServlet的初始化后,会在WEB - INF查找一个文件名[servlet-name]-servlet.xml

在这个示例中,将应该查找spring-servlet.xml

在WEB - INF下面创建一个Spring的配置文件,文件名为:spring-servlet.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.li.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

spring-servlet.xml

源码下载:http://pan.baidu.com/s/1bnwJs8R

原文链接:http://jingyan.baidu.com/article/c843ea0b7f8b7777931e4ae8.html

Spring3 MVC入门示例的更多相关文章

  1. Spring MVC 入门示例讲解

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

  2. Spring MVC 入门示例讲解 - howtodoinjava

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

  3. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  4. 【转】spring MVC入门示例(hello world demo)

    部分内容来自网络:<第二章 Spring MVC入门 —— 跟开涛学SpringMVC > 1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web M ...

  5. spring mvc 入门示例

    classpath <?xml version="1.0" encoding="UTF-8"?> <classpath> <cla ...

  6. NHibernate with ASP.NET MVC 入门示例

    目的:初步了解NHibernate的用法,包括数据库的CRUD, 基于ASP.NET MVC 项目模板 步骤: 创建ASP.NET MVC 新项目 使用NuGet引入FluentNHibernate ...

  7. Spring MVC入门示例

    1.新建一个Java Web项目 2.导入jar包 3.在WEB-INF下面建一个hello.jsp页面. <%@ page language="java" import=& ...

  8. Spring MVC入门示例(1)

    1.新建一个Java Web项目 2.导入jar包 3.在WEB-INF下面建一个hello.jsp页面. 1 <%@ page language="java" import ...

  9. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

随机推荐

  1. spl_autoload_register装在函数的正确写法

    AutoLoading\loading <?php namespace AutoLoading; class Loadind { public static function autoload( ...

  2. NEWS - InstallShield 2013 SP1发布

    2013的这个国庆假期期间,InstallShield厂商Flexerasoftware(中文名:福莱睿)发布了最新版本InstallShield 2013的SP1,由于这个升级包带来一些新的技术支持 ...

  3. MySQL查询及删除重复记录的方法

    查询及删除重复记录的方法(一)1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select p ...

  4. [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽

    作者:zyl910 以往我们想在spring的xml配置文件中增加说明文本时,只能使用xml注释(<!-- 注释 -->).这对于"调试.部署时想批量屏蔽部分bean" ...

  5. pip 安装psycopg的错误

    psycopg包安装有点问题,特别是在windows下,pip从requirements.txt批量安装总是出错,发现是这个包的问题. 这里需要用easy_install来装,因为gfw的问题,最好下 ...

  6. IOS 7 UI 的适配

    官方问题答案:https://developer.apple.com/library/ios/qa/qa1797/_index.html 官方建议: https://developer.apple.c ...

  7. count有关

    1.count有两个作用:统计某个字段有值的记录数:统计结果集的记录数.2.count括号内的表达式不为null,就是统计结果集的记录数.也就是说,count(1),count(*),count(10 ...

  8. WPF 创建桌面快捷方式

    #region 创建桌面快捷方式 string deskTop = System.Environment.GetFolderPath(System.Environment.SpecialFolder. ...

  9. 深入剖析 redis 主从复制

    主从概述 redis 支持 master-slave(主从)模式,redis server 可以设置为另一个 redis server 的主机(从机),从机定期从主机拿数据.特殊的,一个 从机同样可以 ...

  10. RabbitMQ学习笔记4-使用fanout交换器

    fanout交换器会把发送给它的所有消息发送给绑定在它上面的队列,起到广播一样的效果. 本里使用实际业务中常见的例子, 订单系统:创建订单,然后发送一个事件消息 积分系统:发送订单的积分奖励 短信平台 ...