springmvc使用配置文件

152 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第20天,点击查看活动详情

建立src/main/webapp/WEB-INF/web.xml 添加servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--添加DispatcherServlet类-->
    <servlet>
        <servlet-name>smvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置springmvc配置文件位置 默认在WEB-INF下-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:smvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>smvc</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

然后在resource下创建smvc.xml文件此文件中配置消息转换器等 相当于AppConfig类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描包-->
    <context:component-scan base-package="cn.chenxiejia"/>
    <!--静态资源访问-->
    <mvc:resources mapping="/static/*.*" location="/static/"/>
    <mvc:resources mapping="/imgs/*.*" location="/imgs/"/>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!--普通字符串编-->
            <bean name="string" class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=utf-8"/>
            </bean>
            <!--时间日期类编码 继承ObjectMapper 用到jackson组件-->
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <!--                    此类为自己继承ObjectMapper类 此类中具体配置日期格式-->
                    <bean class="cn.chenxiejia.message.CustomObjectMapper"/>
                </property>
                <property name="defaultCharset" value="utf-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--视图处理-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀 就是文件夹位置-->
        <property name="prefix" value="/WEB-INF/static/"/>
        <!--后缀 就是文件类型-->
        <property name="suffix" value=".html"/>
    </bean>

</beans>

CustomObjectMapper类

public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper() {
        //转换java.util.Date类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setDateFormat(sdf);

        //转换jdk1.8新的日期类
        JavaTimeModule m = new JavaTimeModule();
        m.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        m.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        m.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        m.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        m.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        m.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        this.registerModule(m);
    }

}

其他的同无配置方式