【问题集】:记录近期遇到的问题及解决

表单提交事件

<form onsubmit="return formObj.checkForm()" action="<%=request.getContextPath()%>/RegistServlet" method="POST">

切记:表单提交的事件在<form>标签上!onsubmit = "return true"代表提交成功。

地址跳转+虚拟路径

action="<%=request.getContextPath()%>/RegistServlet"

邮箱正则表达式

参考:https://www.cnblogs.com/lst619247/p/9289719.html

var reg = new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");

BeanUtils的使用

//获取所有请求参数
Map<String, String[]> map = request.getParameterMap();
User user = new User();
try {
    //封装为对象
    BeanUtils.populate(user, map);
} catch (IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
}

druid+JdbcTemplate的使用

封装一个JDBC工具类,利用druid产生DataSource对象。

/**
 * @auther Summerday
 * JDBC工具类
 */
public class JDBCUtils {

    //datasource对象
    private static DataSource ds;

    //加载配置文件
    static{
        try {
            //加载配置文件
            Properties prop = new Properties();
            //使用classloader加载配置文件,获取字节输入流
            InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            prop.load(in);
            //初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(prop);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接池对象
     */
    public static DataSource getDataSource(){
        return ds;
    }
    /**
     * 获取连接
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}

利用DataSource对象创建JdbcTemplate对象。

//dao    
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

以下参考:https://www.cnblogs.com/wangyujun/p/10687780.html

    //利用template对象添加数据
    public void Insert(User user){
        String sql = "insert into user values(null,?,?,?,?)";
        int update = template.update(sql, user.getUsername(), user.getPassword(), user.getNickname(), user.getEmail());
    }
//查询字段值并封装成对象
String sql = "select * from user where name = ? and password = ?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class),loginUser.getUsername(), loginUser.getPassword());

实用日期使用

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
String s = sdf.format(date);
System.out.println(s);//2020-03-17 22:28:34

IDEA F8debug失效

找到原因:有道词典F8取词功能冲突,关闭即可。

码云提交不显示贡献

找到原因:因为git绑定的邮箱与码云提交邮箱不统一。

发布了144 篇原创文章 · 获赞 121 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Sky_QiaoBa_Sum/article/details/104965167
今日推荐