Spring学习(七)-Spring MVC 入门示例

来源:互联网 发布:第四套人民币淘宝 编辑:程序博客网 时间:2024/06/06 09:01

Spring MVC属于Spring FrameWork的后续产品。Spring MVC基于模型-视图-控制器(Model-View-Controller,MVC)模式实现。能够帮助我们构建像Spring框架那样灵活和松耦合的Web应用程序。

1、Spring MVC处理请求流程图
这里写图片描述

(1)请求报文携带用户所请求的内容信息,比如请求的URL。请求到达容器后,容器会把请求报文送达Spring MVC的前端控制器DispatchServlet。至此,客户端或浏览器和服务器间的第一阶段交互结束。

(2)DispatchServlet的主要任务是将请求发送给Spring MVC的控制器Controller。但是,在典型的应用程序中,可能会有许多个控制器。而对于DispatchServlet需要为不同URL信息映射到对应的Controller上。DispatchServlet会查询一个或者多个处理器映射来确定映射到哪一个处理器。

(3)通过(2)选中具体的Controller后,DispatchServlet会把请求报文送到Controller,并等待该Controller的处理。

(4)控制器在完成自身的业务逻辑后,产生一些信息,并将这些信息封装成模型model。最后,控制器将模型数据打包,并标示出用于渲染输出的视图名。然后再将模型和视图名发送Dispatchservlet.

(5)Dispatchservlet使用视图解析器将控制器送达的逻辑视图名转换为一个特定的视图,比如特定jsp。

(6)视图的具体实现,并交付模型数据。

(7)视图使用模型数据渲染输出,输出内容将会通过响应对象传递给客户端。

可以看到,请求经过许多步骤,最终才能产生返回给客户端的响应,其中除了请求和应答客户端会和服务端交互,其他步骤均是在Spring内部完成的。在详解Spring MVC之前,我们先来看一下如何搭建一个Spring MVC入门级项目。

2、Spring MVC入门示例

本项目通过注解的方式实现,项目的整体组织架构如下:
这里写图片描述

(1)在WebContent\WEB-INF下添加web.xml,并在web.xml配置初始化配置文件spring-context.xml的路径。

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     <!-- 注册DispatchServlet -->      <servlet>        <servlet-name>appServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <!-- 设置根上下文配置文件位置 -->          <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-context.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>     <!-- 将DispatchServlet映射到"/","/"表示请求URL是否加后缀都可以 -->      <servlet-mapping>        <servlet-name>appServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

(2)在src\下添加Spring上下文配置文件spring-context.xml

<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns:beans="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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">     <!-- 自动扫描的包名,完成Bean的创建和自动装配 -->      <context:component-scan base-package="com.wygu" ></context:component-scan>      <!-- 默认的注解映射的支持 -->      <mvc:annotation-driven />     <!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->    <beans:bean        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />    <beans:bean        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />    <!--试图解析类 -->     <beans:bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <beans:property name="prefix" value="/WEB-INF/jsp/" />        <beans:property name="suffix" value=".jsp" />    </beans:bean></beans:beans>

(3)在WebContent\WEB-INF\jsp\添加register.jsp,show.jsp,实现注册和显示

1) register.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>Add User From</title></head><body>    <form action="show" method="post">        <fieldset>        <legend>注册新用户</legend>            <p>                <label>姓名:</label> <input type="text" id="name" name="name"                    tabindex="1">            </p>            <p>                <label>邮箱:</label> <input type="text" id="eMail" name="eMail"                    tabindex="2">            </p>            <p>                <label>手机号:</label> <input type="text" id="mobileNum" name="mobileNum"                    tabindex="3">            </p>            <p>                <label>密码:</label> <input type="password" id="passWard" name="passWard"                    tabindex="4">            </p>            <p                 id="buttons">                <input id="reset" type="reset" tabindex="5" value="取消"> <input                       id="submit" type="submit" tabindex="6" value="注册">            </p>        </fieldset>    </form></body></html>

2) show.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>    <div id="gloobal">        <h4>注册成功</h4>        <p>         姓名:${user.name}<br /> 电子邮件:${user.eMail}<br /> 手机号:${user.mobileNum}<br /> 密码:${user.passWard}<br />        </p>    </div></body></html>

(4)新建model类User

package com.wygu.model;public class User {    private String name;    private String eMail;    private String mobileNum;    private String passWard;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String geteMail() {        return eMail;    }    public void seteMail(String eMail) {        this.eMail = eMail;    }    public String getMobileNum() {        return mobileNum;    }    public void setMobileNum(String mobileNum) {        this.mobileNum = mobileNum;    }    public String getPassWard() {        return passWard;    }    public void setPassWard(String passWard) {        this.passWard = passWard;    }}

(5)创建UserController

package com.wygu.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import com.wygu.model.User;@Controllerpublic class UserController {    @RequestMapping("register")    public String register(Model model) {        return "register";    }    @RequestMapping("/show")    public String show(@ModelAttribute("form") User user, Model model) {                    model.addAttribute("user", user);        return "show";    }}

启动应用程序后,运行如下:
这里写图片描述

点击注册:
这里写图片描述

显示结果为:
这里写图片描述

后续博客会给出Spring MVC的详解,持之以恒!

原创粉丝点击