本文介绍了如何将struts2中的字符集更改为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个测试字段,我想在其中进行非英语测试(例如俄语)但在我的动作类中,我得到的不是文本 ?????????.我试图编写简单的过滤器来描述 Struts2 中的参数字符集转换

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

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

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

更新我有这个

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

我想用俄语对其进行测试,然后将其发送到我的操作中.但是在我的操作类中,我得到的不是文本 ?????????.to解决这个问题我需要把charset改成utf8而不是win1251.

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.

推荐答案

创建过滤器:

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() {

    }
}

在你的 web.xml 中声明它:

Declare it into your 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>

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

And your're good to go. Also make sure that your every JSP page 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 Connector element.

这篇关于如何将struts2中的字符集更改为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 12:48
查看更多