编码过滤器
<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>
如果设置了过滤器后,html页面乱码,jsp页面正常显示,这时候要删除
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
解决中文数据存入数据库乱码, 页面乱码等问题
不使用框架的web项目中配置log4j
使用ThreadLocal来管理SqlSession中应注意session关闭问题,在关闭SqlSession时要清空ThreadLocal(调用remove方法)
package com.crsbg.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
public class DBUtils {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();
private static SqlSessionFactory factory = null;
//初始化SqlSessionFactory
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("config.xml");
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//获取SqlSession
public static SqlSession getSqlSession() {
SqlSession session = threadLocal.get();
if (session==null){
session = factory.openSession();
threadLocal.set(session);
}
return session;
}
//关闭SQL Session
public static void closeSession(){
SqlSession session = threadLocal.get();
if(session!=null){
session.close();
//需要移出,否则会出现org.apache.ibatis.executor.ExecutorException: Executor was closed异常
threadLocal.remove();
}
}
}
使用Idea开发web设置
1.idea中tomcat乱码:
a. file - settings - 搜File Encodings,改为utf-8
b. 在help菜单 点击 edit custom VM Options,最后追加-Dfile.encoding=UTF-8
c.配置tomcat的页面中:VM option设置:-Dfile.encoding=UTF-8
2.热部署问题(jsp+java)在Tomcat的运行参数中设置
Update:更新操作(经测试,很多时候无效)
Frame:idea失去焦点时触发
推荐选项:
Update:任意
Frame:update classes and resources
idea:热部署,如果是run启动,仅JSP等静态资源有效
如果是debug启动,java和jsp等均有效
总结 热部署:
a. Frame:update classes and resources
b. 以debug模式启动
注意:编写servlet前 需要先加入tomcat环境