Bonjour,
Je suis novice en ce qui concerne Struts. Je désirerais vérifier qu'un champ soit bien rempli en utilisant le plug-in Validator. J'ai cherché des informations dans le forum j'ai tout essayé mais ca ne fonctionne toujours pas. Voici mes fichiers :
struts-config.xml :
validator.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <form-bean name="testValidatorForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="name" type="java.lang.String" /> </form-bean> <action path="/testValidator" type="st.ccam.fmm.web.actions.testValidatorAction" name="testValidatorForm" scope="request" validate="true" input="/pages/testValidator.jsp"> <forward name="success" path="/pages/testValidator.jsp" /> </action>
validator-rules.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 <form name="testValidatorForm"> <field property="name" depends="required" bundle="alternate"> <arg0 key="prompt.test.name"/> </field> </form>
ma page jsp, testValidator.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
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
52
53
54
55
56
57
58
59
60
61
62
63 <validator name="required" classname="org.apache.struts.validator.FieldChecks" method="validateRequired" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" msg="errors.required"> <javascript><![CDATA[ function validateRequired(form) { var isValid = true; var focusField = null; var i = 0; var fields = new Array(); oRequired = new required(); for (x in oRequired) { var field = form[oRequired[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'file' || field.type == 'select-one' || field.type == 'radio' || field.type == 'password') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (trim(value).length == 0) { if (i == 0) { focusField = field; } fields[i++] = oRequired[x][1]; isValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return isValid; } // Trim whitespace from left and right sides of s. function trim(s) { return s.replace( /^\s*/, "" ).replace( /\s*$/, "" ); } ]]> </javascript>
et ma page testValidatorForm.java :
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 <u>Formulaire de test avec Validator </u> <script> function alerte(){ alert("bouton clické hihi"); } </script> <p> <html:form action="/testValidator.do" focus="name" onsubmit="return validatetestValidatorForm(this);"> <html:text property="name" size="20"> <bean:message key="prompt.test.name"/> </html:text><html:errors property="name"/> <html:submit property="submit" value="Vérifier"/> </html:form> </p> <bean:write name="testValidatorForm" property="name" /> <html:javascript formName="testValidatorForm"/>
Quelqu'un aurait-il la solution à mon problème? Merci d'avance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = super.validate(mapping, request); if (!errors.isEmpty()) { ActionMessage message = new ActionMessage("prompt.test.name.required") ; errors.add("name",message) ; } return errors; }
Partager