Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include

原文地址:http://www.work100.net/training/monolithic-project-iot-cloud-admin-manager.html
更多教程:光束云 - 免费课程

使用include

序号 文内章节 视频
1 功能模块细分 -
2 新建ManagerController控制器 -
3 新建manager_list.jsp页面 -
4 运行并查看效果 -
5 通过include引入公用文件 -
6 实例源码 -

请参照如上章节导航进行阅读

1.功能模块细分

接下来实现 后台账户管理 功能,我们继续完善 iot-cloud-web-admin 项目。

首先,我们将功能模块细化为:authiot,将涉及账户相关功能放在 auth 模块下,将涉及物联网相关功能放在 iot 模块下。

然后,构建模块结构:

  • net.work100.training.stage2.iot.cloud.web.admin.web.controller 包下新增 authiot
  • webapp/WEB-INF/views 文件夹下新增 authiot 文件夹

2.新建ManagerController控制器

web.controller.auth 包下新建 ManagerController 类,代码如下:

package net.work100.training.stage2.iot.cloud.web.admin.web.controller.auth;

import net.work100.training.stage2.iot.cloud.web.admin.service.AuthManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * <p>Title: ManagerController</p>
 * <p>Description: </p>
 * <p>Url: http://www.work100.net/training/monolithic-project-iot-cloud-admin.html</p>
 *
 * @author liuxiaojun
 * @date 2020-03-01 14:01
 * ------------------- History -------------------
 * <date>      <author>       <desc>
 * 2020-03-01   liuxiaojun     初始创建
 * -----------------------------------------------
 */
@Controller
@RequestMapping(value = "auth/manager")
public class ManagerController {

    @Autowired
    private AuthManagerService authManagerService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(){
        return "auth/manager_list";
    }
}

说明

ManagerController 类上的注解 @RequestMapping(value = "auth/manager")

  • 作用:控制器下的所有方法的请求路径都将增加前缀,即: auth/manager/*
  • 举例:list 方法,请求路径为:auth/manager/list

3.新建manager_list.jsp页面

views/auth/ 文件夹下新建 manager_list.jsp 页面,代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后台账户</title>
</head>
<body>
    账户列表
</body>
</html>

4.运行并查看效果

启动 Tomcat 登录后,点击菜单 后台账户 - 查新列表,界面如下图:

Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include

5.通过include引入公用文件

5.1.通用布局

参照我们的 首页布局,可见界面布局上有一些通用部分,如下图:

Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include

其中红色区域为每个页面都包含的,蓝色区域为每个页面特有的。

既然每个页面都包含红色区域的 通用部分,那么我们就需要将其提取出来,这里将使用 <jsp:include > 来实现。

5.2.提取通用布局到文件

views 文件夹下新增一个 includes 文件夹,然后提取下表的通用内容为文件,并存储到 includes 下:

文件位置 作用
includes/resources_head.jsp 在页面 header 标签中引入 资源
includes/resources_body.jsp 在页面 body 标签中引入 资源
includes/layout_header.jsp 页面顶部通用部分
includes/layout_left.jsp 页面左侧通用部分(菜单)
includes/layout_footer.jsp 页面底部通用部分(版权信息)

resources_head.jsp 代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="/static/assets/plugins/fontawesome-free/css/all.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="/static/assets/css/adminlte.min.css">
<link rel="icon" href="/static/assets/img/favicon.ico">

resources_body.jsp 代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- REQUIRED SCRIPTS -->

<!-- jQuery -->
<script src="/static/assets/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="/static/assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE -->
<script src="/static/assets/js/adminlte.js"></script>

layout_header.jsp 代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!-- Navbar -->
<nav class="main-header navbar navbar-expand navbar-white navbar-light">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
        <li class="nav-item">
            <a class="nav-link" data-widget="pushmenu" href="#"><i class="fas fa-bars"></i></a>
        </li>
        <li class="nav-item d-none d-sm-inline-block">
            <a href="/" class="nav-link">首页</a>
        </li>
        <li class="nav-item d-none d-sm-inline-block">
            <a href="/auth/manager/list" class="nav-link">后台账户</a>
        </li>
        <li class="nav-item d-none d-sm-inline-block">
            <a href="/auth/tenant/list" class="nav-link">租户</a>
        </li>
    </ul>

    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">
        <li class="nav-item dropdown">
            <a class="nav-link" data-toggle="dropdown" href="#">
                <i class="nav-icon fas fa-user-circle"></i>
                ${manager.userName}
                <i class="fas fa-angle-down right"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-md dropdown-menu-right">
                <a href="/my/profile" class="dropdown-item">
                    <i class="fas fa-address-card mr-2"></i>我的资料
                </a>
                <div class="dropdown-divider"></div>
                <a href="/my/change-password" class="dropdown-item">
                    <i class="fas fa-lock mr-2"></i>修改密码
                </a>
                <div class="dropdown-divider"></div>
                <a href="/logout" class="dropdown-item">
                    <i class="fas fa-sign-out-alt mr-2"></i>退出
                </a>
            </div>
        </li>
    </ul>
</nav>
<!-- /.navbar -->

layout_left.jsp 代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- Brand Logo -->
    <a href="/" class="brand-link">
        <img src="/static/assets/img/logo-128x128.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3"
             style="opacity: .8">
        <span class="brand-text font-weight-light">IoT-Admin</span>
    </a>

    <!-- Sidebar -->
    <div class="sidebar">
        <!-- Sidebar Menu -->
        <nav class="mt-2">
            <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
                <!-- Add icons to the links using the .nav-icon class
                     with font-awesome or any other icon font library -->
                <li class="nav-item">
                    <a href="/main" class="nav-link">
                        <i class="nav-icon fas fa-tachometer-alt"></i>
                        <p>
                            控制台
                        </p>
                    </a>
                </li>
                <li class="nav-item has-treeview">
                    <a href="#" class="nav-link">
                        <i class="nav-icon fas fa-user"></i>
                        <p>
                            后台账户
                            <i class="fas fa-angle-left right"></i>
                        </p>
                    </a>
                    <ul class="nav nav-treeview">
                        <li class="nav-item">
                            <a href="/auth/manager/add" class="nav-link">
                                <i class="far fa-edit nav-icon"></i>
                                <p>新增</p>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="/auth/manager/list" class="nav-link">
                                <i class="far fa-list-alt nav-icon"></i>
                                <p>查询列表</p>
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="nav-item has-treeview">
                    <a href="#" class="nav-link">
                        <i class="nav-icon fas fa-store-alt"></i>
                        <p>
                            租户
                            <i class="fas fa-angle-left right"></i>
                        </p>
                    </a>
                    <ul class="nav nav-treeview">
                        <li class="nav-item">
                            <a href="/auth/tenant/add" class="nav-link">
                                <i class="far fa-edit nav-icon"></i>
                                <p>新增</p>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="/auth/tenant/list" class="nav-link">
                                <i class="far fa-list-alt nav-icon"></i>
                                <p>查询列表</p>
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="nav-item has-treeview">
                    <a href="#" class="nav-link">
                        <i class="nav-icon fas fa-user-friends"></i>
                        <p>
                            租户账户
                            <i class="fas fa-angle-left right"></i>
                        </p>
                    </a>
                    <ul class="nav nav-treeview">
                        <li class="nav-item">
                            <a href="/auth/tenant/user/add" class="nav-link">
                                <i class="far fa-edit nav-icon"></i>
                                <p>新增</p>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="/auth/tenant/user/list" class="nav-link">
                                <i class="far fa-list-alt nav-icon"></i>
                                <p>查询列表</p>
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </nav>
        <!-- /.sidebar-menu -->
    </div>
    <!-- /.sidebar -->
</aside>

<!-- Control Sidebar -->
<aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
</aside>
<!-- /.control-sidebar -->

layout_footer.jsp 代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!-- Main Footer -->
<footer class="main-footer">
    <strong>Copyright &copy; 2019-2020 <a href="http://www.work100.net">光束云</a>.</strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
        <b>Version</b> 1.0.0-beta
    </div>
</footer>

5.3.重构 main.jsp

main.jsp 文件的通用部分替换为我们提取出来的 includes 文件,重构后的 main.jsp 文件代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>控制台 | IoT-Admin</title>
    <jsp:include page="includes/resources_head.jsp" />
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">

    <jsp:include page="includes/layout_header.jsp" />

    <jsp:include page="includes/layout_left.jsp" />

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">控制台</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">首页</a></li>
                            <li class="breadcrumb-item active">控制台</li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-header border-0">
                                <div class="d-flex justify-content-between">
                                    <h3 class="card-title">图表1</h3>
                                    <a href="javascript:void(0);">查看</a>
                                </div>
                            </div>
                            <div class="card-body">
                                图表1-内容
                            </div>
                        </div>
                        <!-- /.card -->

                        <div class="card">
                            <div class="card-header border-0">
                                <h3 class="card-title">表格1</h3>
                                <div class="card-tools">
                                    <a href="#" class="btn btn-tool btn-sm">
                                        <i class="fas fa-download"></i>
                                    </a>
                                    <a href="#" class="btn btn-tool btn-sm">
                                        <i class="fas fa-bars"></i>
                                    </a>
                                </div>
                            </div>
                            <div class="card-body">
                                表格1-内容
                            </div>
                        </div>
                        <!-- /.card -->
                    </div>
                    <!-- /.col-md-6 -->
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-header border-0">
                                <div class="d-flex justify-content-between">
                                    <h3 class="card-title">图表2</h3>
                                    <a href="javascript:void(0);">查看</a>
                                </div>
                            </div>
                            <div class="card-body">
                                图表2-内容
                            </div>
                        </div>
                        <!-- /.card -->

                        <div class="card">
                            <div class="card-header border-0">
                                <h3 class="card-title">表格2</h3>
                                <div class="card-tools">
                                    <a href="#" class="btn btn-sm btn-tool">
                                        <i class="fas fa-download"></i>
                                    </a>
                                    <a href="#" class="btn btn-sm btn-tool">
                                        <i class="fas fa-bars"></i>
                                    </a>
                                </div>
                            </div>
                            <div class="card-body">
                                表格2-内容
                            </div>
                        </div>
                    </div>
                    <!-- /.col-md-6 -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <jsp:include page="includes/layout_footer.jsp" />
</div>
<!-- ./wrapper -->
<jsp:include page="includes/resources_body.jsp" />

</body>
</html>

5.4.重构 auth/manager_list.jsp

将通用布局文件包含到 auth/manager_list.jsp 文件中,代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>查询列表 | 后台账户 | IoT-Admin</title>
    <jsp:include page="../includes/resources_head.jsp" />
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">

    <jsp:include page="../includes/layout_header.jsp" />

    <jsp:include page="../includes/layout_left.jsp" />

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">查询列表</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">后台账户</a></li>
                            <li class="breadcrumb-item active">查询列表</li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">
                列表内容
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <jsp:include page="../includes/layout_footer.jsp" />
</div>
<!-- ./wrapper -->
<jsp:include page="../includes/resources_body.jsp" />

</body>
</html>

重构后的页面效果如下图:

Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include

6.实例源码

实例源码已经托管到如下地址:


上一篇:首页布局 - Java单体应用 - 免费课程 - 光束云 - work100.net")

下一篇:账户列表展示


如果对课程内容感兴趣,可以扫码关注我们的 公众号QQ群,及时关注我们的课程更新

Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include
Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 01.使用include

上一篇:《自己动手写docker》之namespace部门实验


下一篇:HTTP状态码的含义: 200:400:403:404:408:500:503:504