欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring MVC框架入门(外加SSM整合)

程序员文章站 2022-08-30 08:45:43
spring MVC框架 一、什么是spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spr ......

spring MVC框架

一、什么是spring MVC

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

                                                                   ---------百度百科

  从spring官网中可以看出,Spring MVC原名叫Spring Web MVC,它是构建在Servlet API上的最初的Web框架,从一开始就包含在Spring框架中。正式名称“Spring Web MVC”来自其源模块spring-webmvc的名称, 但它通常被称为“Spring MVC”。Spring web mvcStruts2都属于表现层的框架,它是Spring框架的一部分。

二、spring MVC框架的作用

  从请求中接收传入的参数,将处理后的结果返回给页面展示

三、spring MVC执行流程

  spring MVC框架入门(外加SSM整合)

1)、 用户发送请求至前端控制器DispatcherServlet

2)、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。

3)、 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。

4)、 DispatcherServlet通过HandlerAdapter处理器适配器调用处理器

5)、 执行处理器(Controller,也叫后端控制器)。

6)、 Controller执行完成返回ModelAndView

7)、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet

8)、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器

9)、 ViewReslover解析后返回具体View

10)   DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。

11)   DispatcherServlet响应用户

四、快速开发

需求:显示商品列表

1、导入所需基本依赖jar包

 jar包下载地址:springMVC_jar

spring MVC框架入门(外加SSM整合)

2、在项目工程上创建源码包,用来存放配置文件

3、创建spring MVC核心配置文件,文件名就叫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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">         
</beans>

4、创建日志文件,用于打印日志(log4j.properties)

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5、定义Controller类

  ItemController是一个普通的java类,不需要实现任何接口,只需要在类上添加@Controller注解即可。@RequestMapping注解指定请求的url,其中“.action”可以加     也可以不加。注意:1.这里的配置@Controller注解 2.@RequestMapping用于识别域名后缀 3.modelAndView.setViewName用于设置跳转页面

// 在添加注解的同时,还得配置扫描
@Controller
public class ItemsController {
    //指定url到请求方法的映射
    //url中输入一个地址,例如:localhost:8888/SpringMvc/list.action
    //用以替代了struts中采用的配置文件进行匹配来调用那个方法从而识别跳转那个页面
   @RequestMapping("/list")   
   public ModelAndView itemsList()throws Exception{
       List<Items> itemList = new ArrayList<Items>();
        
        //商品列表
        Items items_1 = new Items();
        items_1.setName("联想笔记本_3");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
        
        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6苹果手机!");
        
        itemList.add(items_1);
        itemList.add(items_2);
        //模型视图
        //model模型:模型对象中存放了返回给页面的数据
        //view视图:视图对象中指定给返回的页面的位置
        //创建modelandView对象
        ModelAndView modelAndView = new ModelAndView();
        //添加model(将返回给页面的数据放入模型和视图对象中)
        modelAndView.addObject("itemList", itemList);
        //添加视图(指定给 返回页面的位置)
        modelAndView.setViewName("jsp/itemList.jsp");
        return modelAndView;
    }
   }

 

6、在SpringMvc.xml中配置注解扫描

  这里controller类是创建在cn.clj.controller包下的

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <!-- 配置@Controller注解扫描 -->
        <context:component-scan base-package="cn.clj.controller"/>
         
</beans>

7、在web.xml中配置前端控制器

  注意:指定核心配置文件名不能写错,否则会找不到Controller类

<!-- springMvc前端控制器 -->
  <servlet>
     <servlet-name>springMvc</servlet-name>
     <!--路径:spring-webmvc-4.1.3.RELEASE.jar\org.springframework.web.servlet  -->
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 如果没有指定springMvc核心配置文件那么默认会去找/WEB_INF/+<servlet-name>的内容+  -servlet.xml配置文件 -->
     <!-- 指定springMvc核心配置文件位置 -->
     <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:SpringMvc.xml</param-value>
     </init-param>
     <!-- tomcat启动时就加载这个servlet -->
     <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
     <servlet-name>springMvc</servlet-name>
     <url-pattern>*.action</url-pattern>
  </servlet-mapping>

  DispatcherServlet的路径为:

 spring MVC框架入门(外加SSM整合)

8、配置jsp

 在WebRoot下创建jsp文件夹,用来存放jsp

  1.需引入jstl标签   2.因为传的是itemList,接收值不能写错

<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<body> <form action="${pageContext.request.contextPath }/search.action" method="post"> 查询条件: <table width="100%" border=1> <tr> <!-- 如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用--> <td>商品名称:<input type="text" name="items.name"/></td> <td>商品价格:<input type="text" name="items.price"/></td> <td><input type="submit" value="查询"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名称</td> <td>商品价格</td> <td>生产日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemList}" var="item"> <tr> <td>${item.name}</td> <td>${item.price}</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail}</td> <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form>

9、测试

  此时在浏览器中输入http://localhost:8080/项目名/list.action,如果成功跳转到显示页面为成功

二、关于注解处理器映射器和注解处理器适配器

  注解式处理器映射器注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method

  注解式处理器映射器:注解式处理器适配器,对标记@ResquestMapping的方法进行适配。

  方式一:手动配置最新版本的映射器和适配器(缺点:随着版本更新的重新配置)

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

  方式二:自动配置

<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

  全部代码如下

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <!-- 配置@Controller注解扫描 -->
        <context:component-scan base-package="cn.clj.controller"/>
        
        <!-- 如果没有显示配置处理器映射器和处理器适配器那个springMvc会默认的dispatcherServlet.properties中查找
        对应的处理器映射器和处理器适配器去使用,每个请求都要扫描一次的默认配置文件,效率非常低,会降低访问速度,所以显示的配置处理器映射器和处理器适配器
         -->
         <!-- 注解形式的处理器适配器
         <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->
         <!-- 注解形式的处理器映射器 
         <bean class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver"/>-->
         <!-- 配置最新版的注解的处理器映射器,以上已经过时
         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
         <!-- 配置最新版的注解的处理器适配器 
         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
         <!-- 注解驱动:能够自动配置最新版的处理器映射器和处理器适配器 -->
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
       
</beans>

 

三、关于视图解析器

1.分析情形

 在controller中,每次配置跳转页面时,都要配置跳转视图的全部路径,有点麻烦

2、配置视图解析器

 功能:在配置文件中配置全局跳转视图的前缀名和后缀名,在controller类只要写省去后缀的jsp名即可,配置如下:

 1)在SpringMvc.xml文件中配置视图解析器

      <!-- 配置视图解析器 -->
         <!-- 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称,直接写去掉后缀的页面名 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <!--真正页面路径=前缀+页面名称+后缀  -->
             <!-- 跳转视图前缀 -->
             <property name="prefix" value="/jsp/"></property>
             <!-- 跳转视图后缀 -->
             <property name="suffix" value=".jsp"></property>
      </bean>

 2)更改conroller类中的写法

        //添加视图(指定给 返回页面的位置)
       // modelAndView.setViewName("jsp/itemList.jsp");
        modelAndView.setViewName("itemList");    
        return modelAndView;

四、SSM整合

      个人认为,SpringMvc与Mybatis整合其实就是SSM整合,因为Spring与SpringMvc同属于一家公司,无需整合,当然也需要用到Spring的IOC特性业务分配:此时控制层交给SpringMvc,持久层交给MyBatis,创建管理交给Spring

思路:

Dao层:

1、SqlMapConfig.xml,空文件即可。需要文件头。

2、applicationContext-dao.xml。

a) 数据库连接池

b) SqlSessionFactory对象,需要spring和mybatis整合包下的。

c) 配置mapper文件扫描器。

Service层:

1、applicationContext-service.xml包扫描器,扫描@service注解的类。

2、applicationContext-trans.xml配置事务。

表现层:

Springmvc.xml

1、包扫描器,扫描@Controller注解的类。

2、配置注解驱动。

3、视图解析器

Web.xml

配置前端控制器。

1、快速部署环境

 1)导入相应的依赖jar包

此包含Mybatis依赖jar包与逆向工程依赖jar包、Spring依赖jar包与Spring-mybatis整合包、SpringMVc依赖包,数据库驱动包,第三方连接池

 spring MVC框架入门(外加SSM整合)

   spring MVC框架入门(外加SSM整合)

 2)在工程项目下(非src)创建源码包,用来存放配置文件,包名为config

 3)创建分层包,采用MVC模式开发,每个包的业务不同

 spring MVC框架入门(外加SSM整合)

 4)创建db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.174.132:3306/SSM
jdbc.username=root
jdbc.password=root

 5)配置log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 6)创建spring核心配置文件之applicationContext-dao.xml

  此文件用来管理dao层业务:配置数据源,配置SqlSessionFactory与dao层mapper扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>
    <!-- mapper配置 -->
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
    </bean>
    <!-- 配置Mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.clj.dao"/>
    </bean>

</beans>

 7)创建spring核心配置文件之applicationContext-service.xml

  此文件主要是负责业务层:开启service注解扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- @service扫描 -->
    <context:component-scan base-package="cn.clj.service"></context:component-scan>
    
</beans>

 8)创建spring核心配置文件之applicationContext-transaction.xml

  此文件主要负责事务:配置事务管理并注入数据源,配置事务通知与切面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 事务管理器 -->
    <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="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* cn.clj.service.*.*(..))" />
    </aop:config>
    
</beans>

  9)创建SpringMvc核心配置文件之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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <!-- 配置@Controller注解扫描 -->
        <context:component-scan base-package="cn.clj.controller"/>
        
         <!-- 注解驱动:能够自动配置最新版的处理器映射器和处理器适配器 -->
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
         <!-- 配置视图解析器 -->
         <!-- 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称,直接写去掉后缀的页面名 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <!--真正页面路径=前缀+页面名称+后缀  -->
             <!-- 跳转视图前缀 -->
             <property name="prefix" value="/jsp/"></property>
             <!-- 跳转视图后缀 -->
             <property name="suffix" value=".jsp"></property>
         </bean>
         
</beans>

  10)配置服务器启动扫描

    整合后以上的配置文件,服务器不能自动识别加载,需要在web.xml文件中开启包扫描

  <!-- 开启spring各核心配置文件扫描 -->
  <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>
  <!-- 开启SpringMVc拦截器-->
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

以上整合环境部署大致完成

2、整合开发

 需求1:从数据库查询到商品信息,并将数据返回到jsp中

  1)开启逆向工程自动生成pojo类和mapper接口和映射文件

   1.1: 导入逆向工程jar包mybatis-generator-core-1.3.2

   1.2 :在config包下创建generatorConfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.174.132:3306/SSM" userId="root"
            password="root">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
            userId="yycg"
            password="yycg">
        </jdbcConnection> -->

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="cn.clj.pojo"
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="cn.clj.dao" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.clj.dao" 
            targetProject=".\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="items"></table>
        <table tableName="user"></table>
        <!-- <table schema="" tableName="sys_user"></table>
        <table schema="" tableName="sys_role"></table>
        <table schema="" tableName="sys_permission"></table>
        <table schema="" tableName="sys_user_role"></table>
        <table schema="" tableName="sys_role_permission"></table> -->
        
        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>

  1.3:创建启动类

  这里需要配置generatorConfig.xml文件所在路径,并运行此类

package cn.clj.start;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class StartGenerator {
    public void generator() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("config/generatorConfig.xml"); 
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
    }
    public static void main(String[] args) throws Exception {
        try {
            StartGenerator startService = new StartGenerator();
            startService.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}

 自动生成的文件

 spring MVC框架入门(外加SSM整合)

2、定义接口和实现类

package cn.clj.service;

import java.util.List;

import cn.clj.pojo.Items;

public interface ItemsService {
    public List<Items> list() throws Exception;
}

 注意:这里注入了Mapper接口,并开启了自动扫描注解

package cn.clj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.clj.dao.ItemsMapper;
import cn.clj.pojo.Items;
import cn.clj.pojo.ItemsExample;

@Service
public class ItemsServiceImpl implements ItemsService{
    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public List<Items> list() throws Exception {
        //selectByExampleWithBLOBs(example)包含文本类型
        ItemsExample example=new ItemsExample();
        //example.createCriteria()可以创建查询条件;如果无需任何查询条件,直接将example实例化即可
        List<Items>  list=itemsMapper.selectByExampleWithBLOBs(example);
        return list;
    }    
}

3、创建conroller类

@Controller
public class ItemController {
    //注意:这里可以使用Autowired:自动装配(缺点:当一个接口有两个实现类时就无法世识别)
    //Resource:值是取实现类中定义的注解值
    @Autowired
    private ItemsService itemsService;
    //查询所有
    @RequestMapping("/list")
    public ModelAndView itemsList() throws Exception{
        List<Items> list=itemsService.list();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemList",list);
        modelAndView.setViewName("itemList");
        return modelAndView;
    }
}

 4、创建itemList.jsp接受参数

<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<body> 
<form action="${pageContext.request.contextPath }/search.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<!-- 如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用-->
<td>商品名称:<input type="text" name="items.name"/></td>
<td>商品价格:<input type="text" name="items.price"/></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>生产日期</td>
    <td>商品描述</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemList}" var="item">
<tr>
    <td>${item.name}</td>
    <td>${item.price}</td>
    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    <td>${item.detail}</td>
    
    <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

 5、测试:http://localhost:8080/项目名/list.action

五、SpringMvc值参数绑定

 1、关于@RequestParam标签

  1) 使用@RequestParam常用于处理简单类型的绑定

 如:jsp传入一个值

<input type="text" name="item_id"/>

   controller接收

public String editItem(@RequestParam(value="item_id",required=true) String id) {
    
}

注意:

1.1 ) value:参数名字,即入参的请求参数名字,形参名称为id,但是这里使用value="item_id"限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。如果请求参数中没有item_id将跑出异常:

HTTP Status 500 - Required Integer parameter 'item_id' is not present

1.2)这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值

  2、绑定普通类型

  需求1:打开编辑界面,查看商品详情

  环境:引用以上环境,当触发itemList.jsp中的修改按钮,根据超链接跳转,传入参数为商品id

  1)、编写接口和实现类

public Items findItemsById(Integer id) throws Exception
package cn.clj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.clj.dao.ItemsMapper;
import cn.clj.pojo.Items;
import cn.clj.pojo.ItemsExample;

@Service
public class ItemsServiceImpl implements ItemsService{
    @Autowired
    private ItemsMapper itemsMapper;
    @Override
    public Items findItemsById(Integer id) throws Exception {
        Items items=itemsMapper.selectByPrimaryKey(id);
        return items;
    }
}

 2)、编写controller

  参数通过域名封装到请求中,此时可以在方法中定义HttpServletRequest、HttpSession、Model将参数获得

  注意:这里设置返回页面是个字符串

package cn.clj.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.clj.pojo.Items;
import cn.clj.service.ItemsService;
import cn.clj.vo.QueryVo;

@Controller
public class ItemController {
    //注意:这里可以使用Autowired:自动装配(缺点:当一个接口有两个实现类时就无法世识别)
    //Resource:值是取实现类中定义的注解值
    @Autowired
    private ItemsService itemsService;/**
     * springMvc默认支持的参数类型,也就是说在controller方法中可以加入这些,也可以不加
     * HttpServletRequest
     * HttpServletResponse
     * HttpSession
     * Model
     */
    @RequestMapping("/itemEdit")
    public String itemEdit(HttpServletRequest request,Model model) throws Exception{
        String idStr=request.getParameter("id");
        Items items=itemsService.findItemsById(Integer.parseInt(idStr));
        //Model模型:模型中放入了返回给页面的数据
        //Model底层就是用的request域传递数据,但是对request进行了扩展
        model.addAttribute("item",items);
        //如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
        return "editItem";
    }
}

 3)、创建editItem.jsp接受参数

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  
 <body> 
    <!-- 上传图片是需要指定属性 enctype="multipart/form-data" -->
    <!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> -->
    <form id="itemForm"    action="${pageContext.request.contextPath }/updateitem.action" method="post">
        <input type="hidden" name="id" value="${item.id }" /> 修改商品信息:
        <table width="100%" border=1>
            <tr>
                <td>商品名称</td>
                <td><input type="text" name="name" value="${item.name }" /></td>
            </tr>
            <tr>
                <td>商品价格</td>
                <td><input type="text" name="price" value="${item.price }" /></td>
            </tr>
            <tr>
                <td>商品简介</td>
                <td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="提交" />
                </td>
            </tr>
        </table>

    </form>
</body>

 绑定pojo类型

 需求2、更新数据

 1)、前提有pojo类(其中在修改界面中的接受的Items 的属性必须与pojo类中的属性保持一致)

package cn.clj.pojo;

import java.util.Date;

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    //省略set/get方法
}

 1)、创建接口和实现类

    public void updateItems(Items items) throws Exception;
@Service
public class ItemsServiceImpl implements ItemsService{
    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public void updateItems(Items items) throws Exception {
        // TODO Auto-generated method stub
        //此方法包含大对象文本
        itemsMapper.updateByPrimaryKeyWithBLOBs(items);
    }
    
}

 2)、创建conroller类定义方法

@Controller
public class ItemController {
    //注意:这里可以使用Autowired:自动装配(缺点:当一个接口有两个实现类时就无法世识别)
    //Resource:值是取实现类中定义的注解值
    @Autowired
    private ItemsService itemsService;/**
     * 更新数据 
     * @return
     */
    //1.springMvc可以直接接受基本数据类型,包括string,spring Mvc可以帮你自动进行类型转换
    //controller方法接受的参数的变量名称必须要等于页面上input框的name属性值
    //2.springMvc可以直接接受pojo类型,要求页面上input框的name属性名称必须等于pojo的属性名称
    @RequestMapping("/updateitem")
    public String updateitem(Items items) throws Exception{      //方式二
    //public String updateitem(Integer id,String name,Float price,String detail) throws Exception{   //方式一
//        Items items=new Items();
//        items.setId(id);
//        items.setName(name);
//        items.setPrice(price);
//        items.setDetail(detail);
        //注意:这里jsp源代码中屏蔽了接受时间的框,是因为String类型可以转换为基本类型,但是string类型不能转换为Date类型
        items.setCreatetime(new Date());//数据库字段定义为非空
        itemsService.updateItems(items);
        return "success";
    }
}

 3)、创建success.jsp页面

 <body>
   <h3>更新成功</h3>
  </body>

 解决中文乱码问题

 1)针对post请求

  post请求是封装于服务器端,请求参数不会在域名中出现

  在web.xml中配置过滤器,当服务器启动时就对请求中的参数进行字符编码转换

 <!-- 解决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>

 2)针对get请求

  get请求是响应在地址栏中,通过地址栏可以看到请求参数

  将controller类中接受到的包含中文参数进行字符编码转换

String name=new String(request.getAttribute("参数名").getBytes("iso-8859-1"),"utf-8")); 

 绑定包装类

 需求3:使用包装类接受高级查询条件中所传过来的值

 1) 定义VO

package cn.clj.vo;

import java.util.Arrays;
import java.util.List;

import cn.clj.pojo.Items;
/**
 * 演示高级查询,封装指定pojo类中的指定属性
 * @author 佳先森
 *
 */
public class QueryVo {
    //商品对象
    private Items items;
  //省略set/get、toString方法
}

 2) 定义jsp

<form action="${pageContext.request.contextPath }/search.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<!-- 如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用-->
<td>商品名称:<input type="text" name="items.name"/></td>
<td>商品价格:<input type="text" name="items.price"/></td>
<td><input type="submit" value="查询"/></td> </tr> </table>

 3) controller类中定义方法

@Controller
public class ItemController {
//如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用
    @RequestMapping("/search")
    public String search(QueryVo vo) throws Exception{
        System.out.println(vo);
        return "";
    }

 自定义参数绑定

 需求:接受参数为时间格式

 分析:为什么输入框为时间格式的conroller接收时会报错呢,是因为spring MVC能够将自动将字符串转换为原始型和包装类型,但是它不能讲时间格式的转换为字符串(时间格式有多种),不然会报错,这里只能为时间格式自定义参数绑定

 1) 创建工具类

package cn.controller.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

/**
 * 自定义全局字符串转日期转换器
 * param:s -source:源
 * param:T -target:目标
 * 还需在springMv中配置此工具类
 * @author 佳先森
 *
 */
public class CustomGlobalStrToDateConverter implements Converter<String,Date>{

    @Override
    public Date convert(String source) {
        try {
            Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
            return date;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return null;
    }

}

 2) 在SpringMVc上创建自定义转换器,并将它配置到注解驱动上

 <!-- 注解驱动:能够自动配置最新版的处理器映射器和处理器适配器 -->
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 
<!-- 配置自定义转换器:用于将字符串转换为日期格式
             步骤:1.编写工具类  2.将自定义的转换器配置到注解驱动上
         -->
<bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                 <!-- 指定自定义转换器的全路径名名称 -->
                <bean class="cn.controller.converter.CustomGlobalStrToDateConverter"/>
            </set>
        </property>
</bean>

 3) jsp界面

  注意引入jstl/fmt标签,这是能够在界面中对时间内容进行格式整理

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post"> <table width="100%" border=1> <tr> <td>商品生产日期</td> <td><input type="text" name="createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td> </tr> </table> </form>

 4) controller接收方法

    @RequestMapping("/updateitem")
    public String updateitem(Items items,Model model) throws Exception{
         itemsService.updateItems(items);
         return "success";
    }

六、spring MVC与struts 2区别

1、 springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过虑器。

2、 springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例)struts2是基于类开发,传递参数是通过类的属性,只能设计为多例。

3、 Struts采用值栈存储请求和响应的数据,通过OGNL存取数据, springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过reques域传输到页面。Jsp视图解析器默认使用jstl

七、spring MVC高级参数绑定

 1、绑定数组

 需求:演示批量删除

 1) 定义jsp

  jsp中包含一个form表单,多个input框前有个checkbox复选框

<form action="${pageContext.request.contextPath }/delAll.action" method="post">
   查询条件:
<table width="100%" border=1>
<tr>
<!-- 如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用-->
<td>商品名称:<input type="text" name="items.name"/></td>
<td>商品价格:<input type="text" name="items.price"/></td>
 <td><input type="submit" value="批量删除"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>生产日期</td>
    <td>商品描述</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemList}" var="item">
<tr>
    <!-- 批量删除:name属性名称等于vo中的接受的属性名称 -->
    <td>
        <input type="checkbox"  name="ids" value="${item.id}"/>
    </td>
    <td>${item.name}</td>
    <td>${item.price}</td>
    <td>fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    <td>${item.detail}</td>
     <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>

 2)定义controller中的方法

 这里是通过复选框将选中的数据进行传送,controller方法中得用数组来接收

  方式一:数组作为参数进行接收:注意这里属性名(ids)要与复选框保持一致

//如果批量删除,一堆input复选框,那么可以提交数据(只有被选中的时候才可以提交)
    @RequestMapping("/delAll")
    public String delAll(String[] ids) throws Exception{
        System.out.println(ids.toString());
        return "";
    }

   方式二:将数组作为属性封装到VO对象中,将VO对象作为参数进行接收

public class QueryVo {
    //商品对象
    private Items items;

    //批量删除
    private Integer[] ids;
    
       //省略set/get方法
}
//如果批量删除,一堆input复选框,那么可以提交数据(只有被选中的时候才可以提交)
    @RequestMapping("/delAll")
    public String delAll(QueryVo vo) throws Exception{
        System.out.println(vo.getItems().getName());
                System.out.println(queryVo.getItems().getPrice());
        return "";
    }   

 2、绑定集合(将表单的数据绑定到List中)

 需求:对数据进行批量修改

  1) 在pojo类中定义一个集合的属性

package cn.clj.vo;

import java.util.Arrays;
import java.util.List;

import cn.clj.pojo.Items;
/**
 * 演示高级查询,封装指定pojo类中的指定属性
 * @author 佳先森
 *
 */
public class QueryVo {
    //商品对象
    private Items items;
    //用户对象
    //。。。。
    //批量删除
    private Integer[] ids;
    //批量修改
    private List<Items> itemsList;
    
    
    //省略set/get,toString()方法
    
    
}

   2)更改jsp

<form action="${pageContext.request.contextPath }/updateAll.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<!-- 如果controller接受的是vo,那么页面上input框中name属性值要等于vo属性.属性 (..)进行引用-->
<td>商品名称:<input type="text" name="items.name"/></td>
<td>商品价格:<input type="text" name="items.price"/></td>
<td><input type="submit" value="批量修改"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>生产日期</td>
    <td>商品描述</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemList}" var="item" varStatus="status">
  <tr>
    <!-- 如果批量修改,可以用List<pojo>来接受,页面上input框的name属性值=vo中的接受的属性名称+[list的下标]+.+list泛型属性的名称 -->
    <td>
        <input type="checkbox"  name="ids" value="${item.id}"/>
        <input type="hidden"  name="itemsList[${status.index}].id" value="${item.id}"/>
    </td>
    <td><input type="text" name="itemsList[${status.index}].name" value="${item.name}"/></td>
    <td><input type="text" name="itemsList[${status.index}].price" value="${item.price}"/></td>
    <td><input type="text" name="itemsList[${status.index}].createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
    <td><input type="text" name="itemsList[${status.index}].detail" value="${item.detail}"/></td>
   <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
  </tr>
</c:forEach>

</table>
</form>

 3)在controller类中创建接受方法

    //批量修改
    @RequestMapping("/updateAll")
    public String updateAll(QueryVo vo) throws Exception{
        System.out.println(vo.getItems().getName()); 
System.out.println(vo.getItems().getPrice());
return "";
    }

八、关于spring MVC窄化请求映射

  分析:在团队开发情况下,不同controller中可能出现一个或多个方法RequestMapping值相同,因为配置文件中采用的是包扫描的方法进行映射,就有可能在输入域名的时候跳错controller中的方法。此时,如果为了区分每个conroller中的每个方法,必须配置窄化请求映射,相当于给类起个名字,每当反问域名时,指定跳转方法uri前必须加上这个“类名”,通过此方法对类行进分类管理

  如:

@Controller
//窄化请求映射:为防止方法名重名,相当于在url中多加了一层目录,房子重名
@RequestMapping("/items")
public class ItemController {
//批量修改
    @RequestMapping("/updateAll")
    public String updateAll(QueryVo vo) throws Exception{
        System.out.println(vo);
        return "";
    }

 如果以后要跳转到该方法,域名得写成http://localhost:8080/项目名/items/updateAll.action

九、spring MVC之请求方法限定

 语法为@RequestMapping(value="/XXX",method=RequestMethod.XX),其中XX可以写GET或者POST,如果在方法中限定了请求方法而jsp中表单提交方式不是指定的,会报405错误

@RequestMapping(value="/list",method=RequestMethod.GET)
    public ModelAndView itemsList() throws Exception{
        List<Items> list=itemsService.list();
        System.out.println("进入了");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemList",list);
        modelAndView.setViewName("itemList");
        return modelAndView;
    }

十、controller类方法返回值

   controller方法返回值包含多种,一下介绍几种常用的:

 1、ModelAndView方式

    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ModelAndView itemsList() throws Exception{
        List<Items> list=itemsService.list();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemList",list);
        modelAndView.setViewName("itemList");
        return modelAndView;
    }

 2、String方式(直接return一个字符串),返回的数据由model完成

 种类一:放回普通字符串(去掉页面扩展名)

@RequestMapping("/search")
    public String search(QueryVo vo) throws Exception{
        System.out.println(vo);
        return "success";
    }

 种类二:请求转发方式

 注意:这里是