java 如何将 struts2 中的字符集更改为 utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12220483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:05:28  来源:igfitidea点击:

How to change charset in struts2 to utf-8

javajspstruts2servlet-filters

提问by Aleksei Bulgak

Hi I have testfield in which I want to put test not in English(for example into Russian) but in my action class I get instead of text only ?????????. I trying to write simple filter which described Parameters charset conversion in struts2

嗨,我有测试字段,我想在其中测试不是英语(例如俄语),但在我的动作课中我得到而不是仅文本?????????。我试图写一个简单的过滤器,它描述 了 struts2 中的参数字符集转换

but it still do not work.. can somebody help me

但它仍然不起作用..有人可以帮我吗

updateI have this enter image description here

更新我有这个 在此处输入图片说明

<s:textfield key="index.login" name="login" />

I want to put into it test in Russian language and then send it to my action.but in my action class I get instead of text only ?????????.to fix this problem I need to change charset into utf8 instead of win1251.

我想用俄语对其进行测试,然后将其发送到我的 action.but 在我的 action 类中,我得到的不是文本?????????。要解决这个问题,我需要将字符集更改为 utf8 而不是 win1251。

回答by Paulius Matulionis

Create a filter:

创建过滤器:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig)
            throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

Declare it into your web.xml:

将其声明到您的 web.xml 中:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>your.package.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And your're good to go. Also make sure that your every JSPpage contains: <%@ page contentType="text/html;charset=UTF-8" language="java" %>. If your application is running on tomcat, make sure your add URIEncoding="UTF-8"attribute to your Connectorelement.

你可以走了。还要确保您的每一JSP页都包含:<%@ page contentType="text/html;charset=UTF-8" language="java" %>。如果您的应用程序在 tomcat 上运行,请确保您URIEncoding="UTF-8"Connector元素的add属性。

回答by Christian

If you need to force jsp to UTF-8 you can write the following in web.xml:

如果需要强制 jsp 为 UTF-8,可以在 web.xml 中编写以下内容:

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

回答by exoddus

(cannot comment on previus response)

(无法评论之前的回复)

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

Ok for web.xml > 2.3

好的 web.xml > 2.3

I'm not sure if in 2012 it doesn't exists yet, but take care that this element is only available for web.xml > 2.4 (that element doesn't exist in 2.3 http://java.sun.com/dtd/web-app_2_3.dtd).

我不确定 2012 年它是否还不存在,但请注意该元素仅适用于 web.xml > 2.4(该元素在 2.3 http://java.sun.com/dtd中不存在)/web-app_2_3.dtd)。