Spring3 MVC入门示例

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

 1 package com.li.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller
 8 public class HelloController {
 9     
10     @RequestMapping("/test")
11     public ModelAndView test(){
12         String str="spring MVC 示例";
13         return new ModelAndView("message","str",str);
14     }
15 }
HelloController.java

5.创建JSP

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

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

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

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <a href="test.do">Spring MVC 示例</a>
11 </body>
12 </html>
index.jsp
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 ${str} 
11 </body>
12 </html>
message.jsp

6.Spring MVC的映射Web.xml中

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

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

 1   <servlet>
 2       <servlet-name>spring</servlet-name>
 3       <servlet-class>
 4           org.springframework.web.servlet.DispatcherServlet
 5       </servlet-class>
 6       <load-on-startup>1</load-on-startup>
 7   </servlet>
 8   <servlet-mapping>
 9       <servlet-name>spring</servlet-name>
10       <url-pattern>/</url-pattern>
11   </servlet-mapping>
View Code

7.Spring配置文件

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

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

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

在WEB - INF下面创建一个Spring的配置文件,文件名为:spring-servlet.xml,并把下面的内容复制到文件中:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation=" 
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 7         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
10     default-autowire="byName">
11     <context:component-scan base-package="com.li.controller"/>
12     <bean id="viewResolver"
13         class="org.springframework.web.servlet.view.UrlBasedViewResolver">
14         <property name="viewClass"
15             value="org.springframework.web.servlet.view.JstlView"/>
16         <property name="prefix" value="/"/>
17         <property name="suffix" value=".jsp"/>
18     </bean>
19 </beans>
spring-servlet.xml

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

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

原文地址:https://www.cnblogs.com/pwenlee/p/4624207.html