如何指定我希望我的 jsp-config 指令应用于我们应用程序中提供的所有 JSP 页面?

Posted

技术标签:

【中文标题】如何指定我希望我的 jsp-config 指令应用于我们应用程序中提供的所有 JSP 页面?【英文标题】:How do I specify that I want my jsp-config directive to apply to all JSP pages served in our application? 【发布时间】:2017-05-26 23:00:31 【问题描述】:

我们正在使用 JBoss 7.1.3.Final 和 Java 6。我想将 UTF-8 页面编码应用于我们网站上提供的所有 JSP 页面,因此我将其添加到我们的 web.xml 文件中

<jsp-config>
    <jsp-property-group>
        <url-pattern>/*</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

这会导致以下错误。

Message:
java.lang.StringIndexOutOfBoundsException: String index out of range: 0

StackTrace:
org.apache.jasper.JasperException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:581)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:397)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:402)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:346)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
    at org.owasp.csrfguard.CsrfGuardFilter.doFilter(CsrfGuardFilter.java:63)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.jsp.JspFileHandler.handleRequest(JspFileHandler.java:32)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:198)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:784)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:830)
    at org.apache.jasper.compiler.JspUtil.makeJavaIdentifier(JspUtil.java:803)
    at org.apache.jasper.JspCompilationContext.getServletClassName(JspCompilationContext.java:399)
    at org.apache.jasper.JspCompilationContext.getClassFileName(JspCompilationContext.java:580)
    at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:438)
    at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:406)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:645)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
    ... 69 more

虽然我们的页面存储有以下文件路径...

project/src/main/webapp/WEB-INF/views/folder1/index.jsp

我们的 URL 模式不以 .jsp 结尾。事实上,它们只是单词(例如“/folder1/index”)。但是,当我尝试这个指令时

<jsp-config>
    <jsp-property-group>
        <url-pattern>*</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

我的应用程序在我重新启动服务器时无法启动。我们如何指定我们希望将指令应用于从我们的站点提供的所有 JSP 页面?

【问题讨论】:

我认为“.jsp”模式只适用于以“.jsp”结尾的 URL。尽管这些文件在我的文件系统上被命名为“.jsp”,但最终调用这些文件的 URL 却没有(它们只是单词,例如 '/dir1/path1")。“.jsp”是否适用于不以“ .jsp”? 参考这个developer.jboss.org/thread/265872 JSP 规范 (2.3) 说您应该使用解析为 JSP 的 URL 模式。但尚不清楚这如何干扰 servlet 映射元素。您必须有一些 servlet 映射指令,是吗,因为您的 JSP 的名称不像 .jsp 。 参考这个developer.jboss.org/thread/265872 不幸的是,我运行的 JBoss (7.1.3) 版本与该线程中提到的版本不同。 【参考方案1】:

使用过滤器可以应用页面编码。

public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException 
            request.setCharacterEncoding(encoding);
    //                response.setContentType("text/html;charset=UTF-8");
                    response.setCharacterEncoding(encoding);
            filterChain.doFilter(request, response);

        

        public void init(FilterConfig filterConfig) throws ServletException 
            String encodingParam = filterConfig.getInitParameter("encoding");
            if (encodingParam != null) 
                encoding = encodingParam;
            
        

在 web.xml 中注册这个过滤器

【讨论】:

谢谢,但解决了手头的问题,我如何编写适用于所有 URL 的 URL 模式,因为 "/*" 似乎没有上班?

以上是关于如何指定我希望我的 jsp-config 指令应用于我们应用程序中提供的所有 JSP 页面?的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ Idea标记红色web.xml运行良好[关闭]

将指令指定为另一个指令的参数(插件)

指定URL地址。 Apache Tomcat

Java web中的web-xml中标签定义之jsp-config

sitemesh vs jsp-config (<include-prelude>)

如何指定我希望我的 LIB 链接到的 DLL 文件? - 视觉工作室 2019