MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

模型一般有两类,一种是跟数据库的表结构对应的一个类,还有一种,有时候我们页面显示的内容需要多表连接才能查到,这时候就需要定义一个模型类,用来在数据库读取时存放需要的数据。

在一次请求中,服务器将请求信息发送至Servlet,Servlet处理请求并且将响应内容传给服务器。

  因此servlet充当着控制器(controller)的角色。

  jsp页面就是视图(view)层。

  模型层就是用户定义的模型类。

当我们需要在jsp页面显示数据库数据时,一般是先到一个servlet进行取数据处理,把取到的数据设到request中去,然后请求转发到jsp页面,jsp页面通过el表达式即可获取数据。

说的通俗一点就是当我们点击了一个超链接,超链接不要写xxx.jsp,而应该写某个servlet 的url,这个servlet 进行查询数据库处理,把得到的数据设置到request 之后进行forword跳转到jsp页面进行显示。

下面是自己的项目中的一个例子(代码顺序按照执行顺序):

 

当需要访问一个页面时,先请求对应的servlet,例子中是ViewResultDetailServlet:

<td><a href="<%=request.getContextPath()%>/ViewResultDetailServlet?competitionid=<%=officermsg.getId()%>&competitionname=<%=officermsg.getCompetitionname() %>">查看详情</a></td>

ViewResultDetailServlet的代码:

/**
     * 给admin/viewResultDetail.jsp页面的传输数据
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int competitionid = 0;
        String competitionname = null;
        try{
            competitionid=Integer.parseInt(request.getParameter("competitionid"));
            competitionname=request.getParameter("competitionname");
        }catch(Exception e){
            response.sendRedirect(request.getContextPath()+"/admin/viewResultSummary.jsp");
            e.printStackTrace();
        }
        request.setAttribute("competitionid", competitionid);
        request.setAttribute("competitionname", competitionname);
        ArrayList<TeamScoreMsg> teamScoreMsg = TeamApplyMsgDaoImpl.getTeamApplyMsgList(competitionid,2);//按什么排序   1队长学号  2成绩
        request.setAttribute("teamScoreMsg", teamScoreMsg);
        
        request.getRequestDispatcher("admin/viewResultDetail.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

 

viewResultDetail.jsp

<table
                class="table table-border table-bordered table-bg table-hover table-sort">
                <thead>
                    <tr class="text-c">
                        <th width="100px">队长学号</th>
                        <th width="100px">队长</th>
                        <th width="100px">队伍名称</th>
                        <th width="100px">所属学院</th>
                        <th width="200px">队员</th>
                        <th width="100px">队员人数</th>
                        <th width="50px">成绩</th>
                        <th width="50px">获奖信息</th>
                    </tr>
                </thead>
                <tbody>
                     <c:forEach items="${teamScoreMsg }" var="teamScoreMsg">
                    <tr class="text-c">
                        <td>${teamScoreMsg.captainNum}</td>
                        <td>${teamScoreMsg.captainName}</td>
                        <td>${teamScoreMsg.teamName}</td>
                        <td>${teamScoreMsg.xueyuan}</td>
                        <td>
                        <c:forEach items="${teamScoreMsg.members}" var="student">
                        <p>${student.studentnum}&nbsp;&nbsp;${student.name}&nbsp;&nbsp;${student.xueyuan}</p>
                        </c:forEach>
                        </td>
                        <td>${teamScoreMsg.members.size()}</td>
                        <td>${teamScoreMsg.score}</td>
                        <td>${teamScoreMsg.grade}</td>
                    </tr>
                    </c:forEach>             
                    
                </tbody>
                <tfoot>
                    
                </tfoot>
            </table>