如何在Spring启动应用程序中配置servlet init-param

我有一个使用
Spring MVC的
Spring启动应用程序(版本1.2.1.RELEASE),我需要在JspServlet上设置开发init参数.在web.xml中,这将如下所示:

<servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        ...
        <init-param>
            <param-name>development</param-name>
            <param-value>false</param-value>
        </init-param>
         ...
</servlet>

我原本以为我可以这样做:How to configure spring-boot servlet like in web.xml?,但我不认为我可以用新的JspServlet()替换新的MeteorServlet()并继续我的方式,因为我确信JspServlet.init(ServletConfig配置)需要使用有效的ServletConfig调用.当然,如果我在初始化时没有正确地尝试它,应用程序会给我一个空指针异常(这是因为私有瞬态JspRuntimeContext rctxt;为null):

java.lang.NullPointerException: null
    at org.apache.jasper.servlet.JspServlet.periodicEvent(JspServlet.java:361)
    at org.apache.catalina.core.StandardWrapper.backgroundProcess(StandardWrapper.java:679)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Thread.java:745)

此外,通过实现TomcatEmbeddedServletContainerFactory并提供一个servlet来覆盖ServletConfig的init方法和getInitParameter方法,我们有一个非常hacky的方法,但它有点讨厌,所以我希望有更好的方法.任何指导将不胜感激.

最后,读取此init-param的代码位于org.apache.jasper.EmbeddedServletOptions类中,并且似乎只查找ServletConfig init参数,因此我无法将其指定为上下文参数(我不认为)像server.context-parameters.name = value这样的东西很棒.

String development = config.getInitParameter("development");
if (development != null) {
    if (development.equalsIgnoreCase("true")) {
        this.development = true;
    } else if (development.equalsIgnoreCase("false")) {
        this.development = false;
    } else {
        if (log.isWarnEnabled()) {
            log.warn(Localizer.getMessage("jsp.warning.development"));
        }
    }

}

如果我尝试点击应用程序,我会收到以下错误:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:646)
at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:833)
at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:806)
at org.apache.jasper.JspCompilationContext.getServletClassName(JspCompilationContext.java:332)
at org.apache.jasper.JspCompilationContext.getClassFileName(JspCompilationContext.java:504)
at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:444)
at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:408)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:557)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:335)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:291)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration$MetricsFilter.doFilterInternal(MetricFilterAutoConfiguration.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

最佳答案 或者您可以将参数添加到application.properties文件中,如下所述:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

寻找:

    server.jsp-servlet.init-parameters.* =#用于配置JSP servlet的Init参数

例如:

server.jsp-servlet.init-parameters.development=true
点赞