Spring学习总结六——SpringMVC一
一:什么是springMVC
MVC,顾名思义,m就是model模型,包括处理业务、访问数据库以及封装数据实体类等,view视图层
负责和用户进行交互,就是展示给用户的部分,包括jsp页面,html等,c就是控制器,是controller层。
大致的访问流程是:
1:浏览器发送请求到服务器,然后服务器将请求分配给dispatcher分发器(在web.xml中配置)
2:然后dispatcher分发器调用handlerMapping将前台请求地址与controller控制器进行匹配,这里就要
用到handlerMapping组件
3:controller控制层调用handlerRequest方法处理请求,当然包括业务以及与数据库交互的部分,然后将
处理结果以及页面封装到ModelAndView对象中返回给核心分发器
4:然后分发器调用viewResolver组件将modelandview中返回的视图名称按照viewResolver进行拼接,得到
访问的视图,然后将数据填充到视图中
5:将jsp处理结果返回到浏览器展示
二:示例演示
下面通过示例来演示:
1:第一步配置环境,在lib中导入如下jar包

2:配置spring容器spring-mvc.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: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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 定义请求处理映射 -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 指定请求与controller对应关系 -->
<property name="mappings" ref="urlMappings"/>
</bean> <!--定义映射关系 -->
<util:properties id="urlMappings">
<prop key="/hello.form">helloController</prop>
</util:properties> <bean id="helloController" class="com.hlcui.controller.HelloController"></bean> <!-- 定义viewResolver解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3:配置web.xml,dispatcher控制器在这里配置
<?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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定配置文件的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4:HelloController类
/**
*
*/
package com.hlcui.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; /**
* @author Administrator
*
*/
public class HelloController implements Controller { public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("处理业务逻辑!");
ModelAndView model = new ModelAndView("hello");
return model;
} }
5:hello.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</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">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
This is my JSP page. <br>
</body>
</html>
6:启动服务器,然后通过浏览器访问

访问成功!
三:使用注解的方式对案例进行重构
1:在HelloController类中,可以使用@Controller注解在类上,替代实现Controller接口,需要在
spring容器中配置组件扫描:
<!-- 组件扫描 -->
<context:component-scan base-package="com.hlcui.controller"/>
2:在类上或者方法上使用@RequestMapping注解替换在spring容器中配置urlMapping映射bean
需要在spring容器中配置注解扫描:
<!-- RequestMapping映射配置 -->
<mvc:annotation-driven/>
3:修改后的类以及配置文件如下
<!-- 组件扫描 -->
<context:component-scan base-package="com.hlcui.controller"/> <!-- RequestMapping映射配置 -->
<mvc:annotation-driven/>
@Controller
@RequestMapping("/web")
public class HelloController { @RequestMapping("/hello.form")
public String execute(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("处理业务逻辑!");
return "hello";
} }
4:启动服务器,使用浏览器进行访问

访问成功!
Spring学习总结六——SpringMVC一的更多相关文章
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- spring学习四:springMVC
ref:http://www.cnblogs.com/ysocean/tag/SpringMVC%E5%85%A5%E9%97%A8%E7%B3%BB%E5%88%97/ Spring MVC的处理流 ...
- MyEclipse Spring 学习总结三 SpringMVC 表单处理
SpringMVC 表单处理的项目结构如下图所示: Student.java 文件 public class Student { private Integer age; private String ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- spring学习 十六 spring加载属性文件
第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...
- Spring学习笔记六:Spring整合Hibernate
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html 前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...
- Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)
在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...
- Spring学习(六)——集成memcached客户端
memcached是高性能的分布式内存缓存服务器.许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现RDBMS的负担加重.数据 ...
- Spring学习(六)—— Spring注解(二)
核心原理 1. 用户发送请求给服务器.url:user.do 2. 服务器收到请求.发现Dispatchservlet可以处理.于是调用DispatchServlet. 3. ...
随机推荐
- Java中LinkedList实现原理
数据结构 LinkedList是基于链表结构实现,所以在LinkedList类中包含了first和last两个指针(类型为Node).Node中包含了对prev节点.next节点的引用,这样就构成了双 ...
- STM32f103的数电采集电路的双ADC的设计与使用
STM32F103C8T6拥有3个ADC,其独立使用已经在本文的3.1.3里面有详细的介绍,这里主要是介绍双ADC的同时使用,即STM32的同步规则模式使用.在此模式在规则通道组上执行时,外部触发来自 ...
- NLP-训练个model出来写诗
2018年新年,腾讯整出来个ai春联很吸引眼球,刚好有个需求让我看下能不能训出来个model来写出诗经一样的文风,求助了下小伙伴,直接丢过来2个github,原话是: 查了一下诗经一共38000个字, ...
- Git 提交更改
Jerry 已经提交的更改,他想纠正他的最后一次提交,在这种情况下,git 的修改将帮助操作.最后提交修改操作的变化,包括提交信息,它创建新的提交ID. 修改操作之前,他会检查提交日志. [jerry ...
- Map与实体之间转换
package com.thunisoft.maybee.engine.utils; import java.lang.reflect.Field; import java.lang.reflect. ...
- JSP之应用Servlet过滤器进行身份验证
1.Servlet过滤器的作用描述(1)在HttpServletRequest到达Servlet 之前,拦截客户的HttpServletRequest. 根据需要检查HttpServletReques ...
- linux进程的挂起和恢复
进程的挂起及恢复 #ctrl+z:挂起,程序放到后台,程序没有结束. #jobs:查看被挂起的程序工作号 恢复进程执行时,有两种选择:fg命令将挂起的作业放回到前台执行:用bg命令将挂起的作业放到后台 ...
- 关于Unity中的本地存储
本地存储 在做游戏的时候,经常需要在本机存储一些数据,比如闯关类游戏要记录闯到第几关,做单机的时候要把数据保存到本地,下次启动的时候数据存在,就是把数据保存到磁盘里面或者手机的flash闪存里面. U ...
- IDEA中maven项目导jar包太慢
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/PROGRAM_anywhere/article/details/53842058参考了网上的一些教程 ...
- java的GUI程序的基本思路是以JFrame为基础
JFrame – java的GUI程序的基本思路是以JFrame为基础,它是屏幕上window的对象,能够最大化.最小化.关闭. JPanel – Java图形用户界面(GUI)工具包swing中的面 ...