Springmvc 三个组件, springmvc默认支持的。 查询源码如下
总结: 三个组件, 其中两个组件处理器映射器,处理器适配器,查询源代码发现,这两个类已经过时。 需要配置一个新的处理器映射器,处理器适配器。 替代:
代码:
Springmvc.xml 当中进行配置:
<!-- 配置处理器映射器: 之前默认支持,只是原来过来的过时了 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
使用注解的方式: 简化上述操作;
<!-- 使用注解的方式: 替代上述两个操作: -->
<mvc:annotation-driven/>
后端处理器的代码:
// 支持传统的Servlet的API
@RequestMapping(value="/saveUser5")
public ModelAndView saveUser5(HttpServletRequest request) {
String name = request.getParameter("username");
System.out.println(name);
ModelAndView mav =new ModelAndView();
mav.setViewName("success");
mav.addObject("username", name);// request.setAttribute("username", name);
return mav;
}
:视图解析器的配置, 简化controller层的开发:
数据类型转换总结
自定义类型转换器的类:
package com.yidongxueyuan.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
* Converter 接口类型:springmvc 提供的一个转换器接口:
* spring内部: string-int 等,只要是设计到数据类型的转换都是给接口的子类完成的:
*
* Converter<String,Date>
* String: source
* Date: target
*
* @author Mrzhang
*
*/
public class StringToDateConverter implements Converter<String,Date> {
@Override
public Date convert(String source) {
try {
if(source == null) {
throw new RuntimeException();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(source);
return date;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
写完之后 在springmvc.xml文件 注入进去
<!-- 使用注解的方式: 替代上述两个操作: -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- springmvc 配置自定义类型转换器 -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean id="stringToDateConverter" class="com.yidongxueyuan.utils.StringToDateConverter"></bean>
</set>
</property>
</bean>
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">
<!-- 注解的形式: -->
<context:component-scan base-package="com.yidongxueyaun.controller"/>
<!-- 处理器映射器: 有默认值,但是发现已经过时, 单独进行配置一个没有过时的类 -->
<!-- <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 conversion-service="serviceFactoryBean" />
<!-- 绑定一个自定义转换的工厂 : spring 管理:
属性: converters 配置转换器: 值 list 集合 或者是set集合:
set: 标签表明配置多个转换器:
bean: 自定义的转换器:
-->
<bean id="serviceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 自定有的转换器: -->
<bean class="com.yidongxueyuan.springmvc.converter.DateConverter" />
</set>
</property>
</bean>
<!-- 配置一个视图解析器: -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置一个全局的异常处理器: -->
<bean id="itemException" class="com.yidongxueyaun.ItemException">
</bean>
<!-- 文件上传,id必须设置为multipartResolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传大小 -->
<property name="maxUploadSize" value="5000000" />
</bean>
<!-- 配置一个拦截器 -->
<!-- 配置拦截器 /** 拦截所有: /** /Item/queryLis/xxx.action-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yidongxueyaun.interceptor.Interceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 拦截器多个: -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yidongxueyaun.interceptor.Interceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>