SpringMVC学习笔记上

来源:互联网 发布:思科路由器端口nat 编辑:程序博客网 时间:2024/06/05 16:30

SpringMVC介绍

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

让我们能非常简单的设计出干净的Web层和薄薄的Web层;
√进行更简洁的Web层的开发;
√天生与Spring框架集成(如IoC容器、AOP等);
√提供强大的约定大于配置的契约式编程支持;
√能简单的进行Web层的单元测试;
√支持灵活的URL到页面控制器的映射;
√非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用);
√非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;
√提供一套强大的JSP标签库,简化JSP开发;
√支持灵活的本地化、主题等解析;
√更加简单的异常处理;
√对静态资源的支持;
√支持Restful风格。

SpringMVC执行过程

SpringMVC快速入门 —案例驱动–用户展示列表

  • 1 导入jar包

  • 2 配置web.xml中servlet控制器

    <!-- 前端控制器 --><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:springmvc.xml</param-value>    </init-param></servlet><servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>*.action</url-pattern></servlet-mapping>
  • 3 创建springmvc.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: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.2.xsd     http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-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/aop     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><context:component-scan base-package="com.example.project.controller"></context:component-scan></bean>--注意--创建config文件夹时,是sourefolder
  • 4 创建User类,准备showUser.jsp

  • 5 创建UserController

    @Controller(value = "userController")public class UserController {    /**     *      * @return     */    @RequestMapping("/user_show")    public ModelAndView showUser() {        ArrayList<User> userList = new ArrayList<User>();        userList.add(new User(1, "张老三", "男", 18, new Date()));        userList.add(new User(1, "王老五", "男", 19, new Date()));        ModelAndView modelAndView = new ModelAndView();        modelAndView.addObject("userList", userList);        modelAndView.setViewName("showUser.jsp");        return modelAndView;    }}
  • 6 测试代码

SpringMVC架构流程

【流程图说明】1.用户发送请求至 前端控制器DispatcherServlet。2.前端控制器DispatcherServlet收到请求后调用处理器映射器HandlerMapping。3.处理器映射器HandlerMapping根据请求的Url找到具体的处理器,生成处理器对象Handler及处理器拦截器HandlerIntercepter(如果有则生成)一并返回给前端控制器DispatcherServlet。4.前端控制器DispatcherServlet通过处理器适配器HandlerAdapter调用处理器Controller。5.执行处理器(Controller,也叫后端控制器)6.处理器Controller执行完后返回ModelAnView。7.处理器映射器HandlerAdapter将处理器Controller执行返回的结果ModelAndView返回给前端控制器DispatcherServlet。8.前端控制器DispatcherServlet将ModelAnView传给视图解析器ViewResolver。9.视图解析器ViewResolver解析后返回具体的视图View。10.前端控制器DispatcherServlet对视图View进行渲染视图(即:将模型数据填充至视图中)11.前端控制器DispatcherServlet响应用户。

SpringMVC 重要组件说明

  • DispatcherServlet:前端控制器

用户请求到达前端控制器,它就相当于mvc模式中的c,
dispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。

  • HandlerMapping:处理器映射器

HandlerMapping负责根据用户请求找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等

  • Handler:处理器

Handler 是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。
由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler。

  • HandlAdapter:处理器适配器

通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。

  • View Resolver:视图解析器

View Resolver负责将处理结果生成View视图,
View Resolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,
再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。

  • View:视图

springmvc框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。我们最常用的视图就是jsp。

  • 三大组件

在springmvc的各个组件中,处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。
需要用户开放的组件有handler、view

配置三大组件

  • 为什么要配置?

如果没有显示的配置处理器映射器和处理器适配那么springMvc会去默认的dispatcherServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用,这样每个请求都要扫描一次他的默认配置文件,效率非常低,会降低访问速度,所以要显示的配置处理器映射器和

  • 注解形式的处理器映射器

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
  • 注解形式的处理器适配器

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
  • 配置最新版的注解的处理器映射器

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
  • 配置最新版的注解的处理器适配器

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> 
  • 为了保证不持续修改

    <!-- 配置注解方式处理器映射器 --><mvc:annotation-driven></mvc:annotation-driven>
  • 视图解析器

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 真正的页面路径 =  前缀 + 去掉后缀名的页面名称 + 后缀 --><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/"></property>    <!-- 后缀 --><property name="suffix" value=".jsp"></property></bean>

参数绑定

需求:使用表单上传用户信息

方式一:使用HttpServletRequet接收请求参数

  • 代码

    @RequestMapping("user_regist")public String regist(HttpServletRequest request) {        String username = request.getParameter("username");        System.out.println(username);        return "index.jsp";}
  • SpringMVC默认支持的参数类型

    HttpServletRequest通过request对象获取请求信息HttpServletResponse通过response处理响应信息HttpSession通过session对象得到session中存放的对象Model/ModelMap
  • 请求乱码解决方案:

    • get请求

      new String(request.getParamter(“userName”).getBytes(“ISO8859-1”),”utf-8”)

    • post请求

      使用拦截器方式<filter>    <filter-name>CharacterEncodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>        <param-name>encoding</param-name>        <param-value>utf-8</param-value>    </init-param></filter><filter-mapping>    <filter-name>CharacterEncodingFilter</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>
    • 最佳解决方案:

      修改tomcat配置文件添加编码与工程编码一致,如下:

      <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

方式二:使用基本数据类型直接封装方式

@RequestMapping("user_regist2")public String regist2(String username, String sex, Integer age, Model model) {        User user = new User(username, sex, age);        ArrayList<User> userList = new ArrayList<>();        userList.add(user);        model.addAttribute("userList", userList);        return "showUser.jsp";}

方式三:使用pojo方式进行参数封装

@RequestMapping("user_regist3")public String regist3(User user, Model model) {    ArrayList<User> userList = new ArrayList<>();    userList.add(user);    model.addAttribute("userList", userList);    return "showUser.jsp";}

方式四:使用VO方式进行数据封装

  • 表中字段写法

    <table align="center" cellpadding="10" border="1"    bordercolor="#ff0000">    <tr>        <td>用户名:</td>        <td><input type="text" name="user.username" id="username"></td>    </tr>    <tr>        <td>性别:</td>        <td><input type="text" name="user.sex" id="sex"></td>    </tr>    <tr>        <td>年龄:</td>        <td><input type="text" name="user.age" id="age"></td>    </tr>    <tr>        <td colspan="2"><input type="submit"></td>    </tr></table>
  • VO类

    public class UserVO {private User user;public User getUser() {    return user;}public void setUser(User user) {    this.user = user;}public UserVO() {    super();}}
  • 参数获取

    @RequestMapping("user_regist4")public String regist3(UserVO userVo, Model model) {    ArrayList<User> userList = new ArrayList<>();    userList.add(userVo.getUser());    model.addAttribute("userList", userList);    return "showUser.jsp";}

Controller配置细节

  • 限制请求路径—在类上限制请求路径

    @Controller(value = "userController")@RequestMapping(value="/user")public class UserController {请求时需要拼接 /user
  • 限制Http请求方法

    @RequestMapping(value="/showUser",method=RequestMethod.POST)
  • 返回值

    • ModelAndView

      指定视图,并且把model数据放到request域中。addObject(key,value)setViewName(逻辑视图名称)
    • Void

      如果返回值为void的时候,可以在controller方法形参上定义request和response,使用request或response指定响应结果。比如:转发 重定向 输出
    • String

      普通形式如:"showUser.jsp" 代表逻辑视图的名称默认转发 forward 如:"forward:index.jsp"重定向 redirect 如:"redirect:index.jsp"
  • 绝对路劲和相对路径

    相对路径时相对与当前的文件夹 /user/ success

    绝对路径,在当前应用程序名下 redirect:/suc/success.jsp

自定义类型转换器

  • 创建自定义类型转换器

    public class DateConverter implements Converter<String, Date> {@Overridepublic Date convert(String str) {    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    try {        return dateFormat.parse(str);    } catch (ParseException e) {        e.printStackTrace();    }    return null;}}
  • springmvc文件中配置类型转换器

    <!-- 配置注解的处理器映射器和适配器 --><!-- 默认支持参数绑定的组件:如json数据 --><mvc:annotation-driven conversion-service="conversionService" /><!-- 配置自定以参数转换器 --><bean id="conversionService"    class="org.springframework.format.support.FormattingConversionServiceFactoryB   ean">    <property name="converters">        <list>            <bean class="com.example.project.controller.convert.DateConverter" />        </list>    </property></bean>

ssm整合

MyBatis

  • 导入 db.properties
  • 导入 log4j.properties
  • 配置SqlMapConfig.xml

        <?xml version="1.0" encoding="UTF-8"?>    <!DOCTYPE configuration    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    "http://mybatis.org/dtd/mybatis-3-config.dtd">    <configuration>        <!-- 批量设置别名 -->        <typeAliases>            <package name="com.jiyun.ssm.pojo" />        </typeAliases>    </configuration>
  • 配置dao层配置文件 applicationContext-dao.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: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.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-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/aop         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">    <!-- 加载java配置文件 -->    <context:property-placeholder location="classpath:db.properties" />    <!-- 配置数据源 -->    <!-- 配置数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        destroy-method="close">        <property name="driverClass" value="${db.driver}" />        <property name="jdbcUrl" value="${db.url}" />        <property name="user" value="${db.user}" />        <property name="password" value="${db.password}" />    </bean>    <!-- 配置sqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 配置mybatis的全局配置文件 -->        <property name="configLocation" value="classpath:SqlMapConfig.xml" />        <!-- 配置数据源 -->        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- 扫描需要生产代理对象的mapper -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 需要扫描的mapper的包 -->        <property name="basePackage" value="com.jiyun.ssm.dao" />    </bean></beans>
  • 配置Service层文件

配置Service扫描包

配置事务管理器

配置事务切点

    <?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.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-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/aop         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">    <!-- 通过扫描组件,扫描servcie的bean包 -->    <context:component-scan base-package="com.jiyun.ssm.service" />    <!-- 事务管理器 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 配置数据源 -->        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 配置通知 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <!-- 配置传播特性 -->        <tx:attributes>            <!-- 配置传播特性 -->            <tx:method name="*" propagation="REQUIRED" />            <tx:method name="find*" read-only="true" />            <tx:method name="query*" read-only="true" />            <tx:method name="select*" read-only="true" />            <tx:method name="get*" read-only="true" />        </tx:attributes>    </tx:advice>    <!-- 配置AOP -->    <aop:config>        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jiyun.ssm.service.*.*(..))"/>    </aop:config></beans>

SpringMVC整合


  • 配置文件

组件扫描器扫描controller的bean所在包
配置注解的处理器映射器和适配器

视图解析器

    <?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.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-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/aop         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">    <!-- 组件扫描器扫描controller的bean所在包 -->    <context:component-scan base-package="com.jiyun.ssm.controller" />    <!-- 配置注解的处理器映射器和适配器 -->    <mvc:annotation-driven />    <!-- 视图解析器 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean>    </beans>

web.xml配置

springm配置文件路径

监听器

前端控制器

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    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">    <!-- 配置springmvc的配置文件路径 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext-*.xml</param-value>    </context-param>    <!-- 配置监听器 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 配置前端控制器 -->    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置springmvc的配置文件路径 -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <!-- 支持restful -->        <url-pattern>*.action</url-pattern>    </servlet-mapping></web-app>
原创粉丝点击