JavaWeb小教务系统及MVC架构理解

1、结合一个小的教务系统与JavaWeb基本知识 加深了MVC架构及JavaWeb项目的理解

 

 

2、MVC架构:Model数据层、View展示层、Controller控制层

1)Model层

① 实体类bean pojo

  • 例如这里的 Person实体类 Task实体类 用作封装用户信息

public class Task {
    private String taskAccount;
    private String teacherAccount;
    private String taskDate;
    private String taskName;
​
    public Task() {
    }
​
    public Task(String taskAccount, String teacherAccount, String taskDate, String taskName) {
        this.taskAccount = taskAccount;
        this.teacherAccount = teacherAccount;
        this.taskDate = taskDate;
        this.taskName = taskName;
    }
​
    @Override
    public String toString() {
        return "Task{" +
                "taskAccount='" + taskAccount + '\'' +
                ", teacherAccount='" + teacherAccount + '\'' +
                ", taskDate='" + taskDate + '\'' +
                ", taskName='" + taskName + '\'' +
                '}';
    }
​
    public String getTaskAccount() {
        return taskAccount;
    }
​
    public void setTaskAccount(String taskAccount) {
        this.taskAccount = taskAccount;
    }
​
    public String getTeacherAccount() {
        return teacherAccount;
    }
​
    public void setTeacherAccount(String teacherAccount) {
        this.teacherAccount = teacherAccount;
    }
​
    public String getTaskDate() {
        return taskDate;
    }
​
    public void setTaskDate(String taskDate) {
        this.taskDate = taskDate;
    }
​
    public String getTaskName() {
        return taskName;
    }
​
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }
}

②持久层Dao

  • 例如这里的StudentDao TeacherDao UserLoginDao接口及相应实现类

 

  • Dao 只负责数据库的 crud 没有其他业务逻辑

public class UserLoginDaoImpl implements UserLoginDao {
    @Override
    public Person getLogin(Person person) throws SQLException {
        //数据库 持久层 具体操作
        String sql = " select * from person where userAccount= '" + person.getUserAccount() + "' and userPassword='" + person.getUserPassword() + "'";
        Connection connection = DBUtil.getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        Person newPerson = null;
​
        try {
            pre = connection.prepareStatement(sql);
            res = pre.executeQuery();
            while (res.next()) {
                newPerson = new Person();
                newPerson.setUserAccount(res.getString("userAccount"));
                newPerson.setUserName(res.getString("userName"));
                newPerson.setUserSex(res.getInt("userSex"));
//                newPerson.setUserBirthday(res.getString("userBirthday"));
                newPerson.setUserIdCard(res.getString("userIdCard"));
                newPerson.setUserPassword(res.getString("userPassword"));
                newPerson.setUserIdentify(res.getInt("userIdentify"));
                newPerson.setUserOtherName(res.getString("userOtherName"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return newPerson;
    }
}
public class TeacherDaoImpl implements TeacherDao {
​
    @Override
    public List getAllStudentByTeacherAccount(String userAccount) throws SQLException {
​
        String sql = "SELECT * from person WHERE userAccount IN(SELECT userAccount FROM userteacher " +
                "WHERE teacherAccount = '" + userAccount + "')";
        return Deal.getAllPerson(sql);
    }
​
    @Override
    public List<Task> getAllMyTask(String userAccount) throws SQLException {
        /*先在数据库中基本执行 在通过代码动态调整*/
        String sql = "SELECT * FROM task WHERE teacherAccount = '"+userAccount+"'";
        return Deal.getAllMyTask(sql);
    }
}

③ 业务层service

  • 创建相应service接口与其实现类 例如UserLoginService UserLoginServiceImpl

public interface UserLoginService {
    Person getLogin(Person person) throws Exception;
}
public class UserLoginServiceImpl implements UserLoginService {
​
    //创建持久层代码
    UserLoginDaoImpl ud = new UserLoginDaoImpl();
    @Override
    public Person getLogin(Person person) throws Exception {
        return ud.getLogin(person);
    }
}

2)Controller层

  • Servlet 例如下面的UserLoginServlet

@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、首先拿到前端传来的数据
        String userAccount = request.getParameter("userAccount");
        String userPassword = request.getParameter("userPassword");
        Person person = new Person(userAccount, userPassword);
        UserLoginServiceImpl ul = new UserLoginServiceImpl();
        Person newPerson = null;
        try {
            newPerson = ul.getLogin(person);
            if (newPerson != null) {
                HttpSession session = request.getSession();
                session.setAttribute("session_person", newPerson);
//                request.setAttribute("person",newPerson);
                //进入主界面 跳转到main.jsp
                request.setAttribute("mainRight", "blank.jsp");
                request.getRequestDispatcher("main.jsp").forward(request, response);
​
            } else {
                //继续回到注册界面
                //从后端传回给前端
                //这里  void setAttribute(String var1, Object var2); 采用键值对的形式
                // tip 传回去给后端 后端用${tip} 接受
                request.setAttribute("tip", "账户密码不一致");
                request.getRequestDispatcher("userLogin.jsp").forward(request, response);
            }
​
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3)View层

  • jsp 例如下面的 userlogin.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/5/21
  Time: 15:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录界面</title>
    <link rel="stylesheet" type="text/css" href="css/userLogin.css">
</head>
<body>
    <div id="outerLoginFrame">
        <h1>学生管理系统</h1>
        <div id="innerLoginFrame">
​
            <form action="userLogin" method="post" onclick="return checkLogin()">
                账户:<input type="text" id="userAccount" name="userAccount" class="myText"><br>
                密码:<input type="text" id="userPassword" name="userPassword" class="myText"><br>
                <%--登录--%>
                <input style="width: 140px;margin-left: 40px" type="submit" value="登录">
                <a href="register,jsp" style="width: 140px;margin-left: 20px">注册</a>
                <%--//接受前端传回来的数据--%>
                <span style="color: red;font-size: 15px"  id="tip">${tip}</span>
            </form>
​
        </div>
    </div>
​
</body>
</html>
​
<script>
    function checkLogin(){
        let userAccount = document.getElementById("userAccount").value;
        let userPassword = document.getElementById("userPassword").value;
        if (userAccount==null||userAccount.trim()==""){
            // alert("账户不能为空!");
            document.getElementById("tip").innerHTML = "账户不能为空"
            return false;
        }
​
        if (userPassword==null||userPassword.trim()==""){
            // alert("密码不能为空!");
            document.getElementById("tip").innerHTML = "密码不能为空"
            return false;
        }
​
        return true;
    }
</script>

3、引出 SSM:Spring SpringMVC MyBatis

  • Spring:项目管理 所有对象的创建及维护对象与对象间的关系

  • SpringMVC:搭建MVC架构

  • MyBatis:持久层框架

4、小tips:

  1. 当动了后端代码 要重新启动项目

  1. 在IDEA中导入JSTL标签库_jstl怎么导入

在IDEA中导入JSTL标签库_jstl怎么导入_良辰执念的博客-CSDN博客^v88^control,239^v2^insert_chatgpt&utm_term=jaavaweb%E5%AF%BC%E5%85%A5jstl%E5%BA%93&spm=1018.2226.3001.4187

 

  1. 先在数据库中编写查询语句

  1. 再通过代码动态调整

@Override
public List<Task> getAllMyTask(String userAccount) throws SQLException {
    /*先在数据库中基本执行 在通过代码动态调整*/
    String sql = "SELECT * FROM task WHERE teacherAccount = '"+userAccount+"'";
    return Deal.getAllMyTask(sql);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值