Objective
The front-end made two sets of pages, a set of PCs, a set of mobile phones, I want to program the returned pages, and the controller does not need to care about the terminal, only a fixed string of render.
Method of Thinking
Interceptor intercepts to render, then modifies Viewpath
Realize
Key code
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 () &nbSp { add ("/web/product", Webproductctrl.class, "web/product"); }}/** * PC and Mobile page auto-switching key code * 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 string in Render String viewPath = ""; if (!returnpath.startswith ("/")) { // is not the full path of the page if it does not begin with/start viewpath = appconst.system_routes.getviewpath (Ai.getcontrollerkey ()); } if (IsMobile (C.getrequest ())) { //Mobile viewPath = Viewpath.replace ("/web/", "/wap/"); } render.setview (Viewpath+returnpath); } }
Jfinal using interceptors to tell different browsers to return different results pages