我的ssm项目总结
a、项目启动监听器:
用途:为系统初始化一些,系统参数,及其他初始化操作
例如:System.setProperty(“logpath”,”xxx”),日志输出地址,可在log4j配置文件中${logPath} 获取。
System.setProperty("jsse.enableSNIExtension", "false");解决linux 中,httpClient 异常问题。
实现方式如下:
/** * 项目启动监听器 * @author Sivan * */ public class SystemStartListener implements ServletContextListener{ private Logger logger = LoggerFactory.getLogger(SystemStartListener.class); @Override public void contextDestroyed(ServletContextEvent contextEvent) { } @Override public void contextInitialized(ServletContextEvent contextEvent) { logger.info("system start............"); String path = Environment.class.getResource("").getPath(); String logPath = path.substring(0, path.toUpperCase().lastIndexOf("WEB-INF/")).replaceAll("%20", " ")+"qbzLogs"; System.setProperty("logPath",logPath); System.setProperty("jsse.enableSNIExtension", "false"); } }
web.xml中添加如下配置:
<listener> <listener-class>com.qbz.listener.XXXListener</listener-class> </listener>
b、Spring加载完成监听器:
用途:环境初始化完成,将一些冷数据放入缓存中,
实现方式如下:
/** * Spring 加载完成监听器 * @author Sivan * */ @Component @Scope("singleton") public class SpringStartListener implements ApplicationListener<ContextRefreshedEvent> { private Logger logger = LoggerFactory.getLogger(SpringStartListener.class); @Resource private SystemConstantDao systemConstantDao; @Override public void onApplicationEvent(ContextRefreshedEvent event) { logger.info("spring lodeOver.........."); // 初始化项目基础配置项 systemConstantDao.getAreas(0); systemConstantDao.getBank(); } }
c、Spring MVC 登入拦截器:
用途:为项目做权限控制,拦截。
实现方式如下:
第一步:编写拦截器类,并实现HandlerInterceptor接口:
/** * 登入拦截器 * @author Sivan * */ public class LoginInterceptor implements HandlerInterceptor{ private static final String LOGIN_URL = "/user/userLogin"; private static final Logger LOG = LoggerFactory.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String contextPath = request.getContextPath(); String requestUrl = request.getRequestURI().toString(); HttpSession session = request.getSession(true); // 从session 里面获取用户名的信息 Object obj = session.getAttribute("SESSION_USER"); // 判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆 if (obj == null || "".equals(obj.toString())) { LOG.info("[被拦截的请求:{}]",requestUrl); response.sendRedirect(contextPath+LOGIN_URL+"?toUrl="+requestUrl); return false; } return true; } @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {} @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {} }
第二步:在spring mvc配置文件中添加如下内容:
<mvc:interceptors> <mvc:interceptor> <!-- 需拦截的地址 --> <mvc:mapping path="/user/**" /> <!-- 需排除拦截的地址 --> <mvc:exclude-mapping path="/user/Login" /> <!-- 拦截器实现 --> <bean class="com.qbz.listener.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors>d、处理项目中文乱码问题:
<!--[if !supportLists]-->1、<!--[endif]-->在web.xml中添加如下配置(编码过滤器):
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--[if !supportLists]-->2、<!--[endif]-->在spring mvc中添加如下配置(解决ajax请求/响应json中文乱码问题):
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
e、 Mybatis配置细节:
资源文件加载无效问题:
造成原因:
使用org.mybatis.spring.mapper.MapperScannerConfigurer,自动扫描所有Mapper文件时,会引发提前初始化问题,而此时资源文件还未初始化,所以就直接将占位符做字符串处理了。
解决方案:
将如下配置:
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
改成:
<propertyname="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
SessionFactory配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatisConfig.xml" /> <property name="mapperLocations" value="classpath:com/qbz/mapper/**/*Mapper.xml" /> <property name="typeAliasesPackage" value="com.qbz.entity" /> </bean>
Mapper扫描配置:
<!-- 扫描mapper.java --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qbz.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
分页插件:
在mybatis配置文件中添加如下配置
<plugins> <plugin interceptor="com.qbz.util.pagePlugin.PagePlugin"> <property name="dialect" value="mysql" /> <property name="pageSqlId" value=".*ListPage.*" /> </plugin> </plugins>
上一篇: DB2移动数据库目录调整方法