Jfinal using interceptors to tell different browsers to return different results pages

Source: Internet
Author: User

    1. 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.

    2. Method of Thinking

      Interceptor intercepts to render, then modifies Viewpath

    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.