SpringMvc的使用(一):
一:SpringMvc都做了什么:
Struts2可以完成的功能SpringMvc都可以完成。
- Controller为中心完成对系统流程的控制管理,和struts中的action类似
- 从请求中搜集数据 ,struts2中一般使用模型驱动Bean来收集数据。
- 对传入的参数进行验证 表单等都要校验。我们一般分为客户端校验和服务端校验。一般对格式的校验,数据类型的校验,或者是日期格式的校验,字数的校验,一般使用客户端校验,使用正则表达式加上js。这里SpringMvc所指的校验是服务端的校验,在java代码中的校验。实际上,SpringMvc服务端的校验使用的并不多。其实正真涉及到服务端的校验,我们一般使用AJAX。如:邮箱的注册,我需要做一个校验,看我注册的用户名是否已经被注册,必须向数据库中,做一次校验,校验是否已经被注册。
- 将结果返回给视图 ,从服务端查询出一个结果集,需要将结果集展示在页面上。,
- 针对不同的视图提供不同的解决方案 ,不仅可以支持jsp还可以支持很多其他的视图
- 针对jsp视图技术提供标签库 ,struts中也提供了标签库。基本上每一种框架都有自己的标签库,这样其实有弊端,如果公司中想使用一些新的技术,那么标签库也要重新学习,这样就不利于学习,我们一般都会使用jstl标签。Jstl标签在公司中式大量使用的。
- 拦截器 ,做权限的拦截,拦截器是面向切面的一种编程。
- 上传文件
二:spring-mvc结构
下面的组件都是非常重要的,必须全部掌握,这里的组件有的是基于配置文件开发,有的是基于注解开发。
1:DispatcherServlet:中央控制器(中央转发器),负责拦截,如以.action或者以.do为结尾的,都会被这个组件先拦截到,然后将其分配到具体的Controller的类,然后调用类中的方法。和struts中的StrutsPrepareAndExecuteFilter类似,把请求给转发到具体的控制类。这里不管我是使用配置文件开发,还是使用注解开发,这个组件都要配置。
2:Controller:具体处理请求的控制器(配置文件方式需要配置,注解方式不用配置),和struts中的action类似。
3:handlerMapping:映射处理器,负责映射中央处理器(DispatcherServlet)转发给controller时的映射策略。如何将请求映射到Controller上的,由这个组件决定。使用配置文件开发有映射处理器,使用注解开发,是没有这个组件的使用的,我们会使用注解来替代它的功能。
4:ModelAndView:服务层返回的数据和视图层的封装类,这个类是Spring为我们提供的(无论是配置文件还是注解都不需要配置)
5:ViewResolver & View:视图解析器,解析具体的视图,不管是配置文件开发,还是注解开发,这个组件都需要。可以将ModelAndView组件解析成正真的视图。
6:Interceptors :拦截器,负责拦截我们定义的请求然后做处理工作(无论是配置文件方式还是注解都需要先创建再配置)
红色的是需要自己创建,黑色的需要配置,黑色的SpringMvc已经提供好了的,但是我要在配置文件中配置。
三:SpringMvc组件的运行流程:
1:请求过来,首先会被中央控制器: DispatcherServlet拦截到,但是请求要有一些标识,不能随便的请求都可以拦截到 ,如请求以.do为结尾或者请求以.action为结尾的就拦截到。
2:通过映射处理器,将请求映射到具体的controller上。
3:controller然后访问后台逻辑。就会将结果集,以及要跳转的页面封装成一个ModerAntView(SpringMvc提供的对象)
4:通过ViewResolver将ModerAndView解析成真正的页面
四:SpringMvc的使用:
注:使用框架,我们肯定是需要引入依赖包的,其实SpringMvc是Spring的东西。不需要去寻找下载与SpringMvc相关的依赖包,因为是没有的。我们需要去下载Spring,Spring中会包含SpringMvc的依赖包。
SpringMvc的开发支持配置文件的方式和注解的方式,注解方式使用的更多,配置文件方式使用的不多。
第一步:创建WEB工程:
第二步:引入依赖包到WEB项目的lib目录中:
第三步:在web.xml配置文件中配置DispatcherServlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置servlet -->
<servlet>
<!-- 配置servlt的name -->
<servlet-name>springmvc</servlet-name>
<!-- 配置servlet的全路径 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- 配置Servlet的映射 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 配置servlet要拦截的请求,不要使用/*,使用*.xxx,规范是:*.do,我们最好使用*.do -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
第四步:创建controller:
第五步:配置controller:
在SpringMvc的配置文件中配置controller:
SpringMvc的配置文件有一个默认的命名规则:
1:建立的位置在web工程的WEB-INF之中。
2:
3:SpringMvc的头信息,我们使用pring的头信息就可以了:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
</beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 配置controller的Bean -->
<bean class="com.lb.controller.TestController" name="/hello.do"></bean>
<!-- 上面,我虽然将Controller的Bean配置好了,但是依然不知道如何访问Contorller。
这里的访问有handlerMapping来决定,我们有一种默认的映射处理器,不用配置就可以使用,
这种默认的映射处理器叫做BeanNameUrlHandleMapping,这里的意思的Controller的Bean的name的url的映射处理器。
也就是说通过Bean的名字来做映射,这个名字要以正斜杠开头,然后结尾要以.do为结尾,因为中央控制器拦截的是以.do为结尾的请求
现在Contorller就配置好了,只要我访问name,就会寻找相应的Contorller类。
-->
</beans>
第六步:配置视图解析器:
现在我Contorller配置好之后,整个流程是这个样子的:
当我请求以hello.do为结尾的时候,就会被中央控制器拦截到,发现是以name来做映射的,就会根据name将请求转发到一个具体的Contorller上面,之后就会执行Contorller中的handleRequestInternal方法,执行完逻辑之后,就会组装ModelAndView。
<!-- 配置视图解析器,通过InternalResourceViewResolver这个类
来解析ModelAndView。
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀和后缀:有时后很多的jsp放在比较深的目录下面,有些目录是公用的,
公用的,每次都写的话,其实是一种重复。我们通过视图解析器配置好之后,来为我们加上这些重复的东西。
我们的jsp一般放在web-inf中,这样受保护,安全一点,value="/WEB-INF/jsp/":这段路径就是公共的路径。
-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- jsp文件,都是以.jsp为结尾的,这也是公共的,所以我们可以通过后缀来配置 -->
<property name="suffix" value=".jsp"></property>
<!-- 配置前缀和后缀无非就是来为我拼jsp文件的路径,减少我们的重复 -->
</bean>
第七步:Contorller中页面路径的指定:
/*
* 创建一个类来作为controller,这个类需要继承AbstractController,
* 并且重写handleRequestInternal方法,这个方法返回的是ModelAndView组件。
* 这里有 两个参数:HttpServletRequest,HttpServletResponse,和servlet很相似
*/
public class TestController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
/*
* return new ModelAndView(viewName);这个构造器中只有一个参数,有时候我们访问后台之后,不需要返回结果,直接跳转页面就可以了。
* 这里的参数的jsp页面的路径,或者jsp页面的名字
* return new ModelAndView(viewName, model);这种构造器,第二个参数是,封装的数据
*/
return new ModelAndView("index");
/*
* 现在我的视图解析器已经配置好了,并且视图解析器中,前缀和后缀都已经配置好了,
* 所以当前的ModelAndView中,我只需要指定前缀和后缀中间的路径,视图解析器会自动的来为当前的视图加上前缀和后缀,
* 来确定要跳转的视图的路径,就可以找到相应的页面,并且跳转
*/
}
Jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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>
欢迎使用SpringMvc框架<br>
</body>
第八步:测试: