繁体   English   中英

如何修复apache-tomcat-8.5.39中的默认内容类型呈现问题?

[英]How to fix default content-type rendering issue in apache-tomcat-8.5.39?

我正在设置我的应用程序以使用最新的apache-tomcat-8.5.39进行测试。 当我尝试在任何Web浏览器上加载我的应用程序时,我的资源很少正常加载。

由于Web服务器返回的内容类型,很少有资源无法正确加载。

这些资源是本地资源(从服务器获取):ex:

CSS

请求URL: http:// localhost:8080 / workflow / css / wfstyle.css

SVG

请求URL: http:// localhost:8080 / workflow / images / svg / Delete.svg

但是所有CDN都正常加载

请求网址: https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-ltr.css

这就是我将SVG添加到JSP页面的方式:

<span class="esg-icon__container">
   <img src="<%=request.getContextPath()%>/images/svg/Delete.svg"></img>
</span>

和CSS到JSP:

<%
    if(request.getLocale().getLanguage().contains("ar")){
%>

<link href="https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-rtl.css" rel="stylesheet"/>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/abc-design-system-rtl-custom.css" />

<% } else { %>

<link href="https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-ltr.css" rel="stylesheet"/>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/abc-design-system-ltr-custom.css" />

<%}%>

tomcat的web.xml:

<mime-mapping>
     <extension>svg</extension>
     <mime-type>image/svg+xml</mime-type>
</mime-mapping>

预期的内容类型

对于CSS:

对于SVG:

我做了这样的事情:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
        String charEncoding = "UTF-8";
        String contentType = "image/svg+xml;";

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String file = request.getServletPath();

        if(file.contains(".svg")){
            if(!contentType.equals(servletResponse.getContentType())) {
                servletResponse.setContentType(contentType);
                servletResponse.setCharacterEncoding(charEncoding);
            }
            if(!charEncoding.equals(servletRequest.getCharacterEncoding())) {
                servletRequest.setCharacterEncoding(charEncoding);
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

它有效:

内容类型:image / svg + xml; UTF-8;

这是一个好习惯吗? 如果没有其他方法做同样的事情?

我使用的是Java1.8和Strust1.2。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM