• springmvc 实现pc端手机端适配(同一个请求根据不同客户端展示不同界面)


    转自:https://my.oschina.net/scipio/blog/477854

    主要解决两个问题:
    A、对于mobile的请求和pc端的请求,自动使用不同的模板目录
    B、对于返回json还是返回ftl,由springMVC自己判断,controller方法只写一个

    1、maven依赖

    <dependency>
              <groupId>org.springframework.mobile</groupId>
              <artifactId>spring-mobile-device</artifactId>
              <version>1.1.0.RELEASE</version>
          </dependency>

    2、配置多内容版本

    <!-- http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/ -->
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    
            <property name="defaultContentType" value="text/html" />
    
            <property name="mediaTypes">
                <map>
                    <!-- 告诉视图解析器,.json的请求返回的类型为json格式 -->
                    <entry key="json" value="application/json" />
                </map>
            </property>
    
            <property name="viewResolvers">
                <list>
                    <ref bean="liteDeviceDelegatingViewResolver" />
                </list>
            </property>
    
            <property name="defaultViews">
                <list>
                    <!-- 规范json返回内容 -->
                    <bean class="com.yami.infrastructure.jsonview.AppJsonView">
                    </bean>
                </list>
            </property>
        </bean>

    配置.json的请求返回json,其他返回view

    3、配置viewResolver

    <!-- Mobile ViewResolver -->
        <bean id="liteDeviceDelegatingViewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
            <constructor-arg>
                <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                    <property name="contentType" value="text/html; charset=UTF-8"/>
                    <property name="cache" value="true"/>
                    <property name="prefix" value=""/>
                    <property name="suffix" value=".ftl"/>
                    <property name="exposeSpringMacroHelpers" value="true"/>
                    <property name="exposeRequestAttributes" value="true"/>
                    <property name="exposeSessionAttributes" value="true"/>
                    <property name="requestContextAttribute" value="request"/>
                </bean>
            </constructor-arg>
            <!--mobile 的请求走这里 -->
            <property name="mobilePrefix" value="mobile/"/>
            <property name="tabletPrefix" value="tablet/"/>
            <property name="enableFallback" value="true"/>
        </bean>

    配置mobile来的请求ftl的走mobile目录下模板

    4、配置device判断

    <!-- spring mobile 配置-->
        <mvc:annotation-driven>
            <mvc:argument-resolvers>
                <!-- 让Device支持在注解的controller中使用 -->
                <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
                <!-- 让SitePreference支持在注解的controller中使用 -->
                <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
            </mvc:argument-resolvers>
        </mvc:annotation-driven>
    
        <bean id="deviceResolverHandlerInterceptor" class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
        <bean id="sitePreferenceHandlerInterceptor" class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
        <mvc:interceptors>
            <ref bean="deviceResolverHandlerInterceptor" />
            <ref bean="sitePreferenceHandlerInterceptor" />
        </mvc:interceptors>

    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文件
  • 相关阅读:
    Python进阶-----类、对象的相关知识
    Python进阶-----面向对象和类的基本定义
    Python基础-----hashlib模块
    Python基础-----configparser模块
    Python基础-----logging模块
    Python基础-----re模块(模糊匹配)
    Python基础-----xml模块
    Python基础-----shelve模块
    Python基础-----pickle模块
    Python基础-----json模块
  • 原文地址:https://www.cnblogs.com/longsanshi/p/10820912.html
Copyright © 2020-2023  润新知