1.Spring MVC

1)什么是Spring MVC
Spring MVC是Spring框架中一个模块,实现MVC结构,便于简单,快速开发MVC结构的WEB应用,Spring MVC提供的API封装WEB开发中常用的功能,简化WEB开发的过程

2)Spring MVC 的核心组件
DispatcherServlet (控制器,请求入口)
HandlerMapping (控制器,请求派发)
Controller (控制器,请求处理)
ModelAndView (封装业务处理结果和跳转视图)
ViewResolver (视图显示处理器)

3)Spring MVC的处理流程
浏览器向服务器发送请求 -> 请求交给前端控制器DispatcherServlet -> 前端控制器通过HandlerMapping找到相对应的Controller组件处理请求,执行Contriller组件的约定方法,在约定方法中调用模型层组件来完成业务处理,约定方法返回一个ModeAndView对象,此对象封装处理结果和跳转的视图名称,前端控制器接收到ModelAndView对象之后,调用ViewResolver组件定位View(JSP)传递数据信息,生成相应页面。

2.基于XML配置的MVC应用

搭建Spring MVC环境
创建WEB工程,导入Spring MVC相关开发包
Spring ioc,web,webmvc开发包
在src下添加Spring XML配置文件
名称可以自定义,例如spring-mvc.xml
在web.xml中配置DispacherServlet前端控制器
配置DispacherServlet,同时指定XML配置文件的路径

Controller组件负责执行具体业务处理,编写时需要实现Controller接口及约定方法handleRequest

handleRequest方法返回一个ModelAndView对象,此对象封装处理结果数据和跳转的视图名称

ModelAndView(String viewName)

ModelAndView(String viewName,Model model)

viewName是视图名称,model是处理的结果数据

HandleMapping(是接口)组件,映射请求URL和请求处理器Controller组件对应关系

SimpleUrlHandlerMapping(实现接口)维护一个个的HTTP请求和Controller映射关系列表(Map),根据列表映射对应关系调用Controller

ViewResolver(是接口)组件,对ModelAndView对象封装的视图名称进行解析

InternalResourceViewResolver(实现接口的子类),它支持Servlet和jsp及子类jstlView响应

  <!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup><!-- 启动服务器的时候加载配置,设置优先级 --> </servlet> <servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern> <!-- 以这个结尾的请求才会走前端控制器 -->
</servlet-mapping>

3.基于注解配置的MVC应用

Controller注解应用
推荐使用@Controller注解声明Controller组件,这样就不需要控制器组件实现Controller接口,不需要约定方法
好处:使得控制器定义更加灵活,可以不用去实现Controller接口,请求处理方法可以灵活定义

为了使用使@Controller注解生效,需要在Spring的XML配置文件中开启组件扫描定义
<context:component-scan base-pack=""/>

RequestMapping注解应用
@RequestMapping注解可以用在类定义前和方法定义上,表明此组件类的方法与哪一个请求对应

为了使@ResquestMapping注解生效,需要在Spring的XML配置文件中开启MVC注解扫描
<mvc:annotation-driven/>

4.接收请求参数

Spring MVC请求提交数据到控制器有以下方式
1)使用HttpServletRequest获取
Spring框架自动参数注入到HttpServletRequest中
优点:直接通过HttpServletRequest的getParameter()方法
缺点:需要自己处理数据类型的转换

2)使用@RequestParam注解
Spring会自动将参数注入到方法参数(请求参数名和处理方法的变量名 名称一致)
使用@RequestParam注解映射不一致的名称
优点:参数类型自动转换,但可能出现类型转换异常

3)使用自动封装成Bean对象
定义实体类,属性名必须与请求参数名相同

package com.xms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.xms.entity.User; @Controller
@RequestMapping("/test")
public class TestController { //HttpServletRequest接受请求参数
@RequestMapping("/testOne.do")
public ModelAndView testOne(HttpServletRequest request){ String username=request.getParameter("username");
String password=request.getParameter("password"); System.out.println(username);
System.out.println(password); return new ModelAndView("hello");
} //方法参数接受请求参数(@RequestParam注解可以解决方法参数名和请求参数名不一致的情况)
@RequestMapping("/testTwo")
public ModelAndView testTwo(@RequestParam("username")String username,@RequestParam("password")String pwd){
System.out.println(username);
System.out.println(pwd);
return new ModelAndView("hello"); } //对象接收参数
@RequestMapping("/testThree")
public ModelAndView testThree(User user){
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return new ModelAndView("hello");
} }
    <h1>表单</h1>
<form action="/SpringMybatisDay03_03/test/testThree.do" method="post" >
账号:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" value="提交"> </form>
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" > <context:component-scan base-package="com.xms"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
public class User {

    private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

5.向页面传值
当Controller组件处理后,需要向jsp传值的方法
1)直接使用HttpServletRequest或HttpSession

2)使用ModelAndView对象

3)使用ModelMap参数对象
在Controller处理方法中追加一个ModelMap类型的参数

注意:数据会利用HttpServletRequest的attitude属性传递到页面

SpringMyBatisDay03的更多相关文章

随机推荐

  1. Makefile Demo案例

    # Comments can be written like this. # File should be named Makefile and then can be run as `make &l ...

  2. ActiveMQ 消息持久化到数据库(Mysql、SQL Server、Oracle、DB2等)

    ActiveMQ具体就不介绍了,直接介绍如何讲ActiveMQ持久化到本地数据库,以SQL Server 2008 R2为例1.下载ActiveMQ后直接解压,我下载的是apache-activemq ...

  3. php 函数合并 array_merge 与 + 的区别

    array_merge()是PHP语言中的一个函数,作用是将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组. 如果输入的数组中有相同的字符串键名,该键的键值为最 ...

  4. [Sdoi2016]齿轮

    4602: [Sdoi2016]齿轮 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 613  Solved: 324 [Submit][Status ...

  5. Linux下识别所有Android设备的方法

    修改/etc/udev/rules.d/51-android.rules文件. 方法一: 参考Google文档 SUBSYSTEM=="usb", ATTR{idVendor}== ...

  6. 8.28 jQuery

    2018-8-28 18:25:27 jQuery 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html 2018-8-28 19:53:19 还是讲 ...

  7. poj1182 食物链【并查集-好题!】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.  现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两 ...

  8. 金字塔原理——MECE(Mutually Exclusive Collectively Exhaustive)

    一.金字塔原理 它的样子: 一个中心思想,分出下面2到N个思想支撑,每个分论点下面又有2到N个思想(事实或数据)支撑,以此类推,形状如金字塔.原则是以终为始(先结果后原因),以上统下,归纳分组,逻辑递 ...

  9. LightOJ 1030 - Discovering Gold - [概率DP]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1030 You are in a cave, a long cave! The cave can be repr ...

  10. intel笔记本cpu型号后缀详解(M,U,QM,MQ,HQ,XM)

    M:笔记本专用CPU,一般为双核,M前面一位数字是0,意味着是标准电压处理器,如果是7,则是低电压处理器. U:笔记本专用低电压CPU,一般为双核,U前面一位数字为8,则是28W功耗的低压处理器(标准 ...