NutzCN Logo
问答 nutzboot过滤器怎么配置呢
发布于 2158天前 作者 Hamming 1700 次浏览 复制 上一个帖子 下一个帖子
标签:

nutzboot过滤器怎么配置呢
想配置 SiteMesh 但是要配置过滤器 然后 就不知道怎么配置了
http://wiki.sitemesh.org/wiki/display/sitemesh3/Configuring+SiteMesh+3

19 回复

酱紫对不对

package io.nutz.nutzsite.common.starter;

import org.nutz.boot.starter.WebFilterFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.EnumSet;
import java.util.Map;

/**
 * @author Hamming_Yu on 2018/12/18.
 */
@IocBean
public class SitemeshFilterStarter  implements WebFilterFace {
    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Override
    public String getName() {
        return "sitemesh";
    }

    @Override
    public String getPathSpec() {
        return "/*";
    }

    @Override
    public EnumSet<DispatcherType> getDispatches() {
        return null;
    }

    @Override
    public Filter getFilter() {
        return ioc.get(ConfigurableSiteMeshFilter.class, "sitemeshFilter");
    }

    @Override
    public Map<String, String> getInitParameters() {
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setServletContext(ServletContext sc) {

    }
}

参考文档
http://wiki.sitemesh.org/wiki/display/sitemesh3/Getting+Started+with+SiteMesh+3

<web-app>

  ...

  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

getDispatches 返回REQUEST
getInitParameters 不要返回null

参考文档 配置
创建了/WEB-INF/sitemesh3.xml

<sitemesh>
    <mapping path="/*" decorator="/view/layouts/default.jsp"/>
    <!--<mapping path="/admin/*" decorator="/admin-decorator.html"/>-->
</sitemesh>
package io.nutz.nutzsite.common.starter;

import org.nutz.boot.starter.WebFilterFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.EnumSet;
import java.util.Map;

/**
 * @author Hamming_Yu on 2018/12/18.
 */
@IocBean
public class SitemeshFilterStarter extends ConfigurableSiteMeshFilter implements WebFilterFace {
    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Override
    public String getName() {
        return "sitemesh";
    }

    @Override
    public String getPathSpec() {
        return "/*";
    }

    @Override
    public EnumSet<DispatcherType> getDispatches() {
        return EnumSet.of(DispatcherType.REQUEST);
    }

    @Override
    public Filter getFilter() {
        return ioc.get(ConfigurableSiteMeshFilter.class, "sitemeshFilter");
    }

    @Override
    public Map<String, String> getInitParameters() {
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setServletContext(ServletContext sc) {

    }

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addDecoratorPath("/*", "/view/layouts/default.jsp");
    }
}

访问 是503错误

过滤器 哪里不对吗

23:29:55.111 org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:554) - Failed startup of context o.e.j.w.WebAppContext@273842a6{/,[file:///Users/haiming_yu/IdeaProjects/nutzsite/target/classes/static/, jar:file:/Users/haiming_yu/.m2/repository/org/nutz/nutzboot-starter-swagger/2.3-SNAPSHOT/nutzboot-starter-swagger-2.3-20181217.044738-77.jar!/static/],UNAVAILABLE}
org.nutz.ioc.IocException: IocBean[sitemeshFilter] For object [sitemeshFilter] - type:[class org.sitemesh.config.ConfigurableSiteMeshFilter]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:250)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
	at io.nutz.nutzsite.common.starter.SitemeshFilterStarter.getFilter(SitemeshFilterStarter.java:41)
	at org.nutz.boot.starter.servlet3.NbServletContextListener.lambda$contextInitialized$2(NbServletContextListener.java:120)
	at java.util.ArrayList.forEach(ArrayList.java:1255)
	at org.nutz.boot.starter.servlet3.NbServletContextListener.contextInitialized(NbServletContextListener.java:118)
	at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:957)
	at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:552)
	at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:922)
	at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:364)
	at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497)
	at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459)
	at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:852)

已解决

package io.nutz.nutzsite.common.starter;

import org.nutz.boot.starter.WebFilterFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.EnumSet;
import java.util.Map;

/**
 * @author Hamming_Yu on 2018/12/18.
 */
@IocBean
public class SitemeshFilterStarter extends ConfigurableSiteMeshFilter implements WebFilterFace {
    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Override
    public String getName() {
        return "sitemeshFilter";
    }

    @Override
    public String getPathSpec() {
        return "/*";
    }

    @Override
    public EnumSet<DispatcherType> getDispatches() {
        return EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
    }

    @Override
    public Filter getFilter() {
        return null;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setServletContext(ServletContext sc) {

    }

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addDecoratorPath("/*", "/view/layouts/default.jsp");
    }
}

    @Override
    public Filter getFilter() {
        return null;
    }

不对吧????

这个方法没被执行,还是不行

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addDecoratorPath("/*", "/view/layouts/default.jsp");
    }

getFilter不要返回null呀

酱紫也不行

return ioc.get(ConfigurableSiteMeshFilter.class, "sitemeshFilter");
return ioc.get(SitemeshFilterStarter.class, "sitemeshFilter");
return new ConfigurableSiteMeshFilter();

代码

package io.nutz.nutzsite.common.starter;

import org.nutz.boot.starter.WebFilterFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.EnumSet;
import java.util.Map;

/**
 * @author Hamming_Yu on 2018/12/18.
 */
@IocBean
public class SitemeshFilterStarter extends ConfigurableSiteMeshFilter implements WebFilterFace {
    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Override
    public String getName() {
        return "sitemesh";
    }

    @Override
    public String getPathSpec() {
        return "/*";
    }

    @Override
    public EnumSet<DispatcherType> getDispatches() {
        return EnumSet.of(DispatcherType.REQUEST);
    }

    @Override
    public Filter getFilter() {
        return new ConfigurableSiteMeshFilter();
    }

    @Override
    public Map<String, String> getInitParameters() {
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setServletContext(ServletContext sc) {

    }

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        builder.addDecoratorPath("/*", "/view/layouts/default.jsp");
    }
}

异常

23:37:18.251 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) - Get 'nutFilterStarter'<interface org.nutz.boot.starter.WebFilterFace>
[DEBUG] 23:37:18.251 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) - Get 'shiroFilterStarter'<interface org.nutz.boot.starter.WebFilterFace>
[DEBUG] 23:37:18.254 org.nutz.boot.starter.servlet3.NbServletContextListener.lambda$contextInitialized$2(NbServletContextListener.java:128) - add filter name=sitemesh pathSpec=/*
[WARN ] 23:37:18.256 org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:554) - Failed startup of context o.e.j.w.WebAppContext@b558294{/,[file:///Users/haiming_yu/IdeaProjects/nutzsite/target/classes/static/, jar:file:/Users/haiming_yu/.m2/repository/org/nutz/nutzboot-starter-swagger/2.3-SNAPSHOT/nutzboot-starter-swagger-2.3-20181217.044738-77.jar!/static/],UNAVAILABLE}
java.lang.NullPointerException
	at org.eclipse.jetty.servlet.Holder$HolderRegistration.setInitParameters(Holder.java:294)
	at org.nutz.boot.starter.servlet3.NbServletContextListener.lambda$contextInitialized$2(NbServletContextListener.java:129)
	at java.util.ArrayList.forEach(ArrayList.java:1255)
	at org.nutz.boot.starter.servlet3.NbServletContextListener.contextInitialized(NbServletContextListener.java:118)
	at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:957)
	at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:552)
	at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:922)
	at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:364)
	at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497)
	at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459)
	at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:852)
	at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:278)
	at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
	at org.eclipse.jetty.server.Server.start(Server.java:415)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.server.Server.doStart(Server.java:382)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.nutz.boot.starter.jetty.JettyStarter.start(JettyStarter.java:138)
	at org.nutz.boot.AppContext.startServers(AppContext.java:306)
	at org.nutz.boot.NbApp.execute(NbApp.java:206)
	at org.nutz.boot.NbApp.run(NbApp.java:178)
	at io.nutz.nutzsite.MainLauncher.main(MainLauncher.java:41)
[INFO ] 23:37:18.293 org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:292) - Started ServerConnector@6de30571{HTTP/1.1,[http/1.1]}{127.0.0.1:8080}
[INFO ] 23:37:18.294 org.eclipse.jetty.server.Server.doStart(Server.java:407) - Started @2867ms
    public Map<String, String> getInitParameters() {
        return new HashMap<String, String>();
    }

不对, 应该是

return this; // 因为的Filter就是自身
    @Override
    public Filter getFilter() {
        return this;
    }
    public Map<String, String> getInitParameters() {
        return new HashMap<String, String>();
    }

现在 访问 提示
resources 有jsp页面

HTTP ERROR 500
Problem accessing /view/layouts/default.jsp. Reason:

    JSP support not configured
添加回复
请先登陆
回到顶部