Rumah > hujung hadapan web > html tutorial > 扩展Sitemesh3 DecoratorSelector实现基于请求参数调用装饰模板页面的实现_html/css_WEB-ITnose

扩展Sitemesh3 DecoratorSelector实现基于请求参数调用装饰模板页面的实现_html/css_WEB-ITnose

WBOY
Lepaskan: 2016-06-21 09:19:12
asal
1601 orang telah melayarinya

扩展入口Filter实现:

package lab.s2jh.core.web.sitemesh;import org.sitemesh.DecoratorSelector;import org.sitemesh.builder.SiteMeshFilterBuilder;import org.sitemesh.config.ConfigurableSiteMeshFilter;import org.sitemesh.webapp.WebAppContext;/** * 扩展实现注入基于request参数decorator值进行动态定位装饰器的选择器 */public class ParamConfigurableSiteMeshFilter extends ConfigurableSiteMeshFilter {    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {        //获取原有默认配置装饰选择器        DecoratorSelector<WebAppContext> defaultDecoratorSelector = builder.getDecoratorSelector();        //赋给自定义装饰选择器,则自定义规则未匹配时调用默认选择器获取        builder.setCustomDecoratorSelector(new ParamDecoratorSelector(defaultDecoratorSelector));    }}
Salin selepas log masuk

定制装饰选择器实现:

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;/** * 基于request参数decorator值进行动态定位装饰器的选择器 * 如果decorator参数有值,则返回"/WEB-INF/views/layouts/" + decorator + ".jsp"作为目标装饰模板页面 */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)) {            //按照参数值返回对应路径下面的jsp装饰模板页码            return new String[] { "/WEB-INF/views/layouts/" + decorator + ".jsp" };        }        // Otherwise, fallback to the standard configuration        return defaultDecoratorSelector.selectDecoratorPaths(content, context);    }}
Salin selepas log masuk

调整web.xml配置:

    <filter>        <filter-name>sitemeshFilter</filter-name>        <filter-class>lab.s2jh.core.web.sitemesh.ParamConfigurableSiteMeshFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>sitemeshFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>
Salin selepas log masuk



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan