Bonjour,

Je suis en train de m'initier au javaee 7. J'ai donc développé une mini-application pour me mettre dans le bain, mais je rencontre un léger soucis d'encodage au niveau de l'enregistrement/la lecture du ManagedBean. En effet, il ne semble pas gérer correctement les caractères spéciaux tels que les accents.

J'ai pourtant appliqué la solution décrite dans un des posts du forum (http://www.developpez.net/forums/d51...s/#post3103500), qui consiste à appliquer un filtre, mais apparement j'ai dû oublier un détail.

Voici mon web.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>UTF8_Filter</filter-name>
        <filter-class>com.loloof64.my_friends.core.UTF8_Filter</filter-class>
    </filter>
 
   <filter-mapping>
        <filter-name>UTF8_Filter</filter-name>
        <url-pattern>/*</url-pattern>
   </filter-mapping>
    
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/edit_friend.xhtml</welcome-file>
    </welcome-file-list>
</web-app>
Voici mon Managed Bean CurrentFriend :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.loloof64.my_friends.mbeans;
 
import com.loloof64.my_friends.core.SingleFriend;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
 
/**
 *
 * @author laurent-bernabe
 */
@Named
@SessionScoped
public class CurrentFriend extends SingleFriend implements Serializable {
 
}
Voici sa classe mère SingleFriend :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.loloof64.my_friends.core;
 
/**
 *
 * @author laurent-bernabe
 */
public class SingleFriend {
 
    /**
     * Creates a new instance of SingleFriend
     */
    public SingleFriend() {
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    private String firstName;
    private String lastName;
    private int age;
 
}
voici la page edit_friend.xhtml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>My Friends : friend editing</title>
    </h:head>
    <h:body>
        <h2>Informations about your friend : </h2>
        <h:form>
            <table>
                <tr>
                    <td>First name</td>
                    <td><h:inputText id="first_name" value="#{currentFriend.firstName}"
                            label="first name" required="true"/></td>
                </tr>
                <tr>
                    <td>Last name</td>
                    <td><h:inputText id="last_name" value="#{currentFriend.lastName}"
                         label="last name" required="true"/></td>
                </tr>
                <tr>
                    <td>Age</td>
                    <td><h:inputText id="age" value="#{currentFriend.age}"
                         label="age" required="true">
                            <f:validateLongRange minimum="0" maximum="200" />
                        </h:inputText></td>
                </tr>
            </table>
            <p><h:commandButton value="Submit" action="add_friend"/></p>
            <p><h:message for="age"/></p>
        </h:form>
    </h:body>
</html>
Voici la page add_friend.xhtml:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>My Friends : friend recording</title>
    </h:head>
    <h:body>
        <p>Recorded your friend : </p>
        <ul>
            <li>First name : #{currentFriend.firstName} </li>
            <li>Last name : #{currentFriend.lastName} </li>
            <li>Age : #{currentFriend.age} </li>
        </ul>
        <h:form>
            <h:commandLink value="Go back" action="edit_friend"/>
        </h:form>
    </h:body>
</html>
Voici le filtre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package com.loloof64.my_friends.core;
 
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;
 
/**
 *
 * @author laurent-bernabe
 */
public class UTF8_Filter implements Filter{
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
 
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }
 
    @Override
    public void destroy() {
 
    }
 
}
Je développe sous Netbeans 8.0 avec GlassFish 4.
Je vous remercie d'avance pour votre aide.