Voici le ActionForm :
et le JSP qui l'utilise :
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
64
65
66
67
68
69
70 package user; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; /** * Form bean for Chapter 03 sample application, "Hello World!" * * @author Kevin Bedell */ public final class NewUserNameForm extends ActionForm { private static final long serialVersionUID = 1L; private String name = "test"; public NewUserNameForm () { name="test2"; } // --------------------------------------------------------- Public Methods /** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.name = ""; System.out.println(this.name); } /** * Validate the properties posted in this request. If validation errors are * found, return an <code>ActionErrors</code> object containing the errors. * If no validation errors occur, return <code>null</code> or an empty * <code>ActionErrors</code> object. * * @param mapping The current mapping (from struts-config.xml) * @param request The servlet request object */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((name == null) || (name.length() < 1)) errors.add("name", new ActionError("user.NewUserNamePerson.no.name")); return errors; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Comment avec JSTL afficher la valeur du champ name du bean. Le code suivant ne fonctionne pas :
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 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html:html locale="true"> <head> <title><bean:message key="newUserName.jsp.title"/></title> <html:base/> </head> <body bgcolor="white"><p> <h2><bean:message key="newUserName.jsp.title"/></h2><p> <html:errors/><p> <html:form action="/newUserName.do"> Valeur du test : <c:out value="${userNameForm.name}"/> <br /> <br /> <bean:message key="newUserName.jsp.title"/> <html:text property="name" value="VALEUR DU NOM PAR DEFAUT" size="16" maxlength="16"/><br> <html:submit property="submit" value="Submit"/> <html:reset/> </html:form><br> </body> </html:html>
Même question avec Struts : j'aimerai afficher le nom par défaut à la place de VALEUR DU NOM PAR DEFAUT comme ci-après.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2<c:out value="${userNameForm.name}"/>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <html:text property="name" value="VALEUR DU NOM PAR DEFAUT" size="16" maxlength="16"/><br>
Merci.![]()
Partager