极客大本营 2019-02-28 11:13 采纳率: 100%
浏览 1545
已采纳

Springboot controller问题

目前自己搭建了一个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条回答 默认 最新

  • 无所谓你好你你好 2019-03-01 15:34
    关注

    你把你写的那些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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀