springmvc

SpringMvc执行过程

Springmvc与mybatis整合

导jar包

配置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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  

   <!-- 配置@Controller处理器,包扫描器 -->

   <context:component-scan base-package="com" />

  

   <!-- 配置注解驱动,相当于同时使用最新处理器映射跟处理器适配器,对json数据响应提供支持 -->

   <mvc:annotation-driven></mvc:annotation-driven>

  

   <!-- 配置视图解析器 prefix:前缀 suffix:后缀 -->

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name="prefix" value="/WEB-INF/jsp/"></property>

      <property name="suffix" value=".jsp"></property>

   </bean>

</beans>

配置jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8

jdbc.username=root

jdbc.password=T20000101.

 

配置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

 

配置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>

  

</configuration>

 

配置applicationContent-dao.xm   l

<?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:jdbc.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>

  

   <!-- SqlSessionFactory配置 -->

   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dataSource" />

      <!-- 加载mybatis核心配置文件 -->

      <property name="configLocation" value="classpath:SqlMapConfig.xml" />

      <!-- 别名包扫描 -->

      <property name="typeAliasesPackage" value="com.springmvc.pojo" />

   </bean>

  

    <!-- 动态代理,第二种方式:包扫描(推荐): -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

      <!-- basePackage多个包用","分隔 -->

      <property name="basePackage" value="com.springmvc.mapper" />

    </bean>

</beans>

配置applicationContent_service.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">

   <!-- @Service包扫描器 -->

   <context:component-scan base-package="com.itheima.springmvc.service"/>

</beans>

 

配置applicationContext-trans.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>

      <!-- 其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。

        propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

      read-only="true"表示只能查询,不能修改 -->

        <!-- 传播行为 -->

        <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:method name="query*" propagation="SUPPORTS" read-only="true" />

      </tx:attributes>

   </tx:advice>

 

   <!-- 切面 -->

   <aop:config>

      <aop:advisor advice-ref="txAdvice"

        pointcut="execution(* 业务逻辑类路径com.itheima.springmvc.service.*.*(..))" />

   </aop:config>

</beans>

 

配置mapper和xml

应用mybatic逆向工程得到mapper和xml

 

配置web.xml

<?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">

  <display-name>springmvc_02</display-name>

 

  <!-- 配置spring -->

   <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath: applicationContent*.xml</param-value>

   </context-param>

 

   <!-- 使用监听器加载Spring配置文件 -->

   <listener>

      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

 

  <!-- 解决post乱码问题 -->

   <filter>

      <filter-name>encoding</filter-name>

      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <!-- 设置编码参是UTF8 -->

      <init-param>

         <param-name>encoding</param-name>

         <param-value>UTF-8</param-value>

      </init-param>

   </filter>

   <filter-mapping>

      <filter-name>encoding</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

 

  <!-- 前端控制器 -->

  <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>

<!-- <url-pattern>/</url-pattern> -->

  </servlet-mapping>

<servlet-mapping>

       <servlet-name>default</servlet-name>

       <url-pattern>*.js</url-pattern>

    </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

Springmvc与hibernate整合

配置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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  

   <!-- 配置@Controller处理器,包扫描器 -->

   <context:component-scan base-package="com" />

  

   <!-- 配置注解驱动,相当于同时使用最新处理器映射跟处理器适配器,对json数据响应提供支持 -->

   <mvc:annotation-driven></mvc:annotation-driven>

  

   <!-- 配置视图解析器 prefix:前缀 suffix:后缀 -->

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name="prefix" value="/WEB-INF/jsp/"></property>

      <property name="suffix" value=".jsp"></property>

   </bean>

</beans>

 

配置applicationContent.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:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- 配置c3p0连接池 -->

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

  <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/goods"></property>

  <property name="user" value="root"></property>

  <property name="password" value="T20000101."></property>

  </bean>

 

  <!-- <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory"></property>

  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/> -->

 

  <!-- 事务管理器 -->

   <bean id="transactionManager"

   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

      <!-- 数据源 -->

      <property name="dataSource" ref="dataSource" />

   </bean>

 

   <!-- 通知 -->

   <tx:advice id="txAdvice" transaction-manager="transactionManager">

      <tx:attributes>

      <!-- 其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。

        propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

      read-only="true"表示只能查询,不能修改 -->

        <!-- 传播行为 -->

        <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:method name="query*" propagation="SUPPORTS" read-only="true" />

      </tx:attributes>

   </tx:advice>

 

   <!-- 事务切面 -->

   <aop:config>

      <aop:advisor advice-ref="txAdvice"

        pointcut="execution(* com.itcast.springmvc.service.*.*(..))" />

   </aop:config>

  <!-- 事务管理end -->

 

 

  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

  <property name="sessionFactory" ref="sessionFactory"></property>

  </bean>

 

  <!-- sessionFactory创建交给spring管理 -->

  <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

  <property name="dataSource" ref="dataSource"></property>

  <property name="configLocations" value="classpath:hibernate.xml"></property>

  </bean>

 

  <context:component-scan base-package="com"></context:component-scan>

   <!-- 扫描属性上面的注解 -->

   <context:annotation-config></context:annotation-config>

 

</beans>

 

配置hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

   <session-factory>

      <property name="hibernate.show_sql">true</property>

      <property name="hibernate.format_sql">true</property>

      <property name="hibernate.hbm2ddl.auto">update</property>

     

      <mapping resource="实体类映射文件xml路径">

      <mapping resource="实体类映射文件xml路径"/>

   </session-factory>

</hibernate-configuration>

创建表实体类

配置实体类映射文件.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class table="表名" name="实体类路径">

      <id name="id" column="id">

        <generator class="native"></generator>

      </id>

      <property name="username" column="username"></property>

      <set name="实体类表中set名字" table="用了这个表作为外键的表名">

        <key column="用了这个表作为外键的表中外键名" ></key>

        <one-to-many class="用了这个表作为外键的表实体类路径" />

      </set>

   </class>

</hibernate-mapping>

 

配置spring.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:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- 配置c3p0连接池 -->

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

  <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/数据库"></property>

  <property name="user" value="root"></property>

  <property name="password" value="T20000101."></property>

  </bean>

 

  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory"></property>

  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/>

 

  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

  <property name="sessionFactory" ref="sessionFactory"></property>

  </bean>

 

  <!-- sessionFactory创建交给spring管理 -->

  <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

  <property name="dataSource" ref="dataSource"></property>

  <property name="configLocations" value="classpath:核心配置文件"></property>

  </bean>

 

  <context:component-scan base-package="com"></context:component-scan>

   <!-- 扫描属性上面的注解 -->

   <context:annotation-config></context:annotation-config>

 

</beans>

配置web.xml

<?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">

  <display-name>springmvc_02</display-name>

 

  <!-- 配置spring -->

   <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath: applicationContent*.xml</param-value>

   </context-param>

 

   <!-- 使用监听器加载Spring配置文件 -->

   <listener>

      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

 

  <!-- 解决post乱码问题 -->

   <filter>

      <filter-name>encoding</filter-name>

      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <!-- 设置编码参是UTF8 -->

      <init-param>

         <param-name>encoding</param-name>

         <param-value>UTF-8</param-value>

      </init-param>

   </filter>

   <filter-mapping>

      <filter-name>encoding</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

 

  <!-- 前端控制器 -->

  <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>

<!-- <url-pattern>/</url-pattern> -->

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

获取session,request,response

后台向前台发送数据

第一种 ModelAndView

第二种 Model

 

前台向后台发送数据

属性

属性名不同

对象

数组 

包装pojo

 

 

List集合

自定义参数绑定

/**

 * 第一个参数:source 要转换的源类型

 * 第二个参数:目标,要转换成的数据类型

 * @author tao

 *

 */

public class DateConvert implements Converter<String,Date>{

   /**

    * 将字符串转为时间类型

    */

   public Date convert(String source){

      Date result=null;

      SimpleDateFormat df=new SimplegDateFormat("yyyy-MM-dd HH:mm:ss");

      try {

        result=df.parse(source);

      } catch (ParseException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

      }

      return result;

   }

}

 

<!-- 配置注解驱动,相当于同时使用最新处理器映射跟处理器适配器,对json数据响应提供支持 -->

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

  

   <!-- 定义转换器 -->

   <bean id="MyConvert" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

      <property name="converters">

        <set>

           <bean class="com.springmvc.utils.DateConvert" />

        </set>

      </property>

   </bean>

@RequestMapping的使用

第一种:多个访问地址

第二种:根目录访问

第三种:请求方法的限定

第四种:根据参数返回页面

跳转页面

请求转发

重定向

乱码解决

Post乱码:

       在web.xml配置

<!-- 解决post乱码问题 -->

   <filter>

      <filter-name>encoding</filter-name>

      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <!-- 设置编码参是UTF8 -->

      <init-param>

        <param-name>encoding</param-name>

        <param-value>UTF-8</param-value>

      </init-param>

   </filter>

   <filter-mapping>

      <filter-name>encoding</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

 

Get乱码

 

Response乱码

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=UTF-8");

 

 

全局异常

实现这个接口HandlerExceptionResolver

 

文件上传

导jar包

配置服务器保存路径

Form一定要添加enctype="multipart/form-data" 和是post

在springMvc配置

<!-- 配置多媒体处理器 -->

    <!-- 注意:这里id必须填写:multipartResolver -->

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

       <!-- 最大上传文件大小 -->

       <property name="maxUploadSize" value="8388608" />

    </bean>

在方法参数上MultipartFile 表单name名

 

 

public String updataItem(Item item,Model mo,MultipartFile pictureFile) throws IllegalStateException, IOException{

      //图片新名字

      String newName=UUID.randomUUID().toString();

      //原来图片的名字

      String oldName=pictureFile.getOriginalFilename();

      //后缀

      System.out.println(oldName.lastIndexOf("."));

      String sux=oldName.substring(oldName.lastIndexOf("."));

      //新建本地文件流

      File file=new File("D:\\pic\\"+newName+sux);

      //写入本地磁盘

      pictureFile.transferTo(file);

     

      //保存图片

      item.setPic(newName+sux);

     

      itemService.updataItem(item);

      mo.addAttribute("item",item);

     

      mo.addAttribute("msg","修改成功");

      return "itemEdit";

 

 
 


   }

 

Json交互

 

@RequestMapping("getItem")

   @ResponseBody

   public Item getItem(@RequestBody Item item){

      System.out.println("请求的json"+item);

      //return itemService.getItemById(1);

      item.setName("手机");

      System.out.println("响应的json"+item);

      return item;

   }

需要springmvc加入json支持jar包

@ResponseBody:将对象转为json,后响应

@RequestBody object object:请求发送json,将json转为对象

 

Restful风格使用

@RequestMapping("----------&&&--------{id}.html")

   public String itemQuery(@PathVariable Integer id,Model model){

      Item item=itemService.getItemById(id);

      model.addAttribute("item",item);

      return "itemEdit";

   }

{id}:表示占位符

拦截器

拦截器的基本使用

实现这个接口HandlerInterceptor

在springmvc.xml配置

<mvc:interceptors>

      <mvc:interceptor>

      <!-- /**:拦截所有请求,包括二级以上目录

        /*:拦截一级目录

       -->

        <mvc:mapping path="/**"/>

        <bean class="com.springmvc.interceptor.MyInterceptor2"></bean>

      </mvc:interceptor>

   </mvc:interceptors>


拦截器执行顺序

 

public class MyInterceptor implements HandlerInterceptor {

 

   //第四个执行

   //方法执行之后

   //处理异常,清理资源,记入日志

   public void afterCompletion(HttpServletRequest arg0,

        HttpServletResponse arg1, Object arg2, Exception arg3)

        throws Exception {

      System.out.println("拦截器方法一");

   }

  

   //第二个执行的是被拦截的方法

   //第三个执行

   //方法执行之后,返回modelandview之前被执行

   //设置页面的共用参数等等

   public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

        Object arg2, ModelAndView arg3) throws Exception {

      System.out.println("拦截器方法二");

   }

 

   //第一个执行

   //进入方法前被执行

   //登录拦截,权限验证等等

   public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,

        Object arg2) throws Exception {

      System.out.println("拦截器方法三");

      //true放行,false拦截

      return true;

   }

配置多个拦截器

多个拦截器的执行顺序

一个拦截器拦截一个拦截器放行的执行顺序

       当第一个拦截器拦截时,只执行第一个拦截器的preHandle方法,其它方法不执行

       当第二个拦截器拦截时,请看如图下

 

配置不拦截的地址

<mvc:exclude-mapping path="/toLogin"/>

 

配置静态资源映射

<!-- 指定/WEB-INF/js  /WEB-INF/css 下的所有的静态资源包括子目录下的静态资源 都不被拦截-->

   <!-- mapping=/js/** :表示访问静态资源的拦截的形式 可以访问/js/下的静态资源或者所有的子目录下的静态资源 -->

   <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

   <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值