Extension SITEMESH3 Decoratorselector implementation based on request parameter invocation of decorative template page

Source: Internet
Author: User

    • Extended Ingress Filter Implementation:

package lab.s2jh.core.web.sitemesh;import org.sitemesh.decoratorselector;import  Org.sitemesh.builder.sitemeshfilterbuilder;import org.sitemesh.config.configurablesitemeshfilter;import The  org.sitemesh.webapp.WebAppContext;/** *  extension implements a selector that injects a dynamically positioned adorner based on the decorator value of the request parameter  */public  class ParamConfigurableSiteMeshFilter extends ConfigurableSiteMeshFilter {     protected void applycustomconfiguration (Sitemeshfilterbuilder builder)  {         //get the original default configuration decorator selector          DecoratorSelector<WebAppContext> defaultDecoratorSelector =  Builder.getdecoratorselector ();         //assigns a custom decoration selector, If the custom rule does not match, call the default selector to get         builder.setcustomdecoratorselector (new  paramdecoratorselector (Defaultdecoratorselector));     }} 
    • Custom adornment selector implementation:

package lab.s2jh.core.web.sitemesh;import java.io.ioexception;import  javax.servlet.http.httpservletrequest;import org.apache.commons.lang3.stringutils;import  org.sitemesh.decoratorselector;import org.sitemesh.content.content;import  org.sitemesh.webapp.webappcontext;/** *  selector for dynamically positioned adorners based on decorator value of request parameter  *  If the decorator parameter has a value, it returns "/web-inf/views/layouts/"  + decorator +  ". JSP" as the target Adornment template page  */public  class paramdecoratorselector implements decoratorselector<webappcontext> {     private DecoratorSelector<WebAppContext> defaultDecoratorSelector;     public paramdecoratorselector (decoratorselector<webappcontext>  Defaultdecoratorselector)  {        this.defaultdecoratorselector  = defaultdecoratorselector;    }    public striNg[] selectdecoratorpaths (Content content, webappcontext context)  throws  Ioexception {        // build decorator based  on the request        httpservletrequest request  = context.getrequest ();         string decorator =  request.getparameter ("Decorator");        if  ( Stringutils.isnotblank (decorator))  {             //returns the JSP decoration template number below the corresponding path according to the parameter value             return  new String[] {  "/web-inf/views/layouts/"  + decorator +  ". JSP"  };         }        //  Otherwise, fallback to the standard configuration        return  Defaultdecoratorselector.selectdecoratorpaths (content, context);     }}
    • To adjust the Web. XML configuration:

<filter> <filter-name>sitemeshFilter</filter-name> <FILTER-CLASS>LAB.S2JH.CORE.WEB.S Itemesh. paramconfigurablesitemeshfilter</filter-class> </filter> <filter-mapping> <FILTER-NAME&G T;sitemeshfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>



Extension SITEMESH3 Decoratorselector implementation based on request parameter invocation of decorative template page

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.