servlet - 在MVC模式中 Javabean只是充当值对象的角色吗
天蓬老师
天蓬老师 2017-04-18 10:25:15
0
5
516

Javaweb新手,尝试用MVC模式做一个购物网站,但是今天看一本书里面提到的业务逻辑和Servlet与JavaBean的关系让我对自己之前的做法有了疑惑。

我之前实现各个功能的流程都是这样的(以商品搜索为例):
(GoodDAO是商品相关的数据库操作接口,Good是商品的值对象)
用户从JSP页面发送请求 -> Servlet接收请求 -> Servlet调用GoodDAO中的query方法 -> 该方法返回一个ArrayList的查询结果 -> Servlet将该ArrayList交给JSP(通过session或者request之类的) -> JSP用EL读取并显示搜索结果

但是今天看到的那本书说要把业务逻辑从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?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (5)
迷茫
  1. Yes. In addition, MVC is a design pattern for architecture. Most frameworks support this.

  2. 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)

  3. 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.

    Peter_Zhu

    1. MVC is mostly a framework, struts/spring
    2. Yes
    3. The specific implementation class of the interface is the business layer

      Peter_Zhu

      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

            Latest Downloads
            More>
            Web Effects
            Website Source Code
            Website Materials
            Front End Template
            About us Disclaimer Sitemap
            php.cn:Public welfare online PHP training,Help PHP learners grow quickly!