spring-boot2整合sitemesh3加jsp 专业填坑, 让你少走弯路

之前项目使用的是sitemesh2.4.2 搞的走了好多弯路 好了开场白就不说了 

1. 依赖

        
            org.sitemesh
            sitemesh
            3.0.1
        

2. 建立以下4个类

package com.dance.admin.sitemesh;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigure implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean siteMeshFilter() {
        FilterRegistrationBean fitler = new FilterRegistrationBean();
        fitler.setFilter(new Meshsite3Filter());
        return fitler;
    }
}


package com.dance.admin.sitemesh;

import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

/**
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 *
 * @developers LONGZHIQIANG
 * @createtime 2019-06-30 19:52.
 * @describe    sitemesh 自定义路径映射配置
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 */
public class Meshsite3Filter extends ConfigurableSiteMeshFilter {

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addDecoratorPath("/*", "/sitemesh/main")//拦截规则该路径会被转发
                .addExcludedPath("/static/**")                              //白名单
                .addTagRuleBundle(new Sitemesh3TagRuleBundle());       //自定义标签
    }
}


package com.dance.admin.sitemesh;

import org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State;

/**
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 * @developers LONGZHIQIANG
 * @createtime 2019-06-30 20:05.
 * @describe    自定义标签 页面使用  访问
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 */
public class Sitemesh3TagRuleBundle implements TagRuleBundle {

    public static String SITEMESH_TAG_CONTENT = "content";

    @Override
    public void install(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
        state.addRule(SITEMESH_TAG_CONTENT,
                new ExportTagToContentRule(siteMeshContext,contentProperty.getChild(SITEMESH_TAG_CONTENT),false));
    }

    @Override
    public void cleanUp(State state, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
        if (!(contentProperty.getChild(SITEMESH_TAG_CONTENT)).hasValue()) {
            (contentProperty.getChild(SITEMESH_TAG_CONTENT)).setValue(contentProperty.getValue());
        }
    }
}


package com.dance.admin.sitemesh;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 *
 * @developers LONGZHIQIANG
 * @createtime 2019-06-30 19:55.
 * @describe    转发至 sitemesh视图
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 */
@Controller
@RequestMapping("/sitemesh")
public class Sitemesh3Controller {

    @RequestMapping("/main")
    public String defaultDecorator() {

        return "/../form-views/main";
    }
}

3. 最后一步很坑 在网上找看100个播客 有99个都说到了后台代码怎么写 大该也就是我上面贴出的那样, 唯独他娘的没说到前台怎么用 以前用 sitemesh2.4.2的时候都是 

<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
​
 这样写 

​

 

记住3.0 是在需要被装饰的页面是要这样写

    
        

今天天气真好

在装饰面是这样引入

这样你的装饰页面就能将标签内的内容全部拿过来

若不是自定义标签的话 直接这样写



你可能感兴趣的:(spring-mvc,java)