为了账号安全,请及时绑定邮箱和手机立即绑定

Springboot controller问题

Springboot controller问题

冉冉说 2019-04-26 19:15:07
目前自己搭建了一个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      */
查看完整描述

5 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

你把你写的那些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";
}


查看完整回答
反对 回复 2019-05-16
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

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


查看完整回答
反对 回复 2019-05-16
?
呼唤远方

TA贡献1856条经验 获得超11个赞

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


查看完整回答
反对 回复 2019-05-16
?
绝地无双

TA贡献1946条经验 获得超4个赞

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


查看完整回答
反对 回复 2019-05-16
  • 5 回答
  • 0 关注
  • 489 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号