PLANET_08
PLANET_08
采纳率100%
2019-02-28 11:13

Springboot controller问题

5
已采纳

目前自己搭建了一个OA系统,还在搭建中,然后前端使用的是thymeleaf,我把前端的公共页面head,foot,和左边导航栏全部提取出来,放在IndexController中返回,然后head的导航栏上有一个天气实时展示,主页的时候能展示,但是我打开其他界面就不能展示,其他界面在不同的controller里面,请问一下这种是什么情况,希望大神帮忙解决!


下面是打开其他页面天气预报无响应代码

@Controller
@RequestMapping("/")
public class CustomerController {

    @Resource
    private IOaCustomerInfoService oaCustomerInfoService;

    /**
     * 客户数据展示
     */
    @GetMapping("/customer")
    public String allCustomer(Model model) {
        List<OaCustomerInfo> allList = oaCustomerInfoService.getAllCustomerInfo();
        model.addAttribute("customers",allList);
        return "more/customer";
    }
}

下面是主页可以出现天气预报的代码

@Controller
@RequestMapping("/")
public class IndexController {


    @Resource
    private IOaCustomerService oaCustomerService;


    @Resource
    private IOaCityCodeService oaCityCodeService;

    @Resource
    private IOaCustomerInfoService oaCustomerInfoService;

    @Resource
    private IOaUserPerfService oaUserPerfService;

    /**
     * 主页
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }

    /**
     * 公共头head页面
     */
    @RequestMapping(value = "/head")
    public String test() {
        return "head";
    }

    /**
     * 公共左导航栏left页面
     */
    @RequestMapping(value = "/left")
    public String left() {
        return "left";
    }

    /**
     * 公共尾部foot页面
     */
    @RequestMapping(value = "/foot")
    public String foot() {
        return "foot";
    }


    /**
     * 账户设置界面
     */
    @RequestMapping(value = "/settings")
    public String settings() {
        return "more/settings";
    }


    /**
     * 网页头部Head天气预报展示
     *
     * @param request
     * @param model
     */

//    @GetMapping(value = "/head")
    @ModelAttribute
    public void addressAndWeather(HttpServletRequest request, Model model) {

        String ip = IPUtil.getIpAddrByRequest(request);
        System.out.println("登录IP:" + ip);
        JSONObject address = AddressAndWeatherUtils.returnAddress(ip);
        String cityName = address.getString("city");
        System.out.println("城市:" + cityName);
        cityName = cityName.substring(0, cityName.length() - 1);
        OaCityCode code = oaCityCodeService.getCodeByName(cityName);
        try {
            String str = AddressAndWeatherUtils.returnWeatherJson(code.getCityCode().toString());
            JSONObject weatherJson = JSONObject.parseObject(str);
            JSONObject today = weatherJson.getJSONObject("data").getJSONArray("forecast").getJSONObject(0);
            String high = today.getString("high");
            String low = today.getString("low");
            // 截掉多余字符
            high = high.substring(3);
            low = low.substring(3);
            String returnWeb = cityName + " " + low + "~" + high;
            model.addAttribute("weather", returnWeb);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

html

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>客户管理</title>
    <link th:href="@{/vendor/simple-line-icons/css/simple-line-icons.css}" rel="stylesheet"/>
    <link th:href="@{/vendor/font-awesome/css/fontawesome-all.min.css}" rel="stylesheet"/>
    <link th:href="@{/css/styles.css}" rel="stylesheet"/>
    <link th:href="@{/css/customerButton.css}" rel="stylesheet"/>
</head>
<body class="sidebar-fixed header-fixed">
<div th:replace="head::header-body"></div>
<div th:replace="left::left-body"></div>
<div class="page-wrapper">
    <div class="content">

        <div class="col-md-12">
            <div class="card">
                <div class="card-header bg-light">
                    客户管理
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <button type="button" class="btn btn-primary" ><span class="fa fa-plus-square"></span> 客户新增
                        </button>
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>姓名</th>
                                <th>手机号</th>
                                <th>添加日期</th>
                                <th>修改日期</th>
                                <th>意向度</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr th:each="customer,iterStat:${customers}">
                                <td th:text="${iterStat.count}"></td>
                                <td th:text="${customer.occupation}"></td>
                                <td class="text-nowrap" th:text="${customer.tel}"></td>
                                <td th:text="${#dates.format(customer.opentime,'yyyy-MM-dd HH:mm:ss')}"></td>
                                <td th:text="${#dates.format(customer.updatetime,'yyyy-MM-dd HH:mm:ss')}"></td>
                                <td th:text="${customer.intention}"></td>
                                <td>
                                    <button type="button" class="customerbuttonGreen"><span class="fa fa-search"></span>
                                        跟进详情
                                    </button>
                                    <button type="button" class="customerbutton"><span class="fa fa-eraser"></span>
                                        修改跟进
                                    </button>
                                    <button type="button" class="customerbuttonRed"><span class="fa fa-trash"></span>
                                        删除客户
                                    </button>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div th:replace="foot::foot-body"></div>
    </div>
</div>
<script th:src="@{/vendor/jquery/jquery.min.js}"></script>
<script th:src="@{/vendor/popper.js/popper.min.js}"></script>
<script th:src="@{/vendor/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/vendor/chart.js/chart.min.js}"></script>
<script th:src="@{/js/carbon.js}"></script>
<script th:src="@{/js/demo.js}"></script>
</body>
</html>

公共类

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>head</title>
    <link th:href="@{/vendor/simple-line-icons/css/simple-line-icons.css}" rel="stylesheet"/>
    <link th:href="@{/vendor/font-awesome/css/fontawesome-all.min.css}" rel="stylesheet"/>
    <link th:href="@{/css/styles.css}" rel="stylesheet"/>
</head>
<body>
<div th:fragment="header-body">
    <nav class="navbar page-header">
        <a href="#" class="btn btn-link sidebar-mobile-toggle d-md-none mr-auto">
            <i class="fa fa-bars"></i>
        </a>

        <a class="navbar-brand" href="/index">
            <img th:src="@{/img/logo.png}" alt="logo">
        </a>

        <a href="#" class="btn btn-link sidebar-toggle d-md-down-none">
            <i class="fa fa-bars"></i>
        </a>

        <ul class="navbar-nav ml-auto">
            <li class="nav-item d-md-down-none">
                <a href="#">
                    <i class="fa fa-location-arrow"></i>
                    <span class="small ml-1 d-md-down-none">
                        <span style="padding-right:20px;" href="#" th:text="${weather}"></span>
                    </span>
                </a>
            </li>

            <li class="nav-item d-md-down-none">
                <a href="#">
                    <i class="fa fa-bell"></i>
                    <span class="badge badge-pill badge-danger">5</span>
                </a>
            </li>

            <li class="nav-item d-md-down-none">
                <a href="#">
                    <i class="fa fa-envelope-open"></i>
                    <span class="badge badge-pill badge-danger">4</span>
                </a>
            </li>

            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown"
                   aria-haspopup="true"
                   aria-expanded="false">
                    <img th:src="@{/img/avatar-1.png}" class="avatar avatar-sm" alt="logo">
                    <span class="small ml-1 d-md-down-none">xx,欢迎登录</span>
                </a>

                <div class="dropdown-menu dropdown-menu-right">
                    <div class="dropdown-header">账户</div>

                    <a href="#" class="dropdown-item">
                        <i class="fa fa-bell"></i> 通知
                    </a>

                    <a href="#" class="dropdown-item">
                        <i class="fa fa-envelope"></i> 邮件
                    </a>

                    <a href="#" class="dropdown-item">
                        <i class="fa fa-bell"></i> 工资详情
                    </a>

                    <div class="dropdown-header">设置</div>

                    <a href="#" class="dropdown-item">
                        <i class="fa fa-user"></i> 个人资料
                    </a>

                    <a href="/settings" class="dropdown-item">
                        <i class="fa fa-wrench"></i> 设置
                    </a>

                    <a href="#" class="dropdown-item">
                        <i class="fa fa-lock"></i> 注销
                    </a>
                </div>
            </li>
        </ul>
    </nav>
</div>
<script th:src="@{/vendor/jquery/jquery.min.js}"></script>
<script th:src="@{/vendor/popper.js/popper.min.js}"></script>
<script th:src="@{/vendor/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/vendor/chart.js/chart.min.js}"></script>
<script th:src="@{/js/particles.min.js}"></script>
<script th:inline="javascript">
    $(function () {
        var weather = [[${addressAndWeather}]];
    })
</script>
</body>

</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • a804509721 a804509721 2年前

    你把你写的那些addressAndWeather这个方法 写在这儿 /**
    * 公共头head页面
    */
    @RequestMapping(value = "/head")
    public String test() {
    return "head";
    }你的model需要和页面一起返回

        /**
     * 公共头head页面
     */
    @RequestMapping(value = "/head")
    public String test(HttpServletRequest request, Model model) {
        String ip = IPUtil.getIpAddrByRequest(request);
        System.out.println("登录IP:" + ip);
        JSONObject address = AddressAndWeatherUtils.returnAddress(ip);
        String cityName = address.getString("city");
        System.out.println("城市:" + cityName);
        cityName = cityName.substring(0, cityName.length() - 1);
        OaCityCode code = oaCityCodeService.getCodeByName(cityName);
        try {
            String str = AddressAndWeatherUtils.returnWeatherJson(code.getCityCode().toString());
            JSONObject weatherJson = JSONObject.parseObject(str);
            JSONObject today = weatherJson.getJSONObject("data").getJSONArray("forecast").getJSONObject(0);
            String high = today.getString("high");
            String low = today.getString("low");
            // 截掉多余字符
            high = high.substring(3);
            low = low.substring(3);
            String returnWeb = cityName + " " + low + "~" + high;
            model.addAttribute("weather", returnWeb);
        return "head";
    }
    
    点赞 评论 复制链接分享
  • sd1209286540 sd1209286540 2年前

    你说的是在首页里面添加公共页面吧,数据可以在主页的controller中加载,但是公共页面是在前端加载的,应该用这种

    点赞 评论 复制链接分享
  • baidu_33403616 再写三行 2年前

    注解用错了吧 你其他页面请求如果是post 用postMapping get用getMapping

    点赞 评论 复制链接分享
  • weixin_42920757 小小菜鸟2020 2年前

    贴一下打开其他页面的时候,浏览器的console界面,看看页面有没有拿到数据。

    点赞 评论 复制链接分享
  • qq_34872748 L星火燎原 2年前

    你要把代码截图出来才好分析,不然看不懂你所表达的意思。

    点赞 评论 复制链接分享

为你推荐