从零开始写javaweb框架笔记9-细节完善与代码优化-完善控制器层

来源:互联网 发布:qt初学者 知乎 编辑:程序博客网 时间:2024/05/11 05:21

       在前面的笔记中完善了服务层的代码,在这里我们将进行控制器层代码的完善。以CustomerServlet为例,目前的代码框架是这样的:

package org.jack.com.controller;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Created by jack on 2015/11/29. * 进入客户列表界面 */@WebServlet("customer")public class CustomerServlet extends HttpServlet{    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //to do something    }}
   根据需求,当用户进入“客户列表”后,可看到所有的客户信息,并以表格形式来展现。我们要在CustomerServlet中创建一个CustomerService对象,并通过该对象的getCustomerList获取所有的客户列表。

    修改CustomerServlet后的代码如下:

package org.jack.com.controller;import org.jack.com.model.Customer;import org.jack.com.service.CustomerService;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.List;/** * Created by jack on 2015/11/29. * 进入客户列表界面 */@WebServlet("customer")public class CustomerServlet extends HttpServlet{    private CustomerService customerService;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //to do something        List<Customer> customerList=customerService.getCustomerList();        req.setAttribute("customerList",customerList);        req.getRequestDispatcher("/WEB-INF/view/customer.jsp").forward(req,resp);    }    @Override    public void init() throws ServletException {        customerService=new CustomerService();    }}

      在CustomerServlet中定义一个CustomerService成员变量,并在Servlet的init方法中进行初始化。这样就可以避免创建多个CustomerService实例,其实整个Web应用中只需要一个CustomerService实例(后面使用单例模式进行优化)。

      在doGet方法中,我们调用了CustomerService对象的个体CustomerList方法来获取List对象,并将其放入请求属性中,最后通过请求对象在内部重定向到customer.jsp视图。

    在编写customer.jsp视图之前,我们还需要在pox.xml中添加jsp和jstl的依赖如下:

<!-- JSP -->        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.2</version>            <scope>provided</scope>        </dependency>        <!-- JSTL -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>            <scope>runtime</scope>        </dependency>

   现在整个pox.xml的代码配置如下所示:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>org.jack.com</groupId>    <artifactId>chapter2</artifactId>    <version>1.0.0</version>    <packaging>war</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <!--Servlet-->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.1.0</version>            <scope>provided</scope>        </dependency>        <!-- JSP -->        <dependency>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>            <version>2.2</version>            <scope>provided</scope>        </dependency>        <!-- JSTL -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>            <scope>runtime</scope>        </dependency>        <!--JUnit-->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <!--<version>3.8.1</version>-->            <version>4.11</version>            <scope>test</scope>        </dependency>        <!--SLF4J-->        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.7.7</version>        </dependency>        <!--Mysql-->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.33</version>            <scope>runtime</scope>        </dependency>        <!--Apache Commons Lang-->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.3.2</version>        </dependency>        <!--Apache Commons Collections-->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-collections4</artifactId>            <version>4.0</version>        </dependency>        <!--Apache Commons DbUtils-->        <dependency>            <groupId>commons-dbutils</groupId>            <artifactId>commons-dbutils</artifactId>            <version>1.6</version>        </dependency>        <!--Apache DBCP数据库连接池依赖-->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-dbcp2</artifactId>            <version>2.0.1</version>        </dependency>    </dependencies></project>

     在下一节中,我们开始完善视图层。



0 0
原创粉丝点击