转自:https://my.oschina.net/scipio/blog/477854
主要解决两个问题:
A、对于mobile的请求和pc端的请求,自动使用不同的模板目录B、对于返回json还是返回ftl,由springMVC自己判断,controller方法只写一个1、maven依赖
org.springframework.mobile spring-mobile-device 1.1.0.RELEASE
2、配置多内容版本
配置.json的请求返回json,其他返回view
3、配置viewResolver
配置mobile来的请求ftl的走mobile目录下模板
4、配置device判断
5.测试
/* * Copyright 2008-2018 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license * FileId: mFgPu27MEMKx2FdJguKD8D8/mA79BmNG */package net.shopxx.controller.shop;import javax.inject.Inject;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import net.shopxx.service.ProductCategoryService;/** * Controller - 商品分类 * * @author SHOP++ Team * @version 6.1 */@Controller("shopProductCategoryController")@RequestMapping("/product_category")public class ProductCategoryController extends BaseController { @Inject private ProductCategoryService productCategoryService; /** * 首页 */ @GetMapping public String index(ModelMap model) { model.addAttribute("rootProductCategories", productCategoryService.findRoots(5,0)); return "shop/product_category/index"; //手机端过来执行了 return "shop/product_category/index" 但是最后实际走的是 return "mobile/shop/product_category/index" } @GetMapping("pointCategory") public String pointCategory(ModelMap model) { model.addAttribute("rootProductCategories", productCategoryService.findRoots(5,1)); return "shop/product_category/index_point"; }
注意:
//手机端过来执行了 return "shop/product_category/index" 但是最后实际走的是 return "mobile/shop/product_category/index",也就是说你的mobile目录下要包含这样一个目录文件:
shop/product_category/index,index其实是一个index.ftl文件