Bonjour à tous,

Comme le titre l'indique j'ai un soucis pour implémenter le validator côté serveur de Struts 1.2.

J'ai lu la FAQ, la doc Struts mais soit j'ai mal lu soit j'ai oublié quelque chose mais en re re relisant la doc plusieurs fois, je ne vois pas ce qui ne fonctionne pas.

Cà compile et j'arrive dans main.jsp alors que je test sans remplir le formulaire dans index.jsp. Il est censé m'indiquer les champs requis mais c'est comme-ci il n'y avait pas de validation défini, pourtant il charge bien validation.xml et validator-rules.xml.

Struts-config.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
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>   
  <form-beans>
    <form-bean name="brainBean" type="BrainBean" >
      <form-property name="userName" type="java.lang.String"/>
      <form-property name="password" type="java.lang.String"/>
      <form-property name="freetext" type="java.lang.String"/>
    </form-bean>
  </form-beans>  
  <global-forwards type="org.apache.struts.action.ActionForward">
    <forward path="/main.jsp" name="main" redirect="false"/>
    <forward path="/index.jsp" name="index" redirect="true"/>
  </global-forwards>  
  <action-mappings>
    <action path="/logon" name="brainBean" type="LogonAction" input="/index.jsp" scope="session" validate="true"/>
  </action-mappings> 
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/tlds/validator-rules.xml,/WEB-INF/validation.xml"/>
  </plug-in> 
</struts-config>
J'ai essayé avec et sans les form-property mais çà ne change rien

validation.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
 
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
    <formset>
        <form name="breanBean">
            <field property="userName" depends="required">
                <arg0 key="error.username"/>
            </field>
            <field property="password" depends="required">
                <arg0 key="error.password"/>
            </field>
        </form>
    </formset>
</form-validation>
index.jsp qui lance le formulaire
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
 
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<html:html>
<head>
<title>
    <bean:message key="title"/>
</title>
</head>
 
<BODY>
<html:errors/>
<table>
<html:form action="/logon.do" focus="userName">
  <tr>
    <td><bean:message key="login.username"/></td>
    <td><html:text property="userName" size="50"/></td>
  </tr>
  <tr>
    <td><bean:message key="login.password"/></td>
    <td><html:password property="password" size="50"/></td>
  </tr>
  <tr>
    <td><bean:message key="login.freetext"/></td>
    <td><html:textarea property="freeText" rows="3" cols="50"/></td>
  </tr>
  <tr>
    <td>
			<html:submit>
				<bean:message key="login.button.submit"/>
			</html:submit>
    </td>
    <td>
			<html:reset>
				<bean:message key="login.button.reset"/>
      </html:reset>
    </td>
  </tr>
</html:form>
</table>
 
</body>
</html:html>
BrainBean qui étend ValidatorForm
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
 
import org.apache.struts.validator.*;
 
public class BrainBean extends ValidatorForm {
 
  	private String userName = null;
  	public String getUserName() { return userName; }
  	public void setUserName(String userName) {	this.userName = userName; }
 
  	private String password = null;
  	public String getPassword() { return password; }
  	public void setPassword(String password) {	this.password = password; }
 
  	private String freeText = null;
  	public String getFreeText() { return freeText; }
  	public void setFreeText(String freeText) {	this.freeText = freeText; }
 
}
LogonAction qui étend Action
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
import org.apache.struts.action.*;
import javax.servlet.http.*;
 
public class LogonAction extends Action {
 
  public ActionForward execute (ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response ){
 
    return actionMapping.findForward("main");
  }
 
}
et ma page forward: main.jsp
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
 
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
 
<html:html>
<head>
<title>
    <bean:message key="title"/>
</title>
</head>
<html:errors/>
<BODY>
 
<bean:write property="userName" name="brainBean" /><br>
<bean:write property="password" name="brainBean" /><br>
 
Le texte tapé est : <br> <bean:write property="freeText" name="brainBean" />
 
</BODY>
</html:html>
ApplicationRessources.properties
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
 
# Global
exception = An exception occurs while performing the action, please see the log file
title = Testeur d'implémentation de Struts 1.2
 
# Login
login.username = Entrez votre Login
login.password = Entrez votre Password
login.freetext = Entrez un texte quelconque
login.button.submit = Envoyer
login.button.reset = Vider
 
# Erreurs
error.username = Login field
error.password = Password field
 
# Error messages for Validator framework validations
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.0.   errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
Tant qu'à faire, voici 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>Projet Struts</display-name>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
</web-app>
J'ai lu la doc, les exemples... il ne semble rien manquer, et pourtant :