Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

How to change charset in struts2 to utf-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.

Upvotes: 8

Views: 17193

Answers (4)

user2395139
user2395139

Reputation: 1

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" %>

Upvotes: 0

exoddus
exoddus

Reputation: 2340

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

Upvotes: 1

Christian
Christian

Reputation: 859

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>

Upvotes: 4

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

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.

Upvotes: 15

Related Questions