简体   繁体   English

如何使用 Struts 2 在 JSP 中编写 UTF-8 符号

[英]How to write UTF-8 symbols in JSP with Struts 2

I have a list in my action which I field by values saved on a MySql DB.我的操作中有一个列表,我按保存在 MySql 数据库中的值对其进行字段处理。

I only have one object stored, which has designation .我只存储了一个对象,它的名称为

In JSP when I do this:在 JSP 中,当我这样做时:

<s:iterator value="unitesList">
    <s:property value="designation"/>
</s:iterator>
<s:select list="unitesList" name="unity" listValue="designation" listKey="id" />

I have this:我有这个:

€
<select name="unity" id="unity">
    <option value="1">?</option>
</select>

Why on <s:select> element the element is transformed in a ?为什么在<s:select>元素上, 元素被转换为 a ? ? ?

Place this on the top of the JSP page.将它放在 JSP 页面的顶部。 Symbols in UTF-8 encoding displays better than ? UTF-8 编码的符号比? . .

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

If the page encoding is not defined, the default is (the equivalent of the JSP page directive):如果未定义页面编码,则默认为(相当于 JSP page指令):

<%@page contentType="text/html; charset=ISO-8859-1" %>

The encoding you want is not ISO-8859-1 , but UTF-8 , that supports any language (take a look at Table 23-2 Valid Values for the IANA-Defined Character Set ).您想要的编码不是ISO-8859-1 ,而是UTF-8 ,它支持任何语言(查看表 23-2 IANA 定义的字符集的有效值)。

You can set your page, request and response encoding in three ways:您可以通过三种方式设置页面、请求和响应编码:

  1. Setting the contentType in each JSP:在每个 JSP 中设置contentType

     <%@page contentType="text/html; charset=UTF-8" %>
  2. Setting the pageEncoding in each JSP:在每个 JSP 中设置pageEncoding

     <%@page pageEncoding="UTF-8" %>
  3. Setting the <jsp-property-group> once in web.xml:在 web.xml 中设置一次<jsp-property-group>

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

The HTML <meta> tag will be generated accordingly. HTML <meta>标签将相应地生成。 Remember to not specify different encodings when unnecessarily using both pageEncoding and <jsp-property-group> , it is a translation-time error.请记住,在不必要地同时使用pageEncoding<jsp-property-group>时不要指定不同的编码,这是一个翻译时错误。

For more info about this, read the Oracle docs (pretty old, but still true).有关这方面的更多信息,请阅读Oracle 文档(很旧,但仍然正确)。

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

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