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 {
    }
}
目录
相关文章
|
2月前
|
JSON JavaScript 前端开发
JavaScript实现字符串转json对象的方法
JavaScript实现字符串转json对象的方法
|
17天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
110 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
5天前
|
XML Java 应用服务中间件
springMVC01,springMVC的执行流程【第一个springMVC例子(XML配置版本):HelloWorld】
通过一个HelloWorld实例,介绍了SpringMVC的基本概念、执行流程,并详细讲解了如何创建和配置第一个SpringMVC项目(基于XML)。
springMVC01,springMVC的执行流程【第一个springMVC例子(XML配置版本):HelloWorld】
|
5天前
|
XML JSON JavaScript
JSON对象的stringify()和parse()方法使用
本文阐述了JSON对象的`stringify()`和`parse()`方法的用法,包括如何将JavaScript对象转换为JSON字符串,以及如何将JSON字符串解析回JavaScript对象,并讨论了转换过程中需要注意的事项。
JSON对象的stringify()和parse()方法使用
|
7天前
|
JSON 前端开发 中间件
React读取properties配置文件转化为json对象并使用在url地址中
本文介绍了如何在React项目中读取properties配置文件,将其内容转化为JSON对象,并在请求URL地址时使用这些配置。文章详细说明了异步读取文件、处理字符串转换为JSON对象的过程,并提供了一个封装函数,用于在发起请求前动态生成配置化的URL地址。
21 1
|
1月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
30 1
|
2月前
|
XML Android开发 UED
"掌握安卓开发新境界:深度解析AndroidManifest.xml中的Intent-filter配置,让你的App轻松响应scheme_url,开启无限交互可能!"
【8月更文挑战第2天】在安卓开发中,scheme_url 通过在`AndroidManifest.xml`中配置`Intent-filter`,使应用能响应特定URL启动或执行操作。基本配置下,应用可通过定义特定URL模式的`Intent-filter`响应相应链接。
89 12
|
2月前
|
Shell Android开发
安卓scheme_url调端:在AndroidManifest.xml 中如何配置 Intent-filter?
为了使Android应用响应vivo和oppo浏览器的Deep Link或自定义scheme调用,需在`AndroidManifest.xml`中配置`intent-filter`。定义启动的Activity及其支持的scheme和host,并确保Activity可由外部应用启动。示例展示了如何配置HTTP/HTTPS及自定义scheme,以及如何通过浏览器和adb命令进行测试,确保配置正确无误。
|
7天前
|
算法 安全 Java
微服务(四)-config配置中心的配置加解密
微服务(四)-config配置中心的配置加解密
|
7天前
|
JavaScript
Vue3基础(19)___vite.config.js中配置路径别名
本文介绍了如何在Vue 3的Vite配置文件`vite.config.js`中设置路径别名,以及如何在页面中使用这些别名导入模块。
12 0
Vue3基础(19)___vite.config.js中配置路径别名