完成一个IDEA web项目项目搭建准备工作

Posted musecho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成一个IDEA web项目项目搭建准备工作相关的知识,希望对你有一定的参考价值。

一、 项目搭建准备工作


1. 创建maven web项目,配置Tomcat


2. 测试项目是否跑得起来


3. 导入项目中会遇到的jar包(maven依赖)

  • servlet,jsp,mysql,jstl依赖

    去这个网站找:https://mvnrepository.com/

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- JSTL表达式的依赖 -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- standard标签库 -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>
    

4. 创建项目包结构

  • 项目结构(忽略我的jsp和html文件)


5. 连接数据库


6. 编写实体类


7. 编写基础公共类

  1. 数据库配置文件

    (我的编码全是gbk)

    driver = com.mysql.jdbc.Driver
    url= jdbc:mysql://localhost:3306/TutorSelectSys?useUnicode=true&characterEncoding=gbk
    user = root
    password = root
    
  2. 编写数据库的公共类BaseDao

    package dao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    /**
     * 操作数据库的公共类
     */
    public class BaseDao {
        private static String driver;
        private static String url;
        private static String user;
        private static String password;
    
        static {
            Properties properties = new Properties();
            //通过类加载器读取对应的资源
            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
    
            try {
                properties.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
        }
    
        //获取数据库的链接
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        //查询公共方法
        public static ResultSet executeQuery(Connection conn, String sql, Object[] params, PreparedStatement ps, ResultSet rs) throws SQLException {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //注意:setObject占位符从1开始,数组从0开始
                ps.setObject(i + 1, params[i]);
            }
    
            rs = ps.executeQuery();
            return rs;
        }
    
        //增删改公共方法
        public static int executeUpdate(Connection conn, String sql, Object[] params, PreparedStatement ps) throws SQLException {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //注意:setObject占位符从1开始,数组从0开始
                ps.setObject(i + 1, params[i]);
            }
    
            int updateRows = ps.executeUpdate();
            return updateRows;
        }
    
        //释放资源
        public static boolean closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
            boolean flag = true;
    
            if (rs != null) {
                try {
                    rs.close();
                    //GC回收
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if (ps != null) {
                try {
                    ps.close();
                    //GC回收
                    ps = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if (conn != null) {
                try {
                    conn.close();
                    //GC回收
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            return flag;
        }
    
    
    }
    

  3. 编写字符编码过滤器

    package filter;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    /**
     * 字符编码过滤器
     */
    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("gbk");
            servletResponse.setCharacterEncoding("gbk");
    
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

8. 导入静态资源

js,css等


9. 编写字符编码过滤器(哪步写都行)

  • CharacterEncodingFilter

    @WebFilter("/*")
    public class CharacterEncodingFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("gbk");
            servletResponse.setCharacterEncoding("gbk");
    
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

以上是关于完成一个IDEA web项目项目搭建准备工作的主要内容,如果未能解决你的问题,请参考以下文章

用IDEA将普通Web项目转Maven项目

用IDEA将普通Web项目转Maven项目

idea+springboot+Mybatis搭建web项目

IntelliJ IDEA 创建项目

IDEA搭建Maven Web(SSM)项目——创建项目

Intellij Idea搭建java web项目(问题总结)