Javaweb新手,尝试用MVC模式做一个购物网站,但是今天看一本书里面提到的业务逻辑和Servlet与JavaBean的关系让我对自己之前的做法有了疑惑。
我之前实现各个功能的流程都是这样的(以商品搜索为例):
(GoodDAO是商品相关的数据库操作接口,Good是商品的值对象)
用户从JSP页面发送请求 -> Servlet接收请求 -> Servlet调用GoodDAO中的query方法 -> 该方法返回一个ArrayList
但是今天看到的那本书说要把业务逻辑从Servlet中拿出来,我搜索了一下发现还有一种做法是创建一种叫xxxManager的类(在这里可以叫GoodManager),由Servlet调用该类,该类再调用GoodDAO,Manager最终将数据填入Good实体中返回给Servlet。这就和我做的方法感觉有出入了……
我的Servlet部分代码如下(这个Servlet不是我举例的那个,代码少点但结构类似):
request.setCharacterEncoding("UTF-8"); int gid = Integer.parseInt(request.getParameter("gid")); Good good = null; GoodDAO goodDAO = DAOFactory.getGoodDAOInstance(); HttpSession session = request.getSession(true); try { good=goodDAO.queryByID(gid); session.setAttribute("good", good); response.sendRedirect("goodupdate.jsp"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
我的网站后台结构如下图(省略了很多Servlet):
我的问题是,
我现在的做法是MVC模式吗?
JavaBean在这里仅仅指Good这一个类吗?(它的属性是私有的,提供了getter/setter,可序列化)
GoodDAO的具体实现类算不算是JavaBean?
Yes. In addition, MVC is a design pattern for architecture. Most frameworks support this.
Yes. In addition, I also recommend you two excellent articles to deepen your understanding of Bean. Java beans in the Java Empire (Part 1) and Java beans in the Java Empire (Part 2)
Not strictly speaking.
Others:
I saw that the questioner wanted to recommend a framework to start with. You can try SpringBoot, which is small but comprehensive and can be used out of the box. In addition, there are already corresponding books in China.
1. MVC is mostly a framework, struts/spring
2. Yes
3. The specific implementation class of the interface is the business layer
To answer your questions
1. Yes, but not completely.
2.Yes.
3. According to traditional saying, no. But after the emergence of spring, especially for the IOC concept, this is a bean
Is your package name not mosaic?
It feels like what you are reading are old textbooks. The current approach, even if it is not a framework, is that the servlet is only responsible for receiving values and passing parameters. The specific business logic is extracted to create a service layer, and the dao layer is responsible for connecting to the database. It is recommended to learn springMVC