现在的位置: 首页 > 综合 > 正文

使用jsp include标签包含静态页面出现的乱码问题

2018年05月19日 ⁄ 综合 ⁄ 共 1757字 ⁄ 字号 评论关闭

如果jsp页面包含html页面,有时候设置html页面的编码并不能正常显示html里面的中文,我当时在设置页面编码无效的情况下使用配置web.xml的方式解决了这个问题。

 jsp include页面出现问题的解决方法:

1.当jsp include动态文件时(jsp文件)可以在被include的jsp文件头部加上代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

2.当jsp include静态文件时(html文件)可以在被include的html文件的<head></head>标签内加上代码:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
不能够修改被include的文件时可以修改tomcat的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
   <span style="color:#ff0000;"> <jsp-config> 
        <jsp-property-group> 
            <description> 
                Special property group for JSP Configuration JSP  
                example.  
            </description> 
            <display-name>JSPConfiguration</display-name> 
            <url-pattern>*.html</url-pattern> 
            <el-ignored>true</el-ignored> 
            <page-encoding>UTF-8</page-encoding> 
            <scripting-invalid>false</scripting-invalid> 
            <include-prelude></include-prelude> 
            <include-coda></include-coda> 
        </jsp-property-group> 
    </jsp-config> </span>
 
</web-app>

==一般在解决此问题时只需要在web.xml中配置html页面编码,不需要配置jsp页面的编码,否则样式会出现问题==

      Tomcat 5之后,支持了 JSP 2.0 的规格,同时也支持了部分 J2EE 1.4 的规范,在 J2EE 1.4 的规范中,有关 JSP 的部份,有一个 <jsp-config>;的 XML Tag,这个 XML 区块用来定义与 JSP 相关的特殊属性。
    <el-ignored>                    若为true,表示不支持EL语法
    <page-encoding>            若为true,表示不支持<% scripting %>语法
    <scripting-invalid>           设置页面编码
    <include-prelude>           设置页面抬头,扩展名为.jspf
    <include-coda>               设置页面结尾,扩展名为.jspf

      在一个编码为utf-8的页面中,使用<jsp:include>包含另一个.jsp/.html文件时,被包含的页面单独浏览正常,但被包含后就会遇到乱码问题。解决的办法是,在每个被包含的页面开始加上下面一行<% page contentType=&quot;text/html;charset=utf-8&quot; %>这个方法可以解决jsp include jsp的中文乱码问题。也就是说,被包含的页面必须改成.jsp,哪怕它的内容只有静态html,否则的话还是会出现乱码。

      经测试,在被include页面里声明页面编码才是王道。web.xml里中进行配置,很多时候都不起效的。

抱歉!评论已关闭.