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)); }}
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); }}
<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