CSS正常加载却不生效原因排查
调试一个小的WEB项目,启动正常,但是样式却不能正常加载,刚开始以为是css文件的路径不对导致的,于是使用浏览器的开发者工具调试跟踪,结果发现样式的路径也正常。


换了几个浏览器,微软的edge,IE,谷歌的Chrome都试了下,都是同样的问题。
于是又各种排查JSP页面,一个不经意间把页面上的
<!DOCTYPE html>
声明给删除掉了,结果刷新页面发现样式正常了,于是就把所有页面的这个声明都删除,测试发现所有的页面也都正常,这让我很疑惑。
上网查了下这个标记的作用,只是让浏览器强制使用w3c标准来解析和渲染页面。可是对比了其他正常的项目,也同样有这个标记,并且也显示正常,就觉得这不应该是问题的所在。于是在继续查,有网友提到由于配置了过滤器Filter,设置content_type导致过这样的问题。
于是我也排查了代码中的过滤器,发现了下面的代码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
response.setContentType(contenttype);
chain.doFilter(request, response);
}
<filter>
<filter-name>contenttype</filter-name>
<filter-class>cn.ContTypeFilter</filter-class>
<init-param>
<param-name>contenttype</param-name>
<param-value>text/html</param-value>
</init-param>
</filter>
果然是同样的问题,不知道问什么要这样设置,把
response.setContentType(contenttype);这段代码注释掉。
页面再次加上
<!DOCTYPE html>
样式就能够正常显示。

结论:如果要为页面统一设置content_type,起码要把css文件给排除掉。
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36