SSM的整合及spring-config.xml文件的配置信息,时间日期转换器、Json对象注解配置

简介: SSM的整合及spring-config.xml文件的配置信息,时间日期转换器、Json对象注解配置

SSM整合,Spring配置文件都要配置哪些内容?,这里列举一个全面的,

以后用到哪里复制哪里:

Spring-config.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.3.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/p ">
    <!-- 默认扫描包路径,扫描控制器 -->
    <context:component-scan base-package="com.macw.controller"></context:component-scan>
    <!-- 注解驱动 -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <!-- 转换器配置,时间日期格式化 -->
   <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
      <property name="converters">
        <list>
          <bean class="com.macw.util.DateConverter"></bean>
        </list>
      </property>
     </bean>
   <!-- 开启注解,Json对象的注解配置 -->
    <mvc:annotation-driven>
        <!-- 使用fastjson将java对象转换为json对象 -->
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 定义spring MVC的拦截器 -->
    <!-- <mvc:interceptors>
    <mvc:interceptor>
      拦截所有请求
      <mvc:mapping path="/*/*/lj.do"/>
      自定义判断用户权限的拦截类
      <bean class="com.util.AuthorizedInterceptor"></bean>
    </mvc:interceptor>
  </mvc:interceptors> -->
  <!-- 上传文件大小配置-->
   <!--
    开启文件上传springmvc功能
     springmvc底层会自动去找id为multipartResolver的bean,来作为文件上传的解析器。
     注意:这个bean的id是固定的,不能写错必须是“multipartResolver”
     -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000"></property>
    </bean>
   <!-- 静态资源访问,排除掉一般资源文件(图片,css, js),进入springmvc的控制器  -->
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/img/**" location="/img/"/>
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

时间日期格式化util类:

com.macw.util.DateConverter

package com.macw.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class DateConverter implements Converter<String, Date> {
  public Date convert(String str) {
    String pattern = "yyyy-MM-dd";
    if (str.contains("年")){
      pattern = "yyyy年MM月dd日";
    }else if (str.contains("/")){
      pattern = "yyyy/MM/dd";
    }
    SimpleDateFormat sdf = new SimpleDateFormat("pattern");
    try {
      Date d = sdf.parse("str");
      return  d;
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }
}

拦截器,拦截登录请求CheckSessionUserInterceptor类:

package com.baizhi.interceptor;
import com.baizhi.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
        public class CheckSessionUserInterceptor implements HandlerInterceptor {
            @Override
            public boolean preHandle(HttpServletRequest req, HttpServletResponse response, Object o) throws Exception {
                HttpSession session=req.getSession();
                User user=(User)session.getAttribute("user");
                //检查session里面没有登录标记
                if(user==null){
                    //重定向到登录页面
                    String path=req.getContextPath();
                    response.sendRedirect(path+"/login.jsp");
                    return false;
                }else{
                    return true;
                }
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
目录
相关文章
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
1952 128
|
Java 测试技术 数据库
使用Spring的@Retryable注解进行自动重试
在现代软件开发中,容错性和弹性至关重要。Spring框架提供的`@Retryable`注解为处理瞬时故障提供了一种声明式、可配置的重试机制,使开发者能够以简洁的方式增强应用的自我恢复能力。本文深入解析了`@Retryable`的使用方法及其参数配置,并结合`@Recover`实现失败回退策略,帮助构建更健壮、可靠的应用程序。
1380 1
使用Spring的@Retryable注解进行自动重试
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
852 12
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
1066 2
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
878 0
探索Spring Boot的@Conditional注解的上下文配置
|
智能设计 Java 测试技术
Spring中最大化@Lazy注解,实现资源高效利用
本文深入探讨了 Spring 框架中的 `@Lazy` 注解,介绍了其在资源管理和性能优化中的作用。通过延迟初始化 Bean,`@Lazy` 可显著提升应用启动速度,合理利用系统资源,并增强对 Bean 生命周期的控制。文章还分析了 `@Lazy` 的工作机制、使用场景、最佳实践以及常见陷阱与解决方案,帮助开发者更高效地构建可扩展、高性能的 Spring 应用程序。
578 0
Spring中最大化@Lazy注解,实现资源高效利用
|
Java 测试技术 编译器
@GrpcService使用注解在 Spring Boot 中开始使用 gRPC
本文介绍了如何在Spring Boot应用中集成gRPC框架,使用`@GrpcService`注解实现高效、可扩展的服务间通信。内容涵盖gRPC与Protocol Buffers的原理、环境配置、服务定义与实现、测试方法等,帮助开发者快速构建高性能的微服务系统。
2126 0
|
存储 Java 数据库
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
|
存储 Java Spring
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
652 0
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
|
Java Maven 开发者
Spring 对象生命周期示例|学习笔记
快速学习Spring 对象生命周期示例
319 0
Spring 对象生命周期示例|学习笔记

热门文章

最新文章