使用MVC模式开发一简单的销售额查询系统

与上一篇比较,只改变了index.jsp文件中form的提交路径 <form action="ShowServlet" method="post">

show.jsp文件去掉了java代码,引入了c标签来完成:

<c:if test="${not empty list}">
      <c:forEach items="${list}" var="sales">
       <tr>
        <td>${sales.salestime }</td>
        <td>${sales.salesnum }</td>
       </tr>
      </c:forEach>
     </c:if>
     <c:if test="${empty list}"></c:if>
     <tr>
      <td colspan="2">暂时没有数据!</td>
     </tr>

ShowServlet文件:

package com.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.SalesDao;
import com.vo.Sales;

public class ShowServlet extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  String month = req.getParameter("month");
  SalesDao dao = new SalesDao();
  List<Sales> list = dao.find(month);
  req.setAttribute("list", list);
  req.getRequestDispatcher("show.jsp").forward(req, resp);
 }
 
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req, resp);
 }
}

配置web.xml文件:

<servlet>
   <servlet-name>sales</servlet-name>
   <servlet-class>com.service.ShowServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>sales</servlet-name>
   <url-pattern>/ShowServlet</url-pattern>
  </servlet-mapping>

其他的如dao包和vo包都没有改变。

MVC中,控制器C由Servlet实现,视图V的角色由JSP页面实现,模型M的角色由JavaBean实现。

原文地址:https://www.cnblogs.com/dyllove98/p/3198815.html