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