标签:
目的
前端做了两套页面,一套PC ,一套手机,我想通过程序控制返回的页面,而Controller不需要关心终端,只需要render固定的字符串。
思路方法
拦截器拦截到render,然后修改viewpath
实现
关键代码
public class AppConst { public static Routes SYSTEM_ROUTES = null ; } public class CmsConfig extends JFinalConfig { public void configConstant(Constants constants) { ConfigUtil.loadConfig(loadPropertyFile("application-test.properties", "application.properties", Const.DEFAULT_ENCODING)); constants.setBaseViewPath("/WEB-INF/page"); } public void configInterceptor(Interceptors interceptors) { interceptors.add(new RenderSwitchInterceptor()); } } public class WebRoute extends Routes { @Override public void config() { add("/web/product",WebProductCtrl.class,"web/product"); } } /** * PC 与手机 页面自动切换 关键代码 * Created by YangTao on 2015-8-31. */ public class RenderSwitchInterceptor implements Interceptor { @Override public void intercept(Invocation ai) { Controller c = ai.getController(); ai.invoke(); Render render = c.getRender(); String returnPath = render.getView(); // ctrl中render的字符串 String viewPath = ""; if (!returnPath.startsWith("/")){ // 如果不是以/开头,则说明不是页面的完整路径 viewPath = AppConst.SYSTEM_ROUTES.getViewPath(ai.getControllerKey()); } if (isMobile(c.getRequest())){ //手机 viewPath = viewPath.replace("/web/","/wap/"); } render.setView(viewPath+returnPath); } }
标签:
原文地址:http://my.oschina.net/wanily/blog/499694