How to change charset in struts2 to utf-8 [duplicate]
Asked Answered
D

4

8

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

update I 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.

Decentralization answered 31/8, 2012 at 18:5 Comment(2)
What have you tried exactly? You need to explain what you are trying to do exactly. The link you supplied already is already using UTF-8 encoding so this question makes no sense. I have to downvote this question for being one of the worst researched questions I have seen in a very long time.Denbighshire
What you page encoding? Must be <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> And .java files shoulde be UTF-8 too.Zymotic
E
15

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:

<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 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.

Edgell answered 31/8, 2012 at 18:15 Comment(0)
S
4

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

<jsp-config>
    <jsp-property-group > 
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>
Shipowner answered 4/10, 2012 at 8:1 Comment(0)
K
1

(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

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).

Kimikokimitri answered 7/4, 2014 at 9:35 Comment(0)
D
0

I'm using this this page declaration for an old version of Struts:

<%@ page language="java" pageEncoding="utf-8"%>

This causes Eclipse to show syntax errors on my system:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Derayne answered 1/2 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.